在ansible中,如何打印任务中失败的每个项目的标准?

在ansible中,如何打印任务中失败的每个项目的标准?,ansible,Ansible,我有一个由用户输入提供的服务器阵列,我想测试列表中的每台服务器,如果有任何失败,我想知道哪些服务器失败以及为什么失败(stderr)。请注意,服务器的数量将不一致,因此解决方案需要根据发送的服务器数量动态调整 命令 ansible-playbook test.yml -e '{"chrony_servers":["0.centos.pool.ntp.org","10.centos.pool.ntp.org"]}' Test.yml - name: Services Playbook hos

我有一个由用户输入提供的服务器阵列,我想测试列表中的每台服务器,如果有任何失败,我想知道哪些服务器失败以及为什么失败(stderr)。请注意,服务器的数量将不一致,因此解决方案需要根据发送的服务器数量动态调整

命令

ansible-playbook test.yml -e '{"chrony_servers":["0.centos.pool.ntp.org","10.centos.pool.ntp.org"]}'
Test.yml

- name: Services Playbook
  hosts: localhost
  tasks:

  - name: Install nmap-ncat
    yum: name=nc state=present

  - name: Validate chrony_servers
    command: nc -vzu {{ item }} 123
    with_items:
      - "{{ chrony_servers }}"
    ignore_errors: yes
    register: chrony_server_results

  - debug: var=chrony_server_results

  - fail:
      msg: "{{ chrony_server_results.results.stderr }}"
    when: chrony_server_results.failed is defined and chrony_server_results.failed == true
这是来自此示例的输出

TASK [debug] ********************************************************************************************************************************************************************
ok: [localhost] => {
    "chrony_server_results": {
        "changed": true,
        "failed": true,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": true,
                "_ansible_item_label": "0.centos.pool.ntp.org",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": [
                    "nc",
                    "-vzu",
                    "0.centos.pool.ntp.org",
                    "123"
                ],
                "delta": "0:00:02.217171",
                "end": "2019-05-16 18:58:54.070600",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "nc -vzu 0.centos.pool.ntp.org 123",
                        "_uses_shell": false,
                        "argv": null,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "0.centos.pool.ntp.org",
                "rc": 0,
                "start": "2019-05-16 18:58:51.853429",
                "stderr": "Ncat: Version 7.50 ( https://nmap.org/ncat )\nNcat: Connected to 208.75.88.4:123.\nNcat: UDP packet sent successfully\nNcat: 1 bytes sent, 0 bytes received in 2.21 seconds.",
                "stderr_lines": [
                    "Ncat: Version 7.50 ( https://nmap.org/ncat )",
                    "Ncat: Connected to 208.75.88.4:123.",
                    "Ncat: UDP packet sent successfully",
                    "Ncat: 1 bytes sent, 0 bytes received in 2.21 seconds."
                ],
                "stdout": "",
                "stdout_lines": []
            },
            {
                "_ansible_item_label": "10.centos.pool.ntp.org",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": [
                    "nc",
                    "-vzu",
                    "10.centos.pool.ntp.org",
                    "123"
                ],
                "delta": "0:00:00.312970",
                "end": "2019-05-16 18:58:54.510976",
                "failed": true,
                "invocation": {
                    "module_args": {
                        "_raw_params": "nc -vzu 10.centos.pool.ntp.org 123",
                        "_uses_shell": false,
                        "argv": null,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "10.centos.pool.ntp.org",
                "msg": "non-zero return code",
                "rc": 2,
                "start": "2019-05-16 18:58:54.198006",
                "stderr": "Ncat: Version 7.50 ( https://nmap.org/ncat )\nNcat: Could not resolve hostname \"10.centos.pool.ntp.org\": Name or service not known. QUITTING.",
                "stderr_lines": [
                    "Ncat: Version 7.50 ( https://nmap.org/ncat )",
                    "Ncat: Could not resolve hostname \"10.centos.pool.ntp.org\": Name or service not known. QUITTING."
                ],
                "stdout": "",
                "stdout_lines": []
            }
        ]
    }
}

TASK [fail] *********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'stderr'\n\nThe error appears to have been in '/home/vera/test.yml': line 14, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  - debug: var=chrony_server_results\n  - fail:\n    ^ here\n"}
    to retry, use: --limit @/home/vera/test.retry

PLAY RECAP **********************************************************************************************************************************************************************
预期的结果应该是

"stderr": "Ncat: Version 7.50 ( https://nmap.org/ncat )\nNcat: Could not resolve hostname \"10.centos.pool.ntp.org\": Name or service not known. QUITTING."

您的
chrony\u服务器\u结果的结构是一个字典,其结构与您的调试任务假设的结构稍有不同。
它在顶层有一些键,这些键应用于您正在调用的任务,这些任务包含整个项目,如
changed
failed
。 单个调用的“子结果”(包含相应的
stderr
rc
属性)存储在一个具有键
results
的数组中
这意味着
chrony\u server\u results.results.stderr
是错误的,错误告诉您该数组没有
stderr
属性,而是有多个属性用于单个条目,您需要对其进行迭代。 您可以检查
rc
,以了解各个调用是否实际失败(因为netcat显然在所有情况下都会写入stderr)

以下解决方案首先构建一个数组,该数组仅包含
rc
大于零的结果,然后仅显示这些结果的调试消息:

 - set_fact:
     failed_results: "{{ chrony_server_results.results | selectattr('rc', '>', 0) | list }}"

 - name: Show the stderrs of failed_results
   debug:
     msg: "{{ item.stderr }}"
   with_items: "{{ failed_results }}"

  - fail:
      msg: "See debug output above"
    when: failed_results | length > 0

第一个表达式中使用的过滤器是jinja2的一部分,并记录在这里:

我明白你在这里说的。但是,在本例中,结果仍然无法打印。使用键attr“failed”并过滤“failed”的结果可能更容易:“true”我甚至没有注意到结果也有一个
failed
属性。我现在还不能尝试。错误消息是什么?可以使用
selectattr('failed'、'equalto',false)
,也可以使用
|list
?addedfatal:[localhost]:failed!=>{“msg”:"该任务包含一个带有未定义变量的选项。错误是:“ansible.utils.unsafe\u proxy.AnsibleUnsafeText对象”没有属性“stderr”\n\n错误似乎出现在“/home/vera/test.yml”:第20行第5列,但可能\n出现在文件的其他位置,具体取决于语法问题。\n\n出现问题的行是:\n\n-名称:显示失败结果的标准\n^此处\n“}请查看。我还更新了我的帖子。