首页 > 解决方案 > 在 ansible play 中调试不使用_items

问题描述

需要一些帮助来调试寄存器任务中的特定值。

- debug:
    msg: "{{ item.stdout }}"
  with_items: path_result.results

这是行不通的

{
    "changed": false,
    "path_result": {
        "msg": "All items completed",
        "changed": true,
        "results": [
            {
                "_ansible_parsed": true,
                "stderr_lines": [],
                "_ansible_item_result": true,
                "end": "2019-04-10 14:55:18.726270",
                "_ansible_no_log": false,
                "_ansible_delegated_vars": {
                    "ansible_delegated_host": "**.***.***.***",
                    "ansible_host": "**.***.***.***"
                },
                "cmd": "cat /tmp/abc.conf | grep apple",
                "rc": 0,
                "stdout": "fruit=apple",
                "item": "**.***.***.***",
                "delta": "0:00:00.021499",
                "stderr": "",
                "changed": true,
                "invocation": {
                    "module_args": {
                        "creates": null,
                        "executable": null,
                        "_uses_shell": true,
                        "_raw_params": "cat /tmp/abc.conf | grep apple",
                        "removes": null,
                        "warn": true,
                        "chdir": null,
                        "stdin": null
                    }
                },
                "stdout_lines": [
                    "fruit=apple"
                ],
                "start": "2019-04-10 14:55:18.704771",
                "_ansible_ignore_errors": null,
                "failed": false
            }
        ]
    },
    "_ansible_verbose_always": true,
    "_ansible_no_log": false
}

我想调试这个特定的输出:

"stdout": "fruit=apple"

标签: ansible

解决方案


看起来你已经register编辑了一个游戏的结果:register: path_result。该变量也包含path_result在其中。这就是我认为你拥有的:

- command: cat /tmp/abc.conf | grep apple
  register: path_result

- debug:
    msg: "{{ item.stdout }}"
  with_items: "{{ path_result.results }}"

这是我认为你需要的:

- command: cat /tmp/abc.conf | grep apple
  register: path_result

- debug:
    msg: "{{ item.stdout }}"
  with_items: "{{ path_result.path_result.results }}"

注意 中添加的.path_result部分with_items


推荐阅读