首页 > 解决方案 > 注册结果的 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"
        ]
    }
}

**我想要实现的是,只为每个结果项打印更改的变量的输出。例子: **

{
    "msg": {
        "true"
    }
}

我不明白错误/问题。对我来说,我的案例看起来与https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#using-register-with-a-loop的 ansible 文档中描述的完全一样

我还查看了一些类似的 stackoverflow 帖子,这些帖子也有结果问题,但没有一个能够解决我的问题。

编辑:我正在运行 Ansible 2.6.1

标签: ansible

解决方案


推荐阅读