<p>As mentioned in <a href="https://stackoverflow.com/a/70804292/15962190">Ansible: <code>lineinfile</code> for several lines?</a></p>
<pre class="lang-yaml prettyprint-override"><code>- name: &quot;create block in own file per host&quot;
  blockinfile:
     path: &quot;/dir/logs/{{ inventory_hostname }}.txt&quot;
     create: true
     block: &quot;some commands&quot;
     ...

- name: &quot;add more line in block (even if already executed on another host)&quot;
  lineinfile:
     line: &quot;{{ item }}&quot;
     ...
  loop: &quot;{{ more_commands_1 | default([]) +
    more_commands_2 | default([]) }}&quot; 

- name: &quot;assemble all files in one&quot;
  assemble:
    src: &quot;/dir/logs/&quot;
    dest: &quot;/dir/logs/all_hosts.txt&quot;
    ...
</code></pre>
<p>I used those modules along with <code>check_mode: false</code> and <code>delegate_to: localhost</code>.</p>
<p>Those are handlers (in a role). I notify them in each module that execute commands on a device.</p>
<p>Would be glad to use a more clever solution if it exists.</p>

CC BY-SA 4.0
2022-01-28T08:37:40.697Z
2022-02-05T19:18:55.367Z
2022-02-05T19:18:55.367Z
tintin
<p>I'm trying to write a script to publish basic configurations for any new devices. Also to assert compliancy of basic configuration for devices in production.</p>
<p>Trying to gather all the lines executed, on each devices, coming from the different plays, that I call on a central one.</p>
<p>How to centralize the outputs, (the commands that ansible will execute) and possibly assemble it into one file?</p>
<p>Trying to gather the commands in check mode in order to confirm before execution.</p>

<ansible><networking><infrastructure-as-code>
Ansible network automation: log all commands from multiple plays
tintin
# Ansible network automation: log all commands from multiple plays

(Asked by: username_0 on 2022-01-28)

I'm trying to write a script to publish basic configurations for any new devices. Also to assert compliancy of basic configuration for devices in production.

Trying to gather all the lines executed, on each devices, coming from the different plays, that I call on a central one.

How to centralize the outputs, (the commands that ansible will execute) and possibly assemble it into one file?

Trying to gather the commands in check mode in order to confirm before execution.

**Tags:** ansible, networking, infrastructure-as-code


---

## 1 answers

### Answer by username_0 on 2022-01-28. Score: 0

As mentioned in Ansible: lineinfile for several lines?
- name: "create block in own file per host"
  blockinfile:
     path: "/dir/logs/{{ inventory_hostname }}.txt"
     create: true
     block: "some commands"
     ...

- name: "add more line in block (even if already executed on another host)"
  lineinfile:
     line: "{{ item }}"
     ...
  loop: "{{ more_commands_1 | default([]) +
    more_commands_2 | default([]) }}" 

- name: "assemble all files in one"
  assemble:
    src: "/dir/logs/"
    dest: "/dir/logs/all_hosts.txt"
    ...

I used those modules along with check_mode: false and delegate_to: localhost.
Those are handlers (in a role). I notify them in each module that execute commands on a device.
Would be glad to use a more clever solution if it exists.


---

