---
#	ANSIBLE_LOCALHOST_WARNING=false ansible-playbook rsync_backup_tree.yml

- hosts: 127.0.0.1
  connection: local
  tasks:

  - set_fact: {
      directory: {
        source: '/tmp/rsync/source',
        destination: '/tmp/rsync/destination',
        mySubDir: 'mySubDir',
        backupDir: 'backup',
        },
      filename: {
        base: 'file',
        variant: {
          dir: 'A',
          subDir: 'B',
          },
        },
      textInFile: {
        line1: 'Hello world from file ',
        line2: 'edit 1',
        line3: 'edit 2',
        },
      }

  ####################################### setup
  - name: Create directories
    file:
      path: "{{ item }}"
      state: directory
    loop:
      - "{{ directory.source + '/' + directory.mySubDir }}"
      - "{{ directory.destination }}"

  - name: Create test data in 'source' dir
    lineinfile:
      path: "{{ directory.source + '/' + filename.base + filename.variant.dir + item }}"
      line: "{{ textInFile.line1 + filename.variant.dir + item }}"
      create: yes
    with_sequence: count=2

  - name: Create test data in 'mySubDir'
    lineinfile:
      path: "{{ directory.source + '/' + directory.mySubDir + '/' + filename.base + filename.variant.subDir + item }}"
      line: "{{ textInFile.line1 + filename.variant.subDir + item }}"
      create: yes
    with_sequence: count=2


  ####################################### rsync 1 (initial copy into empty dir)
  - name: Rsync (1)
    command: "{{ 'rsync -avz ' + directory.source + '/ ' + directory.destination }}"


  ####################################### edit 'fileA1' and 'fileB1'
  - name: Append text to files
    lineinfile:
      path: "{{ item }}"
      insertafter: "^{{ textInFile.line1 }}"
      line: "{{ textInFile.line2 }}"
    loop:
      - "{{ directory.source + '/' + filename.base + filename.variant.dir + '1' }}"
      - "{{ directory.source + '/' + directory.mySubDir + '/' + filename.base + filename.variant.subDir + '1' }}"


  ####################################### rsync 2 (with backup option)
  - name: Get date to name the backup dir
    command: "date +%F_%H-%M-%S"
    register: date

  - set_fact:
#      realBackupDir: "{{ directory.backupDir }}"
#      realBackupDir: "{{ directory.backupDir + '1' }}"
      realBackupDir: "{{ directory.backupDir +'_' + date.stdout }}"

  - command: "rsync -avz --backup --backup-dir={{ realBackupDir }} {{ directory.source }}/ {{ directory.destination }}"
    register: rsync

  - command: "tree {{ directory.destination + '/' + realBackupDir }}"
    register: tree

  - command: "cat {{ directory.destination + '/' + realBackupDir + '/' + filename.base + filename.variant.dir + '1' }} {{ directory.destination + '/' + realBackupDir + '/' + directory.mySubDir + '/' + filename.base + filename.variant.subDir + '1' }}"
    register: cat

  - debug:
      var: "{{ item }}"
    loop:
      - rsync.stdout_lines
      - tree.stdout_lines
      - cat.stdout_lines


  ####################################### edit 'fileA1' and 'fileB1' again
  - name: Append text to files
    lineinfile:
      path: "{{ item }}"
      insertafter: "^{{ textInFile.line2 }}"
      line: "{{ textInFile.line3 }}"
    loop:
      - "{{ directory.source + '/' + filename.base + filename.variant.dir + '1' }}"
      - "{{ directory.source + '/' + directory.mySubDir + '/' + filename.base + filename.variant.subDir + '1' }}"


  ####################################### rsync 3 (with backup option)
  - name: Get date to name the backup dir
    command: "date +%F_%H-%M-%S"
    register: date

  - set_fact:
#      realBackupDir: "{{ directory.backupDir }}"
#      realBackupDir: "{{ directory.backupDir + '1' }}"
      realBackupDir: "{{ directory.backupDir +'_' + date.stdout }}"

  - command: "rsync -avz --backup --backup-dir={{ realBackupDir }} {{ directory.source }}/ {{ directory.destination }}"
    register: rsync

  - command: "tree {{ directory.destination + '/' + realBackupDir }}"
    register: tree

  - command: "cat {{ directory.destination + '/' + realBackupDir + '/' + filename.base + filename.variant.dir + '1' }} {{ directory.destination + '/' + realBackupDir + '/' + directory.mySubDir + '/' + filename.base + filename.variant.subDir + '1' }}"
    register: cat

  - debug:
      var: "{{ item }}"
    loop:
      - rsync.stdout_lines
      - tree.stdout_lines
      - cat.stdout_lines


  ####################################### cleaning
  - name: Clean before leaving
    file:
      path: "{{ item }}"
      state: absent
    loop:
      - "{{ directory.source }}"
      - "{{ directory.destination }}"

...
