playbook:
- command: date
register: date_output
- command: hostname
register: hostname_output
- lineinfile:
line: "{{inventory_hostname}} {{ item.cmd }}\n======================\n{{ item.stdout }}"
path: /home/ubuntu/list.log
create: yes
with_items:
- "{{ date_output }}"
- "{{ hostname_output }}"
delegate_to: localhost
playbook execution output:
PLAY [all] *******************************************************************************************************************************************************************************************************
TASK [command] ***************************************************************************************************************************************************************************************************
changed: [192.168.153.32]
changed: [192.168.153.31]
TASK [command] ***************************************************************************************************************************************************************************************************
changed: [192.168.153.31]
changed: [192.168.153.32]
TASK [lineinfile] ************************************************************************************************************************************************************************************************
changed: [192.168.153.31 -> localhost] => (item={'delta': '0:00:00.001888', 'cmd': ['date'], 'rc': 0, 'stdout': 'Tue Jul 6 14:12:45 EDT 2021', 'end': '2021-07-06 14:12:45.780249', 'start': '2021-07-06 14:12:45.778361', 'changed': True, 'stderr': '', 'stdout_lines': ['Tue Jul 6 14:12:45 EDT 2021'], 'stderr_lines': [], 'ansible_facts': {'discovered_interpreter_python': '/usr/bin/python3'}, 'failed': False})
changed: [192.168.153.32 -> localhost] => (item={'changed': True, 'rc': 0, 'start': '2021-07-06 14:12:45.842399', 'cmd': ['date'], 'stderr': '', 'end': '2021-07-06 14:12:45.844352', 'stdout': 'Tue Jul 6 14:12:45 EDT 2021', 'delta': '0:00:00.001953', 'stdout_lines': ['Tue Jul 6 14:12:45 EDT 2021'], 'stderr_lines': [], 'ansible_facts': {'discovered_interpreter_python': '/usr/bin/python3'}, 'failed': False})
changed: [192.168.153.31 -> localhost] => (item={'changed': True, 'stderr': '', 'rc': 0, 'delta': '0:00:00.001809', 'cmd': ['hostname'], 'end': '2021-07-06 14:12:48.427204', 'stdout': 'ubuntu', 'start': '2021-07-06 14:12:48.425395', 'stdout_lines': ['ubuntu'], 'stderr_lines': [], 'failed': False})
changed: [192.168.153.32 -> localhost] => (item={'stderr': '', 'start': '2021-07-06 14:12:48.516075', 'delta': '0:00:00.001940', 'cmd': ['hostname'], 'rc': 0, 'changed': True, 'stdout': 'ubuntu1', 'end': '2021-07-06 14:12:48.518015', 'stdout_lines': ['ubuntu1'], 'stderr_lines': [], 'failed': False})
PLAY RECAP *******************************************************************************************************************************************************************************************************
192.168.153.31 : ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.153.32 : ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Output of log file:
cat /home/ubuntu/list.log
192.168.153.31 ['date']
======================
Tue Jul 6 14:12:45 EDT 2021
192.168.153.31 ['hostname']
======================
ubuntu
192.168.153.32 ['hostname']
======================
ubuntu1
I can see the date
task output for 192.168.153.32
also writing to log file from lineinfile
output. but it's missing in the log file.
when I do next iteration I can see different result:
cat /home/ubuntu/list.log
192.168.153.32 ['date']
======================
Tue Jul 6 15:52:32 EDT 2021
192.168.153.32 ['hostname']
======================
ubuntu1
192.168.153.31 ['hostname']
======================
ubuntu
My best guess is, that they are overwriting each others files, as ansible runs tasks in parallel. Use throttle to limit it to running one task at a time:
- lineinfile:
line: "{{ inventory_hostname }} {{ item.cmd }}\n======================\n{{ item.stdout }}"
path: /home/ubuntu/list.log
create: yes
with_items:
- "{{ date_output }}"
- "{{ hostname_output }}"
delegate_to: localhost
throttle: 1
throttle
was introduced with ansible 2.9
If you need to use an older version, you can either limit the number of forks to 1 using the forks
option in ansible.cfg
or adding -f 1
to your ansible-playbook
command.
Another option is to add serial: 1
to your play (not task!) which will run each host after another.
Both of those options will increase the time your play needs to run.
Take a look at the documentation.