Estou usando o URI para obter uma saída do JSOn para um registrador chamado "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"
}
]
}
}
Do JSON completo, só preciso dos valores:
"name": "vmh20.lab.test"
"name": "vmh19.lab.test"
Esta saída pode fornecer qualquer número de hosts no cluster, dependendo do tamanho do cluster.
Eu quero usar esses valores como entrada para o nome do host na tarefa:
- name: Modify root local user to ESXi
vmware_local_user_manager:
hostname: '{{ item.results.json.value.name }}'
username: root
password: '{{ esxi_pass }}'
local_user_name: root
local_user_password: '{{ esxi_new_pass }}'
validate_certs: False
with_items:
- "{{ vchosts }}"
mas não funciona como no erro:
TASK [Modify root local user to ESXi] **********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'json'\n\nThe error appears to have been in '/Users/jv/Workspace/vmware-powershell/ansible/site.yml': line 90, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Modify root local user to ESXi\n ^ here\n"}
Investigando um pouco mais usando o debug eu tentei
- debug:
var: item.json.value.name
with_items:
- "{{ vchosts.results }}"
mas a saída é um
TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": "Hello world!"
}
Eu também tentei:
- debug:
var: item.vchosts.results.json.value[*].name
with_items:
- "{{ vchosts }}"
- debug:
var: item
with_items:
- "{{ vchosts | json_query('[*].value[*].{name: name}') }}"
e eu tenho o mesmo mundo Olá como antes. o último que tentei foi
- debug:
var: item.name
with_items:
- "{{ vchosts | json_query('*.value') }}"
e, em vez de um mundo Hello, recebo saída vazia na tarefa
TASK [debug] ***********************************************************************************************************************************************************************************************
Estou usando ansible 2.5.4