Ansible 如何在URI模块中检索值以获得注册的循环输出?

Ansible 如何在URI模块中检索值以获得注册的循环输出?,ansible,ansible-tower,Ansible,Ansible Tower,我正在通过Ansible Tower API搜索作业模板&我想从存储在注册结果json条目中的返回的“name”和“id”字段创建一个键/值对字典(?) - name: Search for job templates uri: url: "{{ tower_url }}/api/v2/job_templates?search={{ item.name }}" method: GET user: admin password: "{{ tower_admin_

我正在通过Ansible Tower API搜索作业模板&我想从存储在注册结果json条目中的返回的“name”和“id”字段创建一个键/值对字典(?)

- name: Search for job templates
  uri:
    url: "{{ tower_url }}/api/v2/job_templates?search={{ item.name }}"
    method: GET
    user: admin
    password: "{{ tower_admin_password }}"
    force_basic_auth: yes
    validate_certs: no
  register: get_job_templates
  loop: "{{ job_template_search }}"
这将搜索作业模板并返回以下输出-我已删除了一组故障排除不需要的输出

"get_job_templates": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": {
                    "name": "example_template"
                },
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "allow": "GET, POST, HEAD, OPTIONS",
                "cache_control": "no-cache, no-store, must-revalidate",
                "changed": false,
                "connection": "close",
                "content_language": "en",
                "content_length": "3264",
                "content_type": "application/json",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "headers": {
                            "Authorization": "Basic "
                        },
                        "status_code": [
                            200
                        ],
                        "timeout": 30,
                        "unsafe_writes": null,
                        "url": "",
                        "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "url_username": "admin",
                        "use_proxy": true,
                        "user": "admin",
                        "validate_certs": false
                    }
                },
                "item": {
                    "name": "example_template"
                },
                "json": {
                    "count": 1,
                    "next": null,
                    "previous": null,
                    "results": [
                        {
                            "become_enabled": false,
                            "created": "2019-06-03T13:08:54.586346Z",
                            "credential": null,
                            "custom_virtualenv": null,
                            "description": "",
                            "diff_mode": false,
                            "extra_vars": "",
                            "force_handlers": false,
                            "forks": 0,
                            "host_config_key": "",
                            "id": 10,
                            "inventory": 88,
                            "name": "example_template",
                        }
                    ]
                },
            }
        ]
    }
}
我尝试使用几种不同的方法从结果中提取名称和ID,所有这些方法都与此类似

- set_fact:
    job_template_ids: "{{ job_template_ids | default({}) | combine( { item.name: item.id } ) }}"
  loop: "{{ get_job_templates.results.json }}"
除此之外,我还尝试使用此处提供的解决方案映射属性,但没有成功:

我相信我遗漏了Ansible如何与这样定义的注册变量一起工作的一些基本信息。令人沮丧的是,如果我只是简单地获取所有作业模板、用户、组织等,那么这段代码只需稍微更改循环项(GET_job_templates.json.results)即可生成name:id键/值对,但在搜索特定的模板列表(可能还有其他对象)时,注册变量的布局略有不同。干杯

编辑:在发布这篇文章之后,我尝试了一些其他的方法,可以用于单个工作模板搜索,但由于某些原因不能用于多个工作模板搜索

此代码返回预期的输出,然后可以在将来的代码中使用模板名称调用作业模板,模板名称将返回模板ID

- set_fact:
    job_template_ids: "{{ get_job_templates.results | map(attribute='json') | list | join }}"

- set_fact:
    job_template_ids: "{{ job_template_ids | default({}) | combine( { item.name: item.id } ) }}"
  loop: "{{ job_template_ids.results }}"
如果我在初始任务中添加要搜索的模板列表,它将停止工作,在上面代码的第二个set_fact上设置fact时返回以下错误

fatal: [localhost]: FAILED! => {"msg": "'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'results'"}
我想进步就是进步

下面的任务

- set_fact:
    job_template_ids: "{{ dict(
      get_job_templates.results|json_query('[].json.results[].[name,id]')) }}"
- debug:
    var: job_template_ids
给出:

"job_template_ids": {
    "example_template": 10
}