从输出/结果数组中选择特定键值

问题描述 投票:0回答:1

我想从输出/结果数组中选择一个特定的键,但我遇到了困难,我已经检查了这个post,但我无法将其适应我的剧本:

---
- hosts: '{{ site }}'
  gather_facts: false
  tasks:
  - name: Get Checkmk version
    community.windows.win_file_version:
     path: 'C:\Program Files (x86)\checkmk\service\check_mk_agent.exe'
    register: file_version
  - debug:
     var: file_version

我想选择“product_version”的输出,例如“2.2.0p7”。

TASK [debug] *******************************************************************
ok: [10.10.10.10] => {
    "file_version": {
        "changed": false,
        "failed": false,
        "win_file_version": {
            "file_build_part": "0",
            "file_major_part": "2",
            "file_minor_part": "1",
            "file_private_part": "0",
            "file_version": "2.1.0.0",
            "path": "C:\\Program Files (x86)\\checkmk\\service\\check_mk_agent.exe",
            "product_version": "2.2.0p7"
        }
    }
}
ansible ansible-2.x
1个回答
0
投票

一个最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    result: {
        "changed": false,
        "failed": false,
        "win_file_version": {
            "file_build_part": "0",
            "file_major_part": "2",
            "file_minor_part": "1",
            "file_private_part": "0",
            "file_version": "2.1.0.0",
            "path": "C:\\Program Files (x86)\\checkmk\\service\\check_mk_agent.exe",
            "product_version": "2.2.0p7"
        }
    }

  tasks:

    - debug:
        var: result.win_file_version

    - name: Product version is
      debug:
        msg: "{{ result.win_file_version.product_version }}"

将产生

的输出
TASK [debug] *******************************************************
ok: [localhost] =>
  result.win_file_version:
    file_build_part: '0'
    file_major_part: '2'
    file_minor_part: '1'
    file_private_part: '0'
    file_version: 2.1.0.0
    path: C:\Program Files (x86)\checkmk\service\check_mk_agent.exe
    product_version: 2.2.0p7

TASK [Product version is] *******************************************************************
ok: [localhost] =>
  msg: 2.2.0p7

文档

© www.soinside.com 2019 - 2024. All rights reserved.