Hostvar 语法无法接收额外的变量作为主机名

Hostvar 语法无法接收额外的变量作为主机名

我正在尝试使用额外的变量在“hostvars”变量中设置主机名,以便从其他主机获取 uuid。但似乎无法捕获变量的值。
这是运行行:

$ ansible-playbook -i ../Inventory/my_inventory --vault-id vaultfile getuuid.yml -e vmname=testhost

这是剧本

---
- 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 }}"

这是错误结果

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

答案1

未收集 ansible_product_uuid。hostvars 变量工作正常。

相关内容