ansible - 日志文件中缺少任务输出
问题描述
剧本:
- 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
我可以看到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 并行运行任务。使用油门将其限制为一次运行一项任务:
- 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
选项 inansible.cfg
或添加-f 1
到ansible-playbook
命令中将分叉数限制为 1。
另一种选择是添加serial: 1
到您的游戏中(不是任务!),它将逐个运行每个主机。
这两个选项都会增加您的游戏需要运行的时间。
看看文档。
推荐阅读
- pandas - 如果满足特定条件,如何更新数据框列下的所有值?
- excel - Excel VBA 用户窗体中的 .width 属性没有影响
- excel - 同一宏在一个 Excel 电子表格中工作,但在另一个电子表格中不工作(曾经工作)
- typo3 - tx_seo:将站点地图划分为多个
- python - Django Admin更改列表隐藏用户的选择选项之一
- mapbox - Mapbox:如何使用 MGL_MATCH 表达式设置图层的填充颜色
- javascript - 使用 Cypress 连接到 SQL DB
- apache-spark - 无法使用 spark-atlas-connector 设置 spark 应用程序
- c++ - 在结构之间复制 char 数据
- python - 即使我能够安装其他库,安装 pyinstaller 时出现 SSL 错误