无法从 Ansible 剧本收集 JSON id

Unable to gather JSON id from an Ansible playbook

我在尝试使用剧本获取 Ansible 塔中一组的 ID 时遇到问题,而在剧本中我正在使用 tower-cli 命令。

问题是当我得到这个 tower-cli 命令的输出时:

tower-cli group list -i {{ inventory_id }} --name {{ get_specific_group_hosts }} -f json -a

我在一个变量 group_id 中注册了它,然后我尝试从输出中获取 id,它是一个 JSON.

{
    "stderr_lines": [
        "/var/lib/awx/venv/ansible_ibm/lib/python2.7/site-packages/OpenSSL/crypto.py:12: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.",
        "  from cryptography import x509"
    ],
    "changed": true,
    "end": "2022-01-28 15:54:34.901091",
    "_ansible_no_log": false,
    "_ansible_delegated_vars": {},
    "cmd": "tower-cli group list -i 1283 --name grp_infrastructure -f json -a",
    "stdout": "{\n  \"count\": 1, \n  \"next\": null, \n  \"previous\": null, \n  \"results\": [\n    {\n      \"id\": 6707, \n      \"type\": \"group\", \n      \"url\": \"/api/v2/groups/6707/\", \n      \"related\": {\n        \"created_by\": \"/api/v2/users/1338/\", \n        \"modified_by\": \"/api/v2/users/1338/\", \n        \"variable_data\": \"/api/v2/groups/6707/variable_data/\", \n        \"hosts\": \"/api/v2/groups/6707/hosts/\", \n        \"potential_children\": \"/api/v2/groups/6707/potential_children/\", \n        \"children\": \"/api/v2/groups/6707/children/\", \n        \"all_hosts\": \"/api/v2/groups/6707/all_hosts/\", \n        \"job_events\": \"/api/v2/groups/6707/job_events/\", \n        \"job_host_summaries\": \"/api/v2/groups/6707/job_host_summaries/\", \n        \"activity_stream\": \"/api/v2/groups/6707/activity_stream/\", \n        \"inventory_sources\": \"/api/v2/groups/6707/inventory_sources/\", \n        \"ad_hoc_commands\": \"/api/v2/groups/6707/ad_hoc_commands/\", \n        \"inventory\": \"/api/v2/inventories/1283/\"\n      }, \n      \"summary_fields\": {\n        \"inventory\": {\n          \"id\": 1283, \n          \"name\": \"inventory\", \n          \"description\": \"inventory\", \n          \"has_active_failures\": true, \n          \"total_hosts\": 562, \n          \"hosts_with_active_failures\": 68, \n          \"total_groups\": 30, \n          \"has_inventory_sources\": false, \n          \"total_inventory_sources\": 0, \n          \"inventory_sources_with_failures\": 0, \n          \"organization_id\": 142, \n          \"kind\": \"\"\n        }, \n        \"created_by\": {\n          \"id\": 1338, \n          \"username\": \"\", \n          \"first_name\": \"\", \n          \"last_name\": \"\"\n        }, \n        \"modified_by\": {\n          \"id\": 1338, \n          \"username\": \"\", \n          \"first_name\": \"\", \n          \"last_name\": \"\"\n        }, \n        \"user_capabilities\": {\n          \"edit\": true, \n          \"delete\": true, \n          \"copy\": true\n        }\n      }, \n      \"created\": \"2020-09-17T10:47:16.910421Z\", \n      \"modified\": \"2020-09-17T10:47:16.910437Z\", \n      \"name\": \"grp_infrastructure\", \n      \"description\": \"\", \n      \"inventory\": 1283, \n      \"variables\": \"\"\n    }\n  ]\n}",
    "start": "2022-01-28 15:54:34.289617",
    "delta": "0:00:00.611474",
    "stderr": "/var/lib/awx/venv/ansible_ibm/lib/python2.7/site-packages/OpenSSL/crypto.py:12: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.\n  from cryptography import x509",
    "rc": 0,
    "invocation": {
        "module_args": {
            "warn": true,
            "executable": null,
            "_uses_shell": true,
            "strip_empty_ends": true,
            "_raw_params": "tower-cli group list -i 1283 --name grp_infrastructure -f json -a",
            "removes": null,
            "argv": null,
            "creates": null,
            "chdir": null,
            "stdin_add_newline": true,
            "stdin": null
        }
    },
    "stdout_lines": [
        "{",
        "  \"count\": 1, ",
        "  \"next\": null, ",
        "  \"previous\": null, ",
        "  \"results\": [",
        "    {",
        "      \"id\": 6707, ",
        "      \"type\": \"group\", ",
        "      \"url\": \"/api/v2/groups/6707/\", ",
        "      \"related\": {",
        "        \"created_by\": \"/api/v2/users/1338/\", ",
        "        \"modified_by\": \"/api/v2/users/1338/\", ",
        "        \"variable_data\": \"/api/v2/groups/6707/variable_data/\", ",
        "        \"hosts\": \"/api/v2/groups/6707/hosts/\", ",
        "        \"potential_children\": \"/api/v2/groups/6707/potential_children/\", ",
        "        \"children\": \"/api/v2/groups/6707/children/\", ",
        "        \"all_hosts\": \"/api/v2/groups/6707/all_hosts/\", ",
        "        \"job_events\": \"/api/v2/groups/6707/job_events/\", ",
        "        \"job_host_summaries\": \"/api/v2/groups/6707/job_host_summaries/\", ",
        "        \"activity_stream\": \"/api/v2/groups/6707/activity_stream/\", ",
        "        \"inventory_sources\": \"/api/v2/groups/6707/inventory_sources/\", ",
        "        \"ad_hoc_commands\": \"/api/v2/groups/6707/ad_hoc_commands/\", ",
        "        \"inventory\": \"/api/v2/inventories/1283/\"",
        "      }, ",
        "      \"summary_fields\": {",
        "        \"inventory\": {",
        "          \"id\": 1283, ",
        "          \"name\": \"inventory\", ",
        "          \"description\": \"inventory\", ",
        "          \"has_active_failures\": true, ",
        "          \"total_hosts\": 562, ",
        "          \"hosts_with_active_failures\": 68, ",
        "          \"total_groups\": 30, ",
        "          \"has_inventory_sources\": false, ",
        "          \"total_inventory_sources\": 0, ",
        "          \"inventory_sources_with_failures\": 0, ",
        "          \"organization_id\": 142, ",
        "          \"kind\": \"\"",
        "        }, ",
        "        \"created_by\": {",
        "          \"id\": 1338, ",
        "          \"username\": \"\", ",
        "          \"first_name\": \"\", ",
        "          \"last_name\": \"\"",
        "        }, ",
        "        \"modified_by\": {",
        "          \"id\": 1338, ",
        "          \"username\": \"\", ",
        "          \"first_name\": \"\", ",
        "          \"last_name\": \"\"",
        "        }, ",
        "        \"user_capabilities\": {",
        "          \"edit\": true, ",
        "          \"delete\": true, ",
        "          \"copy\": true",
        "        }",
        "      }, ",
        "      \"created\": \"2020-09-17T10:47:16.910421Z\", ",
        "      \"modified\": \"2020-09-17T10:47:16.910437Z\", ",
        "      \"name\": \"grp_infrastructure\", ",
        "      \"description\": \"\", ",
        "      \"inventory\": 1283, ",
        "      \"variables\": \"\"",
        "    }",
        "  ]",
        "}"
    ]
}

所以当我使用不同类型的 JSON 字符串和方法时,没有任何效果,有时我会得到一个错误,有时我得到一个空字符串,就像我尝试这个时一样:

- name: "get the json list"
  debug:
    msg: "{{ group_id | json_query('stdout.results.id') }}"

输出是这样的`:

TASK [get the json list] *****************************************************************
ok: [localhost] => {
    "msg": ""
}

这是因为 group_id.stdout 是表示 JSON 的字符串,而不是 JSON.
您必须通过 from_json filter.

传递它

此外,对于这样一个简单的要求,您不需要 JMESPath 表达式和 json_query,您可以使用 map

所以,你的任务最终应该是

- debug:
    var: (group_id.stdout | from_json).results | map(attribute='id')

鉴于剧本:

- hosts: localhost
  gather_facts: no

  tasks:
    - debug:
        var: (group_id.stdout | from_json).results | map(attribute='id')
      vars:
        group_id:
          stdout: "{\n  \"count\": 1, \n  \"next\": null, \n  \"previous\": null, \n  \"results\": [\n    {\n      \"id\": 6707, \n      \"type\": \"group\", \n      \"url\": \"/api/v2/groups/6707/\", \n      \"related\": {\n        \"created_by\": \"/api/v2/users/1338/\", \n        \"modified_by\": \"/api/v2/users/1338/\", \n        \"variable_data\": \"/api/v2/groups/6707/variable_data/\", \n        \"hosts\": \"/api/v2/groups/6707/hosts/\", \n        \"potential_children\": \"/api/v2/groups/6707/potential_children/\", \n        \"children\": \"/api/v2/groups/6707/children/\", \n        \"all_hosts\": \"/api/v2/groups/6707/all_hosts/\", \n        \"job_events\": \"/api/v2/groups/6707/job_events/\", \n        \"job_host_summaries\": \"/api/v2/groups/6707/job_host_summaries/\", \n        \"activity_stream\": \"/api/v2/groups/6707/activity_stream/\", \n        \"inventory_sources\": \"/api/v2/groups/6707/inventory_sources/\", \n        \"ad_hoc_commands\": \"/api/v2/groups/6707/ad_hoc_commands/\", \n        \"inventory\": \"/api/v2/inventories/1283/\"\n      }, \n      \"summary_fields\": {\n        \"inventory\": {\n          \"id\": 1283, \n          \"name\": \"inventory\", \n          \"description\": \"inventory\", \n          \"has_active_failures\": true, \n          \"total_hosts\": 562, \n          \"hosts_with_active_failures\": 68, \n          \"total_groups\": 30, \n          \"has_inventory_sources\": false, \n          \"total_inventory_sources\": 0, \n          \"inventory_sources_with_failures\": 0, \n          \"organization_id\": 142, \n          \"kind\": \"\"\n        }, \n        \"created_by\": {\n          \"id\": 1338, \n          \"username\": \"\", \n          \"first_name\": \"\", \n          \"last_name\": \"\"\n        }, \n        \"modified_by\": {\n          \"id\": 1338, \n          \"username\": \"\", \n          \"first_name\": \"\", \n          \"last_name\": \"\"\n        }, \n        \"user_capabilities\": {\n          \"edit\": true, \n          \"delete\": true, \n          \"copy\": true\n        }\n      }, \n      \"created\": \"2020-09-17T10:47:16.910421Z\", \n      \"modified\": \"2020-09-17T10:47:16.910437Z\", \n      \"name\": \"grp_infrastructure\", \n      \"description\": \"\", \n      \"inventory\": 1283, \n      \"variables\": \"\"\n    }\n  ]\n}"

这产生:

TASK [debug] *************************************************************
ok: [localhost] => 
  (group_id.stdout | from_json).results | map(attribute='id'):
  - 6707