Set a fact based on a fact name in a fact

35 views
Skip to first unread message

Lise Mattice

unread,
Sep 14, 2016, 1:52:26 PM9/14/16
to Ansible Project
I have a situation where I am trying to dynamically get the MAC address on the first NIC in a new VM created by Ansible.  I could assume the HW Interface will be "eth0", but it would be better to figure it out at runtime.

Register variable "create_vm_results" is:

ok: [localhost] => {
    "create_vm_results": {
        "ansible_facts": {
            "hw_eth0": {
                "addresstype": "assigned",
                "ipaddresses": null,
                "label": "Network adapter 1",
                "macaddress": "MAC",
                "macaddress_dash": "Dash-MAC",
                "summary": "VM Network"
            },
            "hw_guest_full_name": "Red Hat Enterprise Linux 6 (64-bit)",
            "hw_guest_id": "rhel6_64Guest",
            "hw_interfaces": [
                "eth0"
            ],
            "hw_memtotal_mb": 2048,
            "hw_name": "NAME",
            "hw_power_status": "POWERED ON",
            "hw_processor_count": 1,
            "hw_product_uuid": "UUID",
            "module_hw": true
        },
        "changed": true,
        "changes": "Created VM NAME"
    }
}



Now set facts based on this portion of my playbook:

- name: VMWARE - Identify NIC of primary interface
  set_fact:
    nic: "{{ create_vm_results.ansible_facts.hw_interfaces[0] }}"

- name: VMWARE - Show Created VM Primary NIC (DEBUG)
  debug: var=nic

- name: VMWARE - Identify Adapter of primary interface
  set_fact:
    adptr: "create_vm_results.ansible_facts.hw_{{ nic }}.macaddress"

- name: VMWARE - Show Created VM Primary Adapter (DEBUG)
  debug: var=adptr

- name: VMWARE - Identify MAC Address of primary interface
  set_fact:
    mac: "{{ adptr }}"

- name: VMWARE - Show Created VM Primary MAC (DEBUG)
  debug: var=mac


This produced this output:

TASK [vm_create : VMWARE - Identify NIC of primary interface] ******************
ok: [localhost]

TASK [vm_create : VMWARE - Show Created VM Primary NIC (DEBUG)] ****************
ok: [localhost] => {
    "nic": "eth0"
}

TASK [vm_create : VMWARE - Identify Adapter of primary interface] **************
ok: [localhost]

TASK [vm_create : VMWARE - Show Created VM Primary Adapter (DEBUG)] ************
ok: [localhost] => {
    "adptr": "create_vm_results.ansible_facts.hw_eth0.macaddress"
}

TASK [vm_create : VMWARE - Identify MAC Address of primary interface] **********
ok: [localhost] => (item=create_vm_results.ansible_facts.hw_eth0.macaddress)

TASK [vm_create : VMWARE - Show Created VM Primary MAC (DEBUG)] ****************
ok: [localhost] => {
    "mac": "create_vm_results.ansible_facts.hw_eth0.macaddress"
}



The objective was to get the MAC, but instead it is the "fact" name.  Is there a way to look up the value of this fact?  

Any assistance would be appreciated.

Thanks



Kai Stian Olstad

unread,
Sep 14, 2016, 2:07:02 PM9/14/16
to ansible...@googlegroups.com
On 13. sep. 2016 23:55, Lise Mattice wrote:
[snip]
> - name: VMWARE - Identify Adapter of primary interface
> set_fact:
> adptr: "create_vm_results.ansible_facts.hw_{{ nic }}.macaddress"
>
> - name: VMWARE - Show Created VM Primary Adapter (DEBUG)
> debug: var=adptr
>
[snip]
> TASK [vm_create : VMWARE - Show Created VM Primary Adapter (DEBUG)]
> ************
> ok: [localhost] => {
> "adptr": "create_vm_results.ansible_facts.hw_eth0.macaddress"
> }
[snip]
>
> The objective was to get the MAC, but instead it is the "fact" name. Is
> there a way to look up the value of this fact?
>
> Any assistance would be appreciated.

To use variables you need to use hostvars to make it work

{{
hostvars[inventory_hostname]['create_vm_results']['ansible_facts']['hw_'
+ nic]['macaddress'] }}

--
Kai Stian Olstad

Lise Mattice

unread,
Sep 19, 2016, 1:18:00 PM9/19/16
to Ansible Project
Thanks Kai!

That worked perfectly!

Lise 
Reply all
Reply to author
Forward
0 new messages