Ansible Query, как сделать запрос внутри вывода JSON - PullRequest
0 голосов
/ 11 июня 2018

Я прошу URL API и перехожу к выводу в регистр.Это должен быть вывод в формате JSON.Первая переменная "vcclust" JSON, я могу извлечь "item.cluster", так как это всего лишь один уровень, плохо выглядит второй вывод "vchosts", я не могу разобрать, чтобы получить из него "item.name" и получить имена хостов

Это код:

- hosts: localhost
  gather_facts: False
  vars_prompt:
  - name: "vcenter_hostname"
    prompt: "Enter vcenter hostname"
    private: no
  - name: "vcenter_user"
    prompt: "Enter vcenter username"
    private: no
  - name: "vcenter_pass"
    prompt: "Enter vcenter password"
    private: yes
  - name: "cluster_touched"
    prompt: "Enter cluster name"
    private: no

  tasks:
  - name: Login in to vCenter and store login information
    uri:
      url: "https://{{ vcenter_hostname }}/rest/com/vmware/cis/session"
      force_basic_auth: yes
      method: POST
      user: '{{ vcenter_user }}'
      password: '{{ vcenter_pass }}'
      status_code: 200
      validate_certs: no
    register: login
  - name: Get vCenter cluster ID for {{ cluster_touched }}
    uri:
      url: "https://{{ vcenter_hostname }}/rest/vcenter/cluster?filter.names={{ cluster_touched }}"
      force_basic_auth: yes
      validate_certs: no
      headers:
        Cookie: '{{ login.set_cookie }}'
    register: vcclust
  - name: Get ESXi nodes from cluster ID for {{ cluster_touched }}
    uri:
      url: "https://{{ vcenter_hostname }}/rest/vcenter/host?filter.clusters={{ item.cluster }}"
      force_basic_auth: yes
      validate_certs: no
      headers:
        Cookie: '{{ login.set_cookie }}'
    register: vchosts
    with_items:
    - '{{ vcclust.json.value }}'
  - name: Modify root local user to ESXi
    vmware_local_user_manager:
      hostname: '{{ item.name }}'
      username: root
      password: '{{ esxi_pass }}'
      local_user_name: root
      local_user_password: '{{ esxi_new_pass }}'
      validate_certs: False
    with_items:
    - '{{ vchosts.json.value }}'

И это результат отладки vchosts и vcclust

vcclust:

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "vcclust": {
        "changed": false,
        "connection": "close",
        "content_type": "application/json",
        "cookies": {},
        "date": "Mon, 11 Jun 2018 09:50:43 GMT",
        "failed": false,
        "json": {
            "value": [
                {
                    "cluster": "domain-c310",
                    "drs_enabled": true,
                    "ha_enabled": false,
                    "name": "DB-CLUSTER"
                }
            ]
        },
        "msg": "OK (unknown bytes)",
        "redirected": false,
        "status": 200,
        "url": "https://vcenter01.lab.test/rest/vcenter/cluster?filter.names=DB-CLUSTER"
    }
}

vchosts:

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "vchosts": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "connection": "close",
                "content_type": "application/json",
                "cookies": {},
                "date": "Mon, 11 Jun 2018 09:50:45 GMT",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "backup": null,
                        "body": null,
                        "body_format": "raw",
                        "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": true,
                        "group": null,
                        "headers": {
                            "Cookie": "vmware-api-session-id=56fa6d3015150212b086917d15165bee;Path=/rest;Secure;HttpOnly"
                        },
                        "http_agent": "ansible-httpget",
                        "method": "GET",
                        "mode": null,
                        "owner": null,
                        "regexp": null,
                        "remote_src": null,
                        "removes": null,
                        "return_content": false,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": null,
                        "status_code": [
                            200
                        ],
                        "timeout": 30,
                        "unsafe_writes": null,
                        "url": "https://vcenter01.lab.test/rest/vcenter/host?filter.clusters=domain-c310",
                        "url_password": null,
                        "url_username": null,
                        "use_proxy": true,
                        "validate_certs": false
                    }
                },
                "item": {
                    "cluster": "domain-c310",
                    "drs_enabled": true,
                    "ha_enabled": false,
                    "name": "DB-CLUSTER"
                },
                "json": {
                    "value": [
                        {
                            "connection_state": "CONNECTED",
                            "host": "host-312",
                            "name": "vmh19.lab.test",
                            "power_state": "POWERED_ON"
                        },
                        {
                            "connection_state": "CONNECTED",
                            "host": "host-313",
                            "name": "vmh20.lab.test",
                            "power_state": "POWERED_ON"
                        }
                    ]
                },
                "msg": "OK (unknown bytes)",
                "redirected": false,
                "status": 200,
                "url": "https://vcenter01.lab.test/rest/vcenter/host?filter.clusters=domain-c310"
            }
        ]
    }
}

Я получил ошибку:

ansible (ansible_test) ✗)ansible-playbook site.yml
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

Enter vcenter hostname [vcenter01.lab.test]: 
Enter vcenter username : 
Enter vcenter password : 
Enter cluster name [DB-CLUSTER]: 

PLAY [localhost] *********************************************************************************************************************************************************************************************

TASK [Start SSH service setting for an ESXi Host in given Cluster] *******************************************************************************************************************************************
ok: [localhost]

TASK [Login in to vCenter and store login information] *******************************************************************************************************************************************************
ok: [localhost]

TASK [Get vCenter cluster ID for DB-CLUSTER] *************************************************************************************************************************************************************
ok: [localhost]

TASK [Get ESXi nodes from cluster ID for DB-CLUSTER] *****************************************************************************************************************************************************
ok: [localhost] => (item={'drs_enabled': True, 'cluster': 'domain-c310', 'name': 'DB-CLUSTER', 'ha_enabled': False})

TASK [Modify root local user to ESXi] ************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'json'"}
    to retry, use: --limit @/Users/jv/Workspace/vmware-powershell/ansible/site.retry

PLAY RECAP ***************************************************************************************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=1 

Что я делаю не так?

Ответы [ 2 ]

0 голосов
/ 13 июня 2018

Большое спасибо @Kelson Silva

Вы указали ключ к ответу.

Я не знаю, почему это

- debug: var=item.name
    with_items:
    -  "{{ vchosts.json.value }}"

не совпадает с

- debug: 
    var: item.name
    with_items:
    -  "{{ vchosts.json.value }}"

и на самом деле это сводило меня с ума, это наверняка ошибка в этой версии ansible

проблема в том, что она перетаскивала элементы в моем первом запросе как список внутри Jsonи вам нужно перечислить их.

Чтобы не получить грязный JSON "vchosts", вам нужно использовать только один элемент из "vcclust", поэтому код заканчивается следующим образом:

  - name: Get vCenter cluster ID for {{ cluster_touched }}
    uri:
      url: "https://{{ vcenter_hostname }}/rest/vcenter/cluster?filter.names={{ cluster_touched }}"
      force_basic_auth: yes
      validate_certs: no
      headers:
        Cookie: '{{ login.set_cookie }}'
    register: vcclust

  - name: Get ESXi nodes from cluster ID for {{ cluster_touched }}
    uri:
      url: "https://{{ vcenter_hostname }}/rest/vcenter/host?filter.clusters={{ vcclust.json.value[0].cluster }}"
      force_basic_auth: yes
      validate_certs: no
      headers:
        Cookie: '{{ login.set_cookie }}'
    register: vchosts

  - name: Modify root local user to ESXi
    vmware_local_user_manager:
      hostname: '{{ item.name }}'
      username: root
      password: '{{ esxi_pass }}'
      local_user_name: root
      local_user_password: '{{ esxi_new_pass }}'
      validate_certs: False
    with_items:
    -  "{{ vchosts.json.value }}"
0 голосов
/ 11 июня 2018

Попробуйте использовать json_query.Для этого вам может потребоваться установить jmespath на вашем хосте.Вы можете сделать это с pip install jmespath.

 - name: Get ESXi nodes from cluster ID for {{ cluster_touched }}
    uri:
      url: "https://{{ vcenter_hostname }}/rest/vcenter/host?filter.clusters={{ item.cluster }}"
      force_basic_auth: yes
      validate_certs: no
      headers:
        Cookie: '{{ login.set_cookie }}'
    register: vchosts
    with_items: "{{ vcclust | json_query('*.value') }}"

  - name: Modify root local user to ESXi
    vmware_local_user_manager:
      hostname: '{{ item.name }}'
      username: root
      password: '{{ esxi_pass }}'
      local_user_name: root
      local_user_password: '{{ esxi_new_pass }}'
      validate_certs: False
    with_items: "{{ vchosts.results | json_query('*.value') }}"
...