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/20231031152959.3f0004d7%40gmail.com.

Attachment: pgp6GljosgDtl.pgp
Description: OpenPGP digital signature

Reply via email to