There is no backup system. ... I wouldn't have been able
to create such a thing ... I wouldn't dare use it in
production ... the rest of us have to build our
wheels with the stick-n-stones within our reach.
> The backup system returns the 'backup_file' information so you can
> then operate on the built in backup, like moving it to a central
> location ...
This can be achieved with ~20 lines of code. For example, let's
declare the module defaults
- hosts: all
module_defaults:
lineinfile:
create: true
backup: true
and register the results
tasks:
- lineinfile:
path: /tmp/test1.txt
line: "{{ item }}"
loop: [line1, line2, line3]
register: ns_result_001
- lineinfile:
path: /tmp/test2.txt
line: line1
register: ns_result_0021
- lineinfile:
path: /tmp/test2.txt
line: line2
register: ns_result_0022
Declare the list of all variables *ns_result_* and declare the list
of all backup files
vars:
my_ns_results: "{{ q('vars', *q('varnames', '^ns_result_*')) }}"
ns_bfiles: "{{ (my_ns_results|json_query('.backup') +
my_ns_results|json_query('.results.backup'))|
flatten>select }}"
Fetch the backup files to the controller and optionally delete them on
the remote hosts
post_tasks:
- fetch:
src: "{{ item }}"
dest: /tmp/ansible/backup
loop: "{{ ns_bfiles }}"
- file:
src: "{{ item }}"
state: absent
loop: "{{ ns_bfiles }}"
when: ns_bfiles_delete|d(false)|bool
For example,
tree /tmp/ansible/backup/
/tmp/ansible/backup/
└── test_11
└── tmp
├── test1.txt.7143.2023-10-31@14:18:07~
├── test1.txt.7156.2023-10-31@14:18:08~
└── test2.txt.7182.2023-10-31@14:18:11~