Skip to content

Instantly share code, notes, and snippets.

@Pr1meSuspec7
Last active August 29, 2024 10:10
Show Gist options
  • Save Pr1meSuspec7/339ed7f2fafbde69aa9ead76d0793729 to your computer and use it in GitHub Desktop.
Save Pr1meSuspec7/339ed7f2fafbde69aa9ead76d0793729 to your computer and use it in GitHub Desktop.
Ansible loop output in one file
# Save output under play
- name: Save output
ansible.builtin.copy:
content: |-
{% for host in ansible_play_hosts_all %}
{{ '### ' }}{{ host }}{{ ' ###' }}
{{ hostvars[host]['result']['stdout_lines'] | to_nice_json }}
{% endfor %}
dest: output.log
mode: "0644"
# Save output at the end of playbook
# Use ansible.builtin.set_fact to append lines
- name: Set facts
ansible.builtin.set_fact:
play_output_1: |-
{% for host in ansible_play_hosts_all %}
{{ '### ' }}{{ host }}{{ ' ###' }}
{% for i in hostvars[host]['cli_result']['results'] %}
{{ i['commands'] | to_nice_json }}
{% endfor %}
{% endfor %}
- name: Append facts to one file
ansible.builtin.lineinfile:
line: "{{ play_output_1 }}"
dest: "output/output_{{ date }}.log"
mode: '0644'
create: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment