Reputation: 99
I have a use case i want to create a backup file with latest time stamp and then I want to copy the same/latest back up file to destination.(I was using item
and with_items
for that). But ansible
overriding the existing backup file and creating new file with same time stamp. When I copy the latest backup file it is not moving to destination because same time stamp file already exists.
---
# understanding with_items for correct syntax
- name: create a back up file
file:
path: /test/backup_path/backup_{{ansible_date_time.date}}.bkp
state: touch
mode: u=rw,g=r,o=r
- name: get the latest back up file
shell: ls -1t /test/backup_path | head -1
register: latest_bkp
- debug: msg="{{ latest_bkp.stdout_lines }}"
- name: Copy the latest file to restore path
copy:
src: /test/backup_path/{{ item }}
dest: /test/restore_path/
remote_src: yes
owner: root
group: root
mode: '0644'
force: yes
with_items: "{{ latest_bkp.stdout_lines }}"
My target node output
[root@localhost ~]# ls -alrt /test/backup_path/
total 0
-rw-r--r--. 1 root root 0 Aug 19 17:33 backup1
-rw-r--r--. 1 root root 0 Aug 19 17:34 backup2
drwxr-xr-x. 5 root root 59 Aug 19 18:03 ..
drwxr-xr-x. 2 root root 65 Aug 19 18:49 .
-rw-r--r--. 1 root root 0 Aug 19 19:07 backup_2019-08-19.bkp
[root@localhost ~]# ls -alrt /test/restore_path/
total 0
-rw-r--r--. 1 root root 0 Aug 19 17:34 backup2
drwxr-xr-x. 5 root root 59 Aug 19 18:03 ..
-rw-r--r--. 1 root root 0 Aug 19 18:49 backup_2019-08-19.bkp
drwxr-xr-x. 2 root root 50 Aug 19 18:49 .
Upvotes: 2
Views: 11745
Reputation: 517
My ultimate target is creating a back up file with time stamp and transferring the same file to destination
As I look in your example you create and copy empty none modified file and restart playbook some times.
In this case:
1) Ansible recreates source file in /test/backup_path/
(touch the same file, but set some permissions). And its time stamp updates after every running playbook.
- name: create a back up file
file:
path: /test/backup_path/backup_{{ansible_date_time.date}}.bkp
state: touch
mode: u=rw,g=r,o=r
2) Ansible will copy empty or none modified source file to /test/restore_path/
only at first running playbook or when there is no same destination file (condition of Ansible Idempotency). For this reason time stamp of destination file didn't update after playbook restarting (there is older file with same content). Idempotency means you can be sure of a consistent state in your environment.
- name: Copy the latest file to restore path
copy:
src: /test/backup_path/{{ item }}
dest: /test/restore_path/
remote_src: yes
owner: root
group: root
mode: '0644'
force: yes
with_items: "{{ latest_bkp.stdout_lines }}"
Let's say I will add one new line test
to source file. In this case content of file will be updated.
- name: create a back up file
file:
path: /test/backup_path/backup_{{ansible_date_time.date}}.bkp
state: touch
mode: u=rw,g=r,o=r
- name: Add data to file
lineinfile:
path: /test/backup_path/backup_{{ansible_date_time.date}}.bkp
line: test
After rerunning playbook (only one time) you can sure that source and destination file were updated and have same content and time stamp (note please it's idempotency too).
To resolve your issue you can use none idempotent module Ansible
for copying file (note please it is not best practice).
For example:
instead:
- name: Copy the latest file to restore path
copy:
src: /test/backup_path/{{ item }}
dest: /test/restore_path/
remote_src: yes
owner: root
group: root
mode: '0644'
force: yes
with_items: "{{ latest_bkp.stdout_lines }}"
just use:
- name: Copy the latest file to restore path
shell: cp -rf /test/backup_path/{{ item }} /test/restore_path/{{ item }}
with_items: "{{ latest_bkp.stdout_lines }}"
- name: Change file permissions
file:
owner: root
group: root
mode: '0644'
path: /home/restore_path/{{ item }}
with_items: "{{ latest_bkp.stdout_lines }}"
Upvotes: 1