【问题标题】:How do i access the results array to get a specific value in the objects returned如何访问结果数组以获取返回的对象中的特定值
【发布时间】:2019-07-29 11:02:28
【问题描述】:

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

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

我也尝试在调试任务中循环遍历结果数组,但我得到一个“object' has no attribute '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

另一种选择:

- 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."
            ]
        }
    ]
}

我无法获取每个对象的标准输出值。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您可以使用json_query 过滤器:

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

    这也可以作为循环的输入

    【讨论】:

    • 在循环运行时,最好在调试中定义键,例如- debug: msg: "{{ item.stdout }}" loop: "{{ falcon_config | json_query('results[*]') }}"
    • 致命:[本地主机]:失败! => {"msg": "您需要在运行 json_query 过滤器之前安装 \"jmespath\""}
    【解决方案2】:

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

    你包含的这个 sn-p 应该可以工作:

    - 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"] }}'
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 2022-09-29
      • 2016-11-05
      • 2019-07-14
      • 2017-07-09
      • 2021-03-05
      • 2019-10-13
      • 2020-11-03
      • 2018-11-05
      相关资源
      最近更新 更多