Saving output of commands passed through 'with_items' into a single file

Hi all,

I am trying to run few commands and save output of those commands into a single file for checking later.

So far, I have come up with the following playbook:

`

`

Since you are saving to a file, maybe just save the output directly.

`

  • hosts: localhost
    tasks:
  • name: Clear the file
    shell: “echo > info.txt”
  • name: Capture some information about a host
    shell: “echo shell: ‘{{ item }}’ >> info.txt 2>&1 && {{ item }} >> info.txt 2>&1”
    with_items:
  • echo date +"%Y-%m-%d %T" info
  • ls wrong
  • echo date +"%Y-%m-%d %T" info
  • echo test
    ignore_errors: yes

`

`

shell: echo date +"%Y-%m-%d %T" info
2018-07-20 21:48:45 info
shell: ls wrong
ls: cannot access ‘wrong’: No such file or directory
shell: echo date +"%Y-%m-%d %T" info
2018-07-20 21:48:45 info
shell: echo test
test

`

Alternatively you can actually do an inline Jina loop. e.g.

  • name: Save the output
    local_action:
    module: copy
    content: |
    {% for item in result.results %}
    {{ item.stdout }}
    {% endfor %}
    dest: /root/info.txt

Hi Marcin,

Your solution works perfectly. I modified it a bit to suit my needs. So thank you very much.

Hi Ed,

I tried your solution but it is giving me a syntax error that I am not sure how to approach:

From your mail:

        content: | …
        dest=/root/info.txt

And from the mail you quoted:

content: | …
dest: /root/info.txt

-- Abhijit

Oh wow,

/me hides face.

Thank you :slight_smile: