Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从ansible uri json请求获取一个值_Ansible_Ansible 2.x_Ansible Template - Fatal编程技术网

从ansible uri json请求获取一个值

从ansible uri json请求获取一个值,ansible,ansible-2.x,ansible-template,Ansible,Ansible 2.x,Ansible Template,我是ansible和jinja模板中的noob,我需要从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: GE

我是ansible和jinja模板中的noob,我需要从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\u api\u 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

从uri返回的纯Json

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
            }
        ]
    }
}
ok:[192.168.101.151]=>(item=None)=>{
“gitlab_api_usuario”:{
“更改”:错误,
“msg”:“所有项目已完成”,
“结果”:[
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“breno.barbosa”,
“价值”:{
“主机名”:“informatica13”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“密钥”:“angeloalves”,
“价值”:{
“主机名”:“informatica20”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“弗拉维亚·马蒂斯”,
“价值”:{
“主机名”:“informatica12”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“埃德尔森”,
“价值”:{
“主机名”:“informatica18”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“西尔维德罗”,
“价值”:{
“主机名”:“informatica15”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“莱昂纳多·纳迪”,
“价值”:{
“主机名”:“informatica16”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“亚伯”,
“价值”:{
“主机名”:“informatica3”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“弗拉维亚费雷拉”,
“价值”:{
“主机名”:“informatica8”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“亚历山德罗”,
“价值”:{
“主机名”:“informatica5”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“密钥”:“mayse”,
“价值”:{
“主机名”:“informatica1”
}
}, 
“跳过原因”:“条件结果为假”,
“跳过”:true
}, 
{
“_ansible_ignore_errors”:null,
“_ansible_item_result”:true,
“\u ansible\u no\u log”:false,
“更改”:错误,
“项目”:{
“钥匙”:“布鲁诺·费雷拉”,
“价值”:{
“主机名”:“informatica26”
}
}, 
“s
- 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