How to define multiple with_items using registered variables in Ansible

Viewed 619

AM in a process of achieving below list of tasks, and could someone please rectify the playbook or suggest a way to get the requirement done. High level purpose of the activity is below: find previous day's log files in multiple paths and archive them under a date wise folder (folder has to be created for particular date) in a different path.

My approach is: Create a date wise directory and then search the previous day's log files and then copy them in to the newly created directory and then archive it.

I am having an issue when defining paths and variables in copy section. Can someone help with this?

- name: Purge old spider logs
  become: true
  hosts: node1
  vars:
      date: "{{ lookup('pipe', 'date +%Y-%m-%d') }}"
  tasks:
    - name: create a directory
      file:
        path: /path/{{ date }}
        state: directory
        mode: '777'
      register: logdir

    - name: Find log files
      find:
        path: /test/logs
        age: 3600
        patterns:
          - "name.log.*"
        recurse: yes
      register: testlogs

    - debug:
        var: testlogs.path

    - debug:
        var=item.files
      with_items: '{{ testlogs.files }}'

    - name: Copy files in to backup location
      copy:
        src: "{{ item.files }}"
        dest: "{{ item.path }}"
      with_items:
           - '{{ item.files.testlog.files }}'
           - '{{ item.path.logdir.path }}'

1 Answers

if i understand your problem you want to copy all remote log files to another destination with a folder dated:

- name: Purge old spider logs
  become: true
  hosts: node1
  vars:
      date: "{{ lookup('pipe', 'date +%Y-%m-%d') }}"
  tasks:
    - name: create a remote directory
      file:
        path: /path/{{ date }}
        state: directory
        mode: '777'
      register: logdir

    - name: Find log files
      find:
        path: logs
        age: 3600
        patterns:
          - "name.log.*"
        recurse: yes
      register: testlogs

    - name: Copy (remote) files in to backup location (remote)
      copy:
        remote_src: yes
        src: "{{ item.path }}"
        dest: "{{logdir.path}}/"
      with_items:
        - '{{ testlogs.files }}'
Related