使用循环收集的项目的 Ansible 标准输出

使用循环收集的项目的 Ansible 标准输出

我试图根据文档打印已注册项目的标准输出文档。这是我的 yaml 文件:

- name: network-check
  tags: stats
  shell: "tail -n 3 {{ ansible_facts['nodename'] }}_{{ item }}.out | awk '/Mbits/ { sum += $7; n++} END { print sum / n }'"
  loop: "{{ groups['all'] }}"
  when: ansible_facts['nodename'] != item
  register: check

- name: network-check
  tags: stats
  shell: "echo {{ item.stdout }}"
  loop: "{{ check.results }}"

但我仍然收到如下“stdout atribute”错误:

TASK [pretests : network-stat] ***************************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [card-kub000]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to have been in '/home/holo/cardinality/source/sysop/ansible/roles/pretests/tasks/main.yaml': line 21, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: network-stat\n  ^ here\n"}

我究竟做错了什么?

以下是我的输出:

- debug: msg="{{ item.item }}"
  loop: "{{ check.results }}"

好像

ok: [card-kub000] => (item={'changed': True, 'end': '2019-01-31 00:36:50.479743', 'stdout': '128', 'cmd': "tail -n 3 card-kub000_card-kub004.out | awk '/Mbits/ { sum += $7; n++} END { print sum / n }'", 'rc': 0, 'start': '2019-01-31 00:36:50.470371', 'stderr': '', 'delta': '0:00:00.009372', 'invocation': {'module_args': {'creates': None, 'executable': None, '_uses_shell': True, '_raw_params': "tail -n 3 card-kub000_card-kub004.out | awk '/Mbits/ { sum += $7; n++} END { print sum / n }'", 'removes': None, 'argv': None, 'warn': True, 'chdir': None, 'stdin': None}}, '_ansible_parsed': True, 'stdout_lines': ['128'], 'stderr_lines': [], '_ansible_no_log': False, 'failed': False, 'item': 'card-kub004', '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': 'card-kub004'}) => {
    "msg": "card-kub004"

答案1

我终于找到问题所在了。我有一个条件“when”,它告诉 ansible 如果某些项目与 nodename 相同,则跳过它们,因为第一个项目没有 stdout 字段。我添加了条件来检查项目是否未被跳过,然后它开始工作。

- name: network-check
  tags: stats
  shell: "echo {{ item.stdout }}"
  loop: "{{ check.results }}"
  when: item.changed != False

相关内容