Score:1

Custom fact does not receive value

ph flag
azk


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.
I don't know what I'm is doing wrong in the set_fact module.

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: Set facts target_vm when equal vm-name
      set_fact:
        target_vm: "{{ my_item.guest_name }}"
      with_items: "{{ virtual_info.virtual_machines }}"
      when: my_item.uuid == vm_uuid
      loop_control:
        loop_var: my_item


    - name: Print Guest Name
      debug:
        msg:
          - "{{ foo_item.guest_name }}"
          - "{{ target_vm | default ('') }}"
      with_items: "{{ virtual_info.virtual_machines }}"
      when: foo_item.uuid == vm_uuid
      loop_control:
        loop_var: foo_item


This is the output

TASK [vmwaretaks : Gather only registered virtual machines] ***********************************************************************************************
ok: [testvm]

TASK [vmwaretaks : Set facts target_vm when equal vm-name] ************************************************************************************************
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': []})

TASK [vmwaretaks : Print Guest Name] **********************************************************************************************************************
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': []}) => {
    "msg": [
        "TESTVM",
        ""
    ]
}

PLAY RECAP ************************************************************************************************************************************************
testvm              : ok=13   changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0


As you can see the second message that prints the target_vm var output is empty
Hope you can help. Thanks in advance!

azk avatar
ph flag
azk
I have changed the way I parse the guest_name in the set_fact and had a progress but stills debug module don't prints the target_vm.
azk avatar
ph flag
azk
`- name: Set facts target_vm when equal vm-name set_fact: target_vm: "{{ my_item['guest_name'] }}" with_items: "{{ virtual_info.virtual_machines }}" when: my_item.uuid == vm_uuid loop_control: loop_var: my_item`
azk avatar
ph flag
azk
`TASK [vmwaretaks : Set facts target_vm when equal vm-name] ************************************************************************* 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']}}, 'esxi_hostname': 'b3j15esx05.bio.local', 'cluster': 'JAS-Lab-DEP', 'attributes': {}, 'tags': []}) => {"ansible_facts": {"target_vm": "TESTVM"}...`
Score:0
ph flag
azk

Well. It seems the fact is set with its value but I don't handle to print it correctly so I will expose the question again on other post.

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.