剧本:
- 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执行输出:
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
日志文件输出:
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
我可以看到
date
的 192.168.153.32
任务输出也从 lineinfile
输出写入日志文件。但它在日志文件中丢失了。
当我进行下一次迭代时,我可以看到不同的结果:
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
我最好的猜测是,它们正在覆盖彼此的文件,因为 ansible 并行运行任务。使用 throttle 将其限制为一次运行一项任务:
- 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
是在 ansible 2.9 中引入的
如果您需要使用旧版本,您可以使用
forks
中的 ansible.cfg
选项将分叉数量限制为 1,或者将 -f 1
添加到 ansible-playbook
命令中。serial: 1
添加到您的游戏中(不是任务!),这将依次运行每个主机。