首页 > 解决方案 > 日志文件中缺少任务输出

问题描述

剧本:

- 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

解决方案


我最好的猜测是,他们正在覆盖彼此的文件,因为 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 1ansible-playbook命令中将分叉数限制为 1。
另一种选择是添加serial: 1到您的游戏中(不是任务!),它将逐个运行每个主机。
这两个选项都会增加您的游戏需要运行的时间。
看看文档


推荐阅读