0


This is a playbook part of a role that talks with vmware.
I'm is trying to set a custom fact (target_vm) that receive the virtual machine name.
But when I Print it's value with the debug module it apears to be empty.
It seams that the set_fact is correctly set because the debug output shows {"ansible_facts": {"target_vm": "TESTVM"}.
I've also made a Fail task that reveals if the target_vm fact is set or not.

This is the Playbook

- delegate_to: localhost become: no delegate_facts: yes vars: vc_hostname: 'vcenter.bio.local' vc_username: 'bio.local\ansible' vc_password: "{{ v_pass }}" vm_uuid: '4217200F-46D2-C9FD-E7FD-768D21B327E8' | lower block: - name: Gather only registered virtual machines vmware_vm_info: hostname: '{{ vc_hostname }}' username: '{{ vc_username }}' password: '{{ vc_password }}' validate_certs: False vm_type: vm delegate_to: localhost register: virtual_info no_log: true - name: Get VM name from UUID set_fact: target_vm: "{{ my_item.guest_name }}" loop: "{{ virtual_info.virtual_machines }}" when: my_item.uuid == vm_uuid loop_control: loop_var: my_item delegate_to: localhost delegate_facts: True - name: Verify interface name is set fail: msg: "Could not determine target_vm name!" when: target_vm is not defined - name: Print Output debug: msg: "The VM name is: {{ target_vm }}" 


This is the output

TASK [vmwaretaks : Get VM name from UUID] *************************************************************************************** ok: [testvm] => (item={'guest_name': 'TESTVM', 'guest_fullname': 'Red Hat Enterprise Linux 6 (64-bit)', 'power_state': 'poweredOn', 'ip_address': '192.168.54.32', 'mac_address': ['00:32:52:97:e9:c8'], 'uuid': '4217200f-46d2-c9fd-e7fd-768d21b327e8', 'vm_network': {'00:32:52:97:e9:c8': {'ipv4': ['192.168.54.32'], 'ipv6': ['fe80::250:56ff:fe97:d4c3']}}, 'esxi_hostname': 'b3j15esx05.bio.local', 'cluster': 'JAS-Lab-DEP', 'attributes': {}, 'tags': []}) => {"ansible_facts": {"target_vm": "TESTVM"}, "ansible_loop_var": "my_item", "changed": false, "my_item": {"attributes": {}, "cluster": "JAS-Lab-DEP", "esxi_hostname": "b3j15esx05.bio.local", "guest_fullname": "Red Hat Enterprise Linux 6 (64-bit)", "guest_name": "TESTVM", "ip_address": "192.168.54.32", "mac_address": ["00:32:52:97:e9:c8"], "power_state": "poweredOn", "tags": [], "uuid": "4217200f-46d2-c9fd-e7fd-768d21b327e8", "vm_network": {"00:32:52:97:e9:c8": {"ipv4": ["192.168.54.32"], "ipv6": ["fe80::250:56ff:fe97:d4c3"]}}}} TASK [vmwaretaks : Verify interface name is set] ******************************************************************************** fatal: [testvm]: FAILED! => {"changed": false, "msg": "Could not determine target_vm name!"} PLAY RECAP ********************************************************************************************************************** testvm : ok=13 changed=0 unreachable=0 failed=1 skipped=2 rescued=0 ignored=0 

1 Answer 1

1

You have delegate_facts: True on the set_fact task, so you are setting the variable for localhost. You are then attempting to access it in the context of the play host, which does not have it set.

You have to decide where you want to set it. If localhost is correct, you need to access it via hostvars (hostvars['localhost']['testvm']); if it's not, you need to remove delegate_facts.

1
  • You're wright @flowerysong. I was accessing the wrong way to the target_vm fact. The hostvars way makes the point. Thanks you for instruct me! Commented Sep 27, 2021 at 19:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.