Last active
August 29, 2024 10:10
-
-
Save Pr1meSuspec7/339ed7f2fafbde69aa9ead76d0793729 to your computer and use it in GitHub Desktop.
Ansible loop output in one file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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