记录结果的可见循环

时间:2018-08-28 10:29:39

标签: ansible

我正在遍历一些python命令,并将其结果注册到名为 scriptstatus 的变量中。 scriptstatus的示例输出为:

{
"scriptstatus": {
    "msg": "All items completed",
    "changed": true,
    "results": [
        {
            "_ansible_parsed": true,
            "stderr_lines": [
                "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?"
            ],
            "_ansible_item_result": true,
            "end": "2018-08-28 12:02:26.992264",
            "_ansible_no_log": false,
            "stdout": "",
            "cmd": [
                "python3",
                "/home/pi/set_local_parameter.py",
                "--api",
                "openhab",
                "--parameter_num",
                "parameter_nums",
                "--parameter_val",
                "parameter_vals",
                "--word_size",
                "word_sizes",
                "--man_id",
                "010F",
                "--references",
                "0801:1001",
                "0801:2001",
                "0801:3001",
                "0801:4001",
                "8800:3001"
            ],
            "rc": 0,
            "start": "2018-08-28 12:02:26.252247",
            "failed": false,
            "delta": "0:00:00.740017",
            "item": [
                "parameter_nums",
                "parameter_vals",
                "word_sizes"
            ],
            "changed": true,
            "invocation": {
                "module_args": {
                    "creates": null,
                    "executable": null,
                    "_uses_shell": false,
                    "_raw_params": "python3 /home/pi/set_local_parameter.py --api openhab --parameter_num parameter_nums --parameter_val parameter_vals --word_size word_sizes --man_id 010F --references 0801:1001 0801:2001 0801:3001 0801:4001 8800:3001",
                    "removes": null,
                    "argv": null,
                    "warn": true,
                    "chdir": null,
                    "stdin": null
                }
            },
            "stdout_lines": [],
            "stderr": "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?",
            "_ansible_ignore_errors": null,
            "_ansible_item_label": [
                "parameter_nums",
                "parameter_vals",
                "word_sizes"
            ]
        }
    ]
},
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false
}

在此示例中,只有一个结果,我可以直接访问诸如 scriptstatus.results.0.changed 之类的变量。

但是我需要循环结果。我想做的是:

- name: Show possible warnings
  debug:
     var: "{{ item.changed }}"
  loop: "{{ scriptstatus.results }}"

但是它的输出是:

{
"changed": false,
"_ansible_no_log": false,
"_ansible_item_result": true,
"item": {
    "_ansible_parsed": true,
    "stderr_lines": [
        "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?"
    ],
    "_ansible_item_result": true,
    "end": "2018-08-28 12:02:26.992264",
    "_ansible_no_log": false,
    "stdout": "",
    "cmd": [
        "python3",
        "/home/pi/set_local_parameter.py",
        "--api",
        "openhab",
        "--parameter_num",
        "parameter_nums",
        "--parameter_val",
        "parameter_vals",
        "--word_size",
        "word_sizes",
        "--man_id",
        "010F",
        "--references",
        "0801:1001",
        "0801:2001",
        "0801:3001",
        "0801:4001",
        "8800:3001"
    ],
    "rc": 0,
    "start": "2018-08-28 12:02:26.252247",
    "failed": false,
    "delta": "0:00:00.740017",
    "item": [
        "parameter_nums",
        "parameter_vals",
        "word_sizes"
    ],
    "changed": true,
    "invocation": {
        "module_args": {
            "creates": null,
            "executable": null,
            "_uses_shell": false,
            "_raw_params": "python3 /home/pi/set_local_parameter.py --api openhab --parameter_num parameter_nums --parameter_val parameter_vals --word_size word_sizes --man_id 010F --references 0801:1001 0801:2001 0801:3001 0801:4001 8800:3001",
            "removes": null,
            "argv": null,
            "warn": true,
            "chdir": null,
            "stdin": null
        }
    },
    "stdout_lines": [],
    "stderr": "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?",
    "_ansible_ignore_errors": null,
    "_ansible_item_label": [
        "parameter_nums",
        "parameter_vals",
        "word_sizes"
    ]
},
"true": "VARIABLE IS NOT DEFINED!",
"_ansible_verbose_always": true,
"_ansible_ignore_errors": null,
"_ansible_item_label": {
    "_ansible_parsed": true,
    "stderr_lines": [
        "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?"
    ],
    "_ansible_item_result": true,
    "end": "2018-08-28 12:02:26.992264",
    "_ansible_no_log": false,
    "stdout": "",
    "failed": false,
    "cmd": [
        "python3",
        "/home/pi/set_local_parameter.py",
        "--api",
        "openhab",
        "--parameter_num",
        "parameter_nums",
        "--parameter_val",
        "parameter_vals",
        "--word_size",
        "word_sizes",
        "--man_id",
        "010F",
        "--references",
        "0801:1001",
        "0801:2001",
        "0801:3001",
        "0801:4001",
        "8800:3001"
    ],
    "rc": 0,
    "start": "2018-08-28 12:02:26.252247",
    "delta": "0:00:00.740017",
    "item": [
        "parameter_nums",
        "parameter_vals",
        "word_sizes"
    ],
    "changed": true,
    "invocation": {
        "module_args": {
            "warn": true,
            "executable": null,
            "_uses_shell": false,
            "_raw_params": "python3 /home/pi/set_local_parameter.py --api openhab --parameter_num parameter_nums --parameter_val parameter_vals --word_size word_sizes --man_id 010F --references 0801:1001 0801:2001 0801:3001 0801:4001 8800:3001",
            "removes": null,
            "argv": null,
            "creates": null,
            "chdir": null,
            "stdin": null
        }
    },
    "stdout_lines": [],
    "stderr": "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?",
    "_ansible_ignore_errors": null,
    "_ansible_item_label": [
        "parameter_nums",
        "parameter_vals",
        "word_sizes"
    ]
}
}

首先,我想知道为什么打印整个项目。但是后来我认识到了这一行

"true": "VARIABLE IS NOT DEFINED!"

使用时

- name: Show possible warnings
      debug:
         mgs: "{{ item.changed }}"
      loop: "{{ scriptstatus.results }}"

输出只有很小的变化,例如未定义变量!丢失。例如。输出为:

{
    "changed": false,
    "_ansible_no_log": false,
    "_ansible_item_result": true,
    "item": {
        "_ansible_parsed": true,
        "stderr_lines": [
            "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?"
        ],
        "_ansible_item_result": true,
        "end": "2018-08-29 08:17:16.423869",
        "_ansible_no_log": false,
        "stdout": "",
        "cmd": [
            "python3",
            "/home/pi/set_local_parameter.py",
            "--api",
            "openhab",
            "--parameter_num",
            "80",
            "--parameter_val",
            "0",
            "--word_size",
            "1",
            "--man_id",
            "010F",
            "--references",
            "0801:1001",
            "0801:2001",
            "0801:3001",
            "0801:4001",
            "8800:3001"
        ],
        "rc": 0,
        "start": "2018-08-29 08:17:15.647229",
        "failed": false,
        "delta": "0:00:00.776640",
        "item": [
            "80",
            "0",
            "1"
        ],
        "changed": true,
        "invocation": {
            "module_args": {
                "creates": null,
                "executable": null,
                "_uses_shell": false,
                "_raw_params": "python3 /home/pi/set_local_parameter.py --api openhab --parameter_num 80 --parameter_val 0 --word_size 1 --man_id 010F --references 0801:1001 0801:2001 0801:3001 0801:4001 8800:3001",
                "removes": null,
                "argv": null,
                "warn": true,
                "chdir": null,
                "stdin": null
            }
        },
        "stdout_lines": [],
        "stderr": "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?",
        "_ansible_ignore_errors": null,
        "_ansible_item_label": [
            "80",
            "0",
            "1"
        ]
    },
    "msg": true,
    "_ansible_verbose_always": true,
    "_ansible_ignore_errors": null,
    "_ansible_item_label": {
        "_ansible_parsed": true,
        "stderr_lines": [
            "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?"
        ],
        "_ansible_item_result": true,
        "end": "2018-08-29 08:17:16.423869",
        "_ansible_no_log": false,
        "stdout": "",
        "failed": false,
        "cmd": [
            "python3",
            "/home/pi/set_local_parameter.py",
            "--api",
            "openhab",
            "--parameter_num",
            "80",
            "--parameter_val",
            "0",
            "--word_size",
            "1",
            "--man_id",
            "010F",
            "--references",
            "0801:1001",
            "0801:2001",
            "0801:3001",
            "0801:4001",
            "8800:3001"
        ],
        "rc": 0,
        "start": "2018-08-29 08:17:15.647229",
        "delta": "0:00:00.776640",
        "item": [
            "80",
            "0",
            "1"
        ],
        "changed": true,
        "invocation": {
            "module_args": {
                "warn": true,
                "executable": null,
                "_uses_shell": false,
                "_raw_params": "python3 /home/pi/set_local_parameter.py --api openhab --parameter_num 80 --parameter_val 0 --word_size 1 --man_id 010F --references 0801:1001 0801:2001 0801:3001 0801:4001 8800:3001",
                "removes": null,
                "argv": null,
                "creates": null,
                "chdir": null,
                "stdin": null
            }
        },
        "stdout_lines": [],
        "stderr": "WARNING:root:No device with given manufacturer and type found! Hint: Do you used HEX-numbers?",
        "_ansible_ignore_errors": null,
        "_ansible_item_label": [
            "80",
            "0",
            "1"
        ]
    }
}

**我想实现的是,每个结果项仅输出变量 changed 的输出。示例:**

{
    "msg": {
        "true"
    }
}

我不了解错误/问题。对我来说,我的情况与https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#using-register-with-a-loop

的ansible文档中描述的情况完全一样

我还研究了一些类似的stackoverflow帖子,这些帖子也存在结果问题,但是没有一个能够解决我的问题。

编辑:我正在运行Ansible 2.6.1

0 个答案:

没有答案