I'm is trying to use an extra vars to set the hostname in the "hostvars" variable to get the uuid from other host. But it seems that don't catches the variable's value.
This is the run line:
$ ansible-playbook -i ../Inventory/my_inventory --vault-id vaultfile getuuid.yml -e vmname=testhost
This is the playbook
---
- hosts: localhost
gather_facts: true
vars:
vcenter_hostname: 'vcenter.bio.local'
vcenter_username: 'bio.local\ansible'
vcenter_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
33376437643462306363663235353732613838623561616532383236633563663938656236643861
3562366533306633386632356265623664396562636665360a323039396464336561383865386661
37383766643536313639313337363263653537613238396136393581373932633166343834383162
3735643035403631620a613666363336656634646436336336393238393866303133633265383461
6434
target_host: "{{ vmname }}"
vm_uuid: "{{ hostvars[target_host]['ansible_product_uuid'] }}"
tasks:
- name: Print host
debug:
msg: "{{ vm_uuid }}"
This is the Error result
PLAY [localhost] ************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [localhost]
TASK [Print host] ***********************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: {{ hostvars[target_host]['ansible_product_uuid'] }}: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_product_uuid'\n\nThe error appears to be in '/user/getuuid.yml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Print host\n ^ here\n"}
PLAY RECAP ******************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0