Score:0

Hostvar syntax cannot receive extra variable as hostname

ph flag
azk

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
in flag
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]`?
azk avatar
ph flag
azk
target_host receives the extra variable "vmname" that get the hostname with the -e option when I run the playbook
azk avatar
ph flag
azk
Sorry. I was trying to get value that was not gathered. Thanks for the help!!
br flag
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.
Score:0
ph flag
azk

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.