That looks fine as far as it goes. I think we all agree that none of this is particularly hard to implement.
However, the way I read the original poster's original problem, there is an implied constraint: One cannot go creating additional files in the .conf directory. Full stop. So the "backup: true" option - which lacks the long sought "backup_dir:" modifier - is off the table. The options I see are: (a) create and maintain a staging directory and copy the files from there to the target .conf directory; (b) handle copies in advance - which is essentially implementing what you wish "backup_dir:" would do if existed; (c) give up on "roll your own 'backup: true'" altogether. On Tuesday, October 31, 2023 at 10:30:20 AM UTC-4 Vladimir Botka wrote: > On Tue, 31 Oct 2023 05:17:35 -0700 (PDT) > Todd Lewis <[email protected]> wrote: > > > 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. > > > > On Monday, October 30, 2023 at 11:34:33 AM UTC-4 Brian Coca wrote: > > > > > 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, > > shell> 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~ > > -- > Vladimir Botka > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5044711c-c426-4f81-a484-a00921d5b99an%40googlegroups.com.
