Я запрашиваю контроллер для пользователя по имени. Затем мне нужно извлечь идентификатор пользователя из ответа, чтобы я мог предпринять дополнительные действия над пользователем. Я получаю информацию, и она превращается в new_dict, но содержимое, которое я хочу, кажется ответным массивом в ответе. Как динамически получить значение для идентификатора?
Вот задачи -
- name: GET NPD user
uri:
url : "{{ APPD_NPD_CONTROLLER_URL }}/api/rbac/v1/users/name/{{ APPD_USERNAME }}"
method : GET
headers:
Authorization : "{{ APPD_AUTH_TOKEN }}"
return_content : yes
force_basic_auth : no
body_format : raw
body :
status_code:
- 200
- 201
- 202
register: output
- name: user to dict
set_fact:
new_dict: '{{ output|dict2items|rejectattr("value", "equalto", "")|list|items2dict }}'
- name: get user id value
debug:
msg: "{{ query( 'dict', item.value ) }}"
with_dict: "{{ new_dict }}"
#"{{ lookup( 'dict', item.value ) }}"
register: uid_out
Ожидаемый результат -
"msg": [
{
"key": "id",
"value": "6781"
}
]
Фактический вывод -
TASK [user to dict] ******************************************************************************************************
task path: /user_by_name.yml:28
ok: [localhost] => {
"ansible_facts": {
"new_dict": {
"age": "0",
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"connection": "close",
"content": "{\"id\": 6781,\"name\": \"user10\",\"displayName\": \"user10\",\"security_provider_type\": \"INTERNAL\"}",
"content_type": "application/vnd.appd.cntrl+json;v=1",
"cookies": {
"JSESSIONID": "0000000000000000000000000000"
},
"cookies_string": "JSESSIONID=0000000000000000000000000000",
"date": "Tue, 01 Oct 2019 19:50:51 GMT",
"elapsed": 0,
"failed": false,
"json": {
"displayName": "user10",
"id": 6781,
"name": "user10",
"security_provider_type": "INTERNAL"
},
"msg": "OK (unknown bytes)",
"redirected": false,
"server": "AppDynamics",
"set_cookie": "JSESSIONID=0000000000000000000000000000; Path=/controller; HttpOnly;Secure",
"status": 200,
"transfer_encoding": "chunked",
"url": "https://....appdynamics.com/controller/api/rbac/v1/users/name/user10",
"via": "1.1 varnish",
"warnings": [
"Platform darwin on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information."
],
"x_content_type_options": "Nosniff",
"x_frame_options": "SAMEORIGIN",
"x_varnish": "1486419193",
"x_xss_protection": "1; mode=block"
}
},
"changed": false
}
TASK [get user id value] *************************************************************************************************
task path: /user_by_name.yml:33
ok: [localhost] => (item={'key': 'cookies', 'value': {'JSESSIONID': '0000000000000000000000000000'}}) => {
"msg": [
{
"key": "JSESSIONID",
"value": "0000000000000000000000000000"
}
]
}
fatal: [localhost]: FAILED! => {
"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"
}