从 ansible uri json 请求中获取值



我是 ansible 和 jinja 模板中的菜鸟,我需要从 uri json 结果中获取一个值,但我不知道该怎么做。

我在下面有 uri 请求:

- 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 }}"

所以我从变量中获取寄存器gitlab_api_usuario当我使用下面的调试时,我得到以下结果。

- debug:
var: "{{ item }}"
with_items: "gitlab_api_usuario.results | map(attribute='content') | list"

结果:

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":"mayconbrito@teste.rede.teste","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]"
}

因此,我需要从唯一的成功请求中获取id值。

纯洁的杰森从乌里回来了。

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":"mayconbrito@bioextratus.rede.bioextratus","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": "mayconbrito@bioextratus.rede.bioextratus", 
"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
}
]
}
}

我不知道为什么,但您的过滤不会导致列表变量:

- name: print results
debug:
msg: item
loop: "{{ gitlab_api_usuario.results | map(attribute='content') | list }}"

输出:

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":"mayconbrito@bioextratus.rede.bioextratus","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."}

您可以改用它:

- 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

它将获得包含content属性(即单个字典元素的列表(的唯一(如您的问题中所述(结果,然后从第一个(和唯一(元素中选取id属性。

相关内容

  • 没有找到相关文章

最新更新