簡體   English   中英

無法從 Ansible 劇本中收集 JSON id

[英]Unable to gather JSON id from an Ansible playbook

我在嘗試使用劇本獲取 Ansible 塔中的一個組的 ID 時遇到問題,並且在劇本中我使用的是 tower-cli 命令。

問題是當我得到這個 tower-cli 命令的 output 時:

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

我在一個變量group_id中注冊它,然后我嘗試從 output 獲取 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') }}"

output是這樣的`:

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

這是因為group_id.stdout是代表 JSON 的字符串,而不是 JSON。
您必須通過from_json過濾器傳遞它。

此外,對於這樣一個簡單的要求,您不需要 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM