0

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
azk
  • 39
  • 9
  • Did ansible actually gather the facts for `target_host`? You can't use a fact for the target host unless you either gather that fact, or you have fact caching enabled and the cache is still valid. What happens if you just do a `debug` on the var `hostvars[target_host]`? – Zoredache Oct 04 '21 at 23:39
  • target_host receives the extra variable "vmname" that get the hostname with the -e option when I run the playbook – azk Oct 04 '21 at 23:53
  • Sorry. I was trying to get value that was not gathered. Thanks for the help!! – azk Oct 05 '21 at 00:00
  • If the only host is ``localhost`` then there are no other ``hostvars`` except ``hostvars.localhost`` available to the play. Test it. Print an existing fact. Edit the question and make it [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Remove the redundant variables and code, e.g. 1) Remove ``variables vcenter_*`` 2) Remove options ``-i`` and ``--vault-id``. The options do not influence the problem. You don't provide the files anyway. – Vladimir Botka Oct 05 '21 at 03:25

1 Answers1

0

The ansible_product_uuid was not gathered. The hostvars variable works fine.

azk
  • 39
  • 9