如何访问结果数组以在返回的对象中获取特定值

时间:2019-07-29 11:02:28

标签: ansible

在调试从命令模块注册的变量时,我无法访问ansible返回的结果数组中的某些值。

我正在使用with_items运行多个命令,并将结果注册到“ registeredVar”中。我试图访问像这样的结果registeredVar.results.stdout,但得到的是“未定义变量!”错误。

我也尝试在调试任务中循环遍历结果数组,但出现“对象”没有属性“ stdout”的错误

下面是我正在运行的任务

- name: check configuration
  shell: "{{ item }}"
  register: falcon_config
  with_items:
    - /opt/CrowdStrike/falconctl -g --aph
    - /opt/CrowdStrike/falconctl -g --cid
    - /opt/CrowdStrike/falconctl -g --app

下面是输出变量的调试任务

- debug:
    var: falcon_config['results'].stdout

alternativley:

- debug:
    var: '{{ item.stdout }}'
  with_items: falcon_config['results']

这是运行falcon_config['results']时结果数组的调试输出

    "falcon_config['results']": [
        {
            "_ansible_ignore_errors": null,
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": true,
            "cmd": "/opt/CrowdStrike/falconctl -g --aph",
            "delta": "0:00:00.006724",
            "end": "2019-07-29 10:36:05.481672",
            "failed": false,
            "invocation": {
                "module_args": {
                    "_raw_params": "/opt/CrowdStrike/falconctl -g --aph",
                    "_uses_shell": true,
                    "chdir": null,
                    "creates": null,
                    "executable": null,
                    "removes": null,
                    "stdin": null,
                    "warn": true
                }
            },
            "item": "/opt/CrowdStrike/falconctl -g --aph",
            "rc": 0,
            "start": "2019-07-29 10:36:05.474948",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "aph=crowdstrike.domain.",
            "stdout_lines": [
                "aph=crowdstrike.intra.absaafrica."
            ]
        },
        {
            "_ansible_ignore_errors": null,
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": true,
            "cmd": "/opt/CrowdStrike/falconctl -g --cid",
            "delta": "0:00:00.006716",
            "end": "2019-07-29 10:36:05.662173",
            "failed": false,
            "invocation": {
                "module_args": {
                    "_raw_params": "/opt/CrowdStrike/falconctl -g --cid",
                    "_uses_shell": true,
                    "chdir": null,
                    "creates": null,
                    "executable": null,
                    "removes": null,
                    "stdin": null,
                    "warn": true
                }
            },
            "item": "/opt/CrowdStrike/falconctl -g --cid",
            "rc": 0,
            "start": "2019-07-29 10:36:05.655457",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "cid=\"185d26e78791sksdasd9d1033sa4\".",
            "stdout_lines": [
                "cid=\"185d26e78791sksdasd9d1033sa4\"."
            ]
        },
        {
            "_ansible_ignore_errors": null,
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": true,
            "cmd": "/opt/CrowdStrike/falconctl -g --app",
            "delta": "0:00:00.006617",
            "end": "2019-07-29 10:36:05.840573",
            "failed": false,
            "invocation": {
                "module_args": {
                    "_raw_params": "/opt/CrowdStrike/falconctl -g --app",
                    "_uses_shell": true,
                    "chdir": null,
                    "creates": null,
                    "executable": null,
                    "removes": null,
                    "stdin": null,
                    "warn": true
                }
            },
            "item": "/opt/CrowdStrike/falconctl -g --app",
            "rc": 0,
            "start": "2019-07-29 10:36:05.833956",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "app=8080.",
            "stdout_lines": [
                "app=8080."
            ]
        }
    ]
}

我无法获取每个对象的stdout值。

2 个答案:

答案 0 :(得分:0)

您可以使用json_query过滤器:

    - debug:
        var: falcon_config | json_query('results[*].stdout')

这也可以作为循环的输入

答案 1 :(得分:0)

您遇到的问题是,当您register在循环上输出模块时,最终会得到一个列表。因此,在行var: falcon_config['results'].stdout上它失败了,因为falcon_config['results']是一个列表并且没有stdoud键。您必须在循环中访问此结果。

您包含的这段代码应该可以正常工作:

- debug:
    var: '{{ item.stdout }}'
  with_items: falcon_config['results']

我使用以下playbook测试了您的代码,并且确实如此:

---
- hosts: local
  connection: local
  gather_facts: no
  tasks:
    - name: Run all shell commands
      shell: '{{ item }}'
      register: falcon_config
      with_items:
        - 'echo Hello World'
        - 'echo Something'
        - 'echo lorem ipsum'

    - name: Debug the results
      debug:
        var: item["stdout"]
      with_items: '{{ falcon_config["results"] }}'

我希望对您有帮助