0

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?

Tolsadus
  • 1,123
  • 11
  • 22
Vijay
  • 3
  • 1
  • 2
  • Welcome to Serverfault! You'd get an answer faster if you pay some time re-formatting your question to be more readable. – Esa Jokinen Mar 08 '18 at 07:57

1 Answers1

1

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 }}'
Henrik Pingel
  • 8,676
  • 2
  • 24
  • 38
  • I did those changes, Ansible is not fetching files.here is message from debug mode . `"file": "/tmp/\"{'stderr_lines': [], u'changed': True, u'end': u'2018-03-11 20:21:28.160102', u'stdout': u'/tmp/EM_Prereq_testbafffmqygx_root_Success_20180302065814.txt\\n/tmp/EM_Prereq_testbafffmqygx_root_Warning_20180311200247.txt\\n/tmp/EM_Prereq_test0180311201454.txt\n/tmp/EM_Prereq_testbafffmqygx_root_Warning_20180311201744.txt\n/tmp/EM_Prereq_testbafffmqygx_root_Warning_20180311202123.txt", "msg": "the remote file does not exist, not transferring, ignored" }` – Vijay Mar 11 '18 at 20:45
  • hm, please try: ` with_items: '{{ path_files.stdout_lines }}'` – Henrik Pingel Mar 15 '18 at 22:01