Run a.sh on remote hosts and fetch all output files. output file with timestamp. I wrote below code and it is failing with error

ansibleyaml

I want to run a.sh on remote hosts and fetch all output files. output file with timestamp. You can see below my ansible tasks.

---
   - name: 'abcd'
     hosts: 'all'
     gather_facts: 'false'
     tasks:
       - name : 'Copy the script to /tmp/ and set permission'
         copy :
           src : 'a.sh'
           dest: '/tmp'
           mode: '0700'
       - name: 'Execute the script'
         shell: >
           /tmp/a.sh
         register: 'results'
       - name: 'Display output'
         debug:
           msg: '{{ results.stdout }}'
       - name: 'Remove script'
         file:
           path: '/tmp/a.sh'
           state: 'absent'
       - name: 'fetch'
         shell: "ls /tmp/test_Prereq_*"
         register: path_files
         fetch :
          src : '/tmp/"{{item}}"'
          dest : '/home/vj/testout'
         with_items: '{{ path_files.stdout }}'

ansible-playbook report_task.yml –limit

ERROR! conflicting action statements: shell, fetch

The error appears to have been in '/home/vicheruk/report_task.yml': line 24, column 8, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

     state: 'absent'
 - name: 'fetch'
   ^ here

Any ideas?

Best Answer

Your playbook is syntactically wrong. fetch is an module and needs to be called in its own task.

Also there is not too much sense in looping and writing in the same file. Probably you want to include {{ item }} in the dest parameter as well.

This should do the trick:

- name: 'register files'
  shell: "ls /tmp/test_Prereq_*"
  register: path_files
- name: fetch
  fetch:
    src: '/tmp/"{{ item }}"'
    dest: '/home/vj/testout-{{ item }}'
  with_items: '{{ path_files.stdout }}'