Reputation: 2413
I'm a noob in ansible and jinja template, and I need to get a value from a uri json result, but I don't know how to do this.
I have the uri request below:
- name: Busca o id do usuario na Gitlab API
uri:
url: "{{ gitlab_host_server }}/api/v4/users?access_token={{ gitlab_access_token }}&username={{ item.key }}"
method: GET
status_code: 200,400
headers:
"Content-Type": "application/json"
"Accept": "application/json"
"Cache-Control": "no-cache"
"content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
body_format: json
return_content: yes
register: gitlab_api_usuario
when: item['value'].hostname == "{{ ansible_hostname }}"
with_dict: "{{ hostname_desenvolvedores }}"
So I get the register from gitlab_api_usuario
variable, and when I use the debug below I have the following result.
- debug:
var: "{{ item }}"
with_items: "gitlab_api_usuario.results | map(attribute='content') | list"
Result:
ok: [192.168.101.151] => (item=None) => {
"gitlab_api_usuario.results | map(attribute='content') | list": "[Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, u'[{\"id\":8,\"name\":\"Maycon Brito\",\"username\":\"maycon.brito\",\"state\":\"active\",\"avatar_url\":\"http://192.168.101.181:8083/uploads/-/system/user/avatar/8/956667.jpeg\",\"web_url\":\"http://192.168.101.181:8083/maycon.brito\",\"created_at\":\"2018-05-15T09:33:02.866-03:00\",\"bio\":null,\"location\":null,\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"\",\"website_url\":\"\",\"organization\":null,\"last_sign_in_at\":\"2018-05-15T16:39:53.045-03:00\",\"confirmed_at\":\"2018-05-15T09:33:02.584-03:00\",\"last_activity_on\":\"2018-05-21\",\"email\":\"[email protected]\",\"theme_id\":1,\"color_scheme_id\":1,\"projects_limit\":100000,\"current_sign_in_at\":\"2018-05-17T17:19:13.442-03:00\",\"identities\":[],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":false,\"external\":false,\"is_admin\":true}]', Undefined]"
}
So, I need to get the id
value from the unique successful request.
The pure Json returned from uri.
ok: [192.168.101.151] => (item=None) => {
"gitlab_api_usuario": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "breno.barbosa",
"value": {
"hostname": "informatica13"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "angeloalves",
"value": {
"hostname": "informatica20"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "flavia.martis",
"value": {
"hostname": "informatica12"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "edeilson",
"value": {
"hostname": "informatica18"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "silvandro",
"value": {
"hostname": "informatica15"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "leonardo.nardy",
"value": {
"hostname": "informatica16"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "abel",
"value": {
"hostname": "informatica3"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "flaviaferreira",
"value": {
"hostname": "informatica8"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "alessandro",
"value": {
"hostname": "informatica5"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "mayse",
"value": {
"hostname": "informatica1"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "bruno.ferreira",
"value": {
"hostname": "informatica26"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "natalia.ramos",
"value": {
"hostname": "informatica10"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "marina.ferreira",
"value": {
"hostname": "informatica17"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "fernando.assis",
"value": {
"hostname": "informatica7"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"cache_control": "max-age=0, private, must-revalidate",
"changed": false,
"connection": "close",
"content": "[{\"id\":8,\"name\":\"Maycon Brito\",\"username\":\"maycon.brito\",\"state\":\"active\",\"avatar_url\":\"http://192.168.101.181:8083/uploads/-/system/user/avatar/8/956667.jpeg\",\"web_url\":\"http://192.168.101.181:8083/maycon.brito\",\"created_at\":\"2018-05-15T09:33:02.866-03:00\",\"bio\":null,\"location\":null,\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"\",\"website_url\":\"\",\"organization\":null,\"last_sign_in_at\":\"2018-05-15T16:39:53.045-03:00\",\"confirmed_at\":\"2018-05-15T09:33:02.584-03:00\",\"last_activity_on\":\"2018-05-21\",\"email\":\"[email protected]\",\"theme_id\":1,\"color_scheme_id\":1,\"projects_limit\":100000,\"current_sign_in_at\":\"2018-05-17T17:19:13.442-03:00\",\"identities\":[],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":false,\"external\":false,\"is_admin\":true}]",
"content_length": "777",
"content_type": "application/json",
"cookies": {},
"date": "Tue, 22 May 2018 12:27:52 GMT",
"etag": "W/\"6964fe3bee522682cecd5546dca13c8b\"",
"failed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": null,
"body_format": "json",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {
"Accept": "application/json",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
"content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": true,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
"200",
"400"
],
"timeout": 30,
"unsafe_writes": null,
"url": "http://192.168.101.181:8083/api/v4/users?access_token=38f861948f551f8bd1c00147f5eb66550d0ccba4e723f559ed6bd9ef915be1f3&username=maycon.brito",
"url_password": null,
"url_username": null,
"use_proxy": true,
"validate_certs": true
}
},
"item": {
"key": "maycon.brito",
"value": {
"hostname": "informatica14"
}
},
"json": [
{
"avatar_url": "http://192.168.101.181:8083/uploads/-/system/user/avatar/8/956667.jpeg",
"bio": null,
"can_create_group": true,
"can_create_project": true,
"color_scheme_id": 1,
"confirmed_at": "2018-05-15T09:33:02.584-03:00",
"created_at": "2018-05-15T09:33:02.866-03:00",
"current_sign_in_at": "2018-05-17T17:19:13.442-03:00",
"email": "[email protected]",
"external": false,
"id": 8,
"identities": [],
"is_admin": true,
"last_activity_on": "2018-05-21",
"last_sign_in_at": "2018-05-15T16:39:53.045-03:00",
"linkedin": "",
"location": null,
"name": "Maycon Brito",
"organization": null,
"projects_limit": 100000,
"skype": "",
"state": "active",
"theme_id": 1,
"twitter": "",
"two_factor_enabled": false,
"username": "maycon.brito",
"web_url": "http://192.168.101.181:8083/maycon.brito",
"website_url": ""
}
],
"link": "<http://192.168.101.181:8083/api/v4/users?access_token=38f861948f551f8bd1c00147f5eb66550d0ccba4e723f559ed6bd9ef915be1f3&active=false&blocked=false&external=false&order_by=id&page=1&per_page=20&sort=desc&username=maycon.brito&with_custom_attributes=false>; rel=\"first\", <http://192.168.101.181:8083/api/v4/users?access_token=38f861948f551f8bd1c00147f5eb66550d0ccba4e723f559ed6bd9ef915be1f3&active=false&blocked=false&external=false&order_by=id&page=1&per_page=20&sort=desc&username=maycon.brito&with_custom_attributes=false>; rel=\"last\"",
"msg": "OK (777 bytes)",
"redirected": false,
"server": "nginx",
"status": 200,
"strict_transport_security": "max-age=31536000",
"url": "http://192.168.101.181:8083/api/v4/users?access_token=38f861948f551f8bd1c00147f5eb66550d0ccba4e723f559ed6bd9ef915be1f3&username=maycon.brito",
"vary": "Origin",
"x_content_type_options": "nosniff",
"x_frame_options": "SAMEORIGIN",
"x_next_page": "",
"x_page": "1",
"x_per_page": "20",
"x_prev_page": "",
"x_request_id": "179a39c6-9b19-44a5-ba73-e2ef881207be",
"x_runtime": "0.026570",
"x_total": "1",
"x_total_pages": "1"
},
{
"_ansible_ignore_errors": null,
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"key": "lucas.lima",
"value": {
"hostname": "informatica19"
}
},
"skip_reason": "Conditional result was False",
"skipped": true
}
]
}
}
Upvotes: 3
Views: 8113
Reputation: 6685
I'm not sure why, but your filtering doesn't lead to a list variable:
- name: print results
debug:
msg: item
loop: "{{ gitlab_api_usuario.results | map(attribute='content') | list }}"
Output:
TASK [print results] ************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Invalid data passed to 'loop', **it requires a list, got this instead**: [Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, u'[{\"id\":8,\"name\":\"Maycon Brito\",\"username\":\"maycon.brito\",\"state\":\"active\",\"avatar_url\":\"http://192.168.101.181:8083/uploads/-/system/user/avatar/8/956667.jpeg\",\"web_url\":\"http://192.168.101.181:8083/maycon.brito\",\"created_at\":\"2018-05-15T09:33:02.866-03:00\",\"bio\":null,\"location\":null,\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"\",\"website_url\":\"\",\"organization\":null,\"last_sign_in_at\":\"2018-05-15T16:39:53.045-03:00\",\"confirmed_at\":\"2018-05-15T09:33:02.584-03:00\",\"last_activity_on\":\"2018-05-21\",\"email\":\"[email protected]\",\"theme_id\":1,\"color_scheme_id\":1,\"projects_limit\":100000,\"current_sign_in_at\":\"2018-05-17T17:19:13.442-03:00\",\"identities\":[],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":false,\"external\":false,\"is_admin\":true}]', Undefined]. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}
You can use this instead:
- name: print results
set_fact:
final_var: "{{ item.content }}"
when: item.content is defined
loop: "{{ gitlab_api_usuario.results }}"
- name: print results
debug:
var: final_var[0].id
It will get the unique (as stated in your question) result that contains the content
attribute, which is a list of a single dictionary elements, and then pick up from the 1st (and unique) element the id
attribute.
Upvotes: 3