行动手册:
- 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
剧本执行输出:
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
我可以看到192.168.153.32
的date
任务输出也从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并行运行任务。使用油门将其限制为一次运行一个任务:
- 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
如果需要使用较旧的版本,可以使用ansible.cfg
中的forks
选项将fork的数量限制为1,也可以将-f 1
添加到ansible-playbook
命令中
另一个选项是将serial: 1
添加到您的游戏中(而不是任务!(,它将逐个运行每个主机
这两个选项都会增加游戏运行所需的时间
查看文档。