0

I have an Ansible playbook that I'm running from AWX. The playing uses the Infoblox nios module to retrieve information about Infoblox host registrations.

I'm using the set_fact module to to take the output of the query and then define a number of new facts to use elsewhere in the playbook.

The problem that I have is that the query can return a differing number of variables depending on the format of the registration and this breaks the playbook.

What I am trying to do is workout if I can set a new fact only if the specific variable is returned in the original query.

I've tried using "if defined" but that doesn't seem to work.

In example 1 the play "fetch host record" returns the following values. host, ipv4addr and mac as the host has a Mac Address in Infoblox

ok: [localhost] => {
    "ansible_facts": {
        "host": {
            "ipv4addrs": [
                {
                    "host": "myhost1.test.com",
                    "ipv4addr": "192.168.30.1",
                    "mac": "00:22:33:11:44:55"
                }
            ],
            "name": "myhost1.test.com",
            "view": "Internal"
        }
    },
    "changed": false
}

In example 2 the same play only returns host and ipv4addr as the host does not have a Mac Address registered.

ok: [localhost] => {
    "ansible_facts": {
        "host": {
            "ipv4addrs": [
                {
                    "host": "myhost2.test.com",
                    "ipv4addr": "192.168.30.2"
                }
            ],
            "name": "myhost2.test.com",
            "view": "Internal"
        }
    },
    "changed": false
}

My playbook contains the following and works only if the host contains a Mac Address as the fact host, doesn't contain a value for host.ipv4addrs[0].mac so it crashes out. I'd like to add some logic to only try and set niosmac if host.ipv4addrs[0].mac is defined.

  tasks:
    - name: fetch host record
      set_fact:
        host: "{{ lookup('nios', 'record:host', filter={niossearchcatagory: searchcriteria, 'view': 'Internal'}, provider=nios_provider) }}"

    - name: Set niosip
      set_fact:
        niosip: "{{ host.ipv4addrs[0].ipv4addr }}"
        nioshostname: "{{ host.name }}"
        niosdhcp: "{{ host.ipv4addrs[0].configure_for_dhcp }}"
        niosmac: "{{ host.ipv4addrs[0].mac }}"

Here's the version I attempted using is defined

tasks:
    - name: fetch host record
      set_fact:
        host: "{{ lookup('nios', 'record:host', filter={niossearchcatagory: searchcriteria, 'view': 'Internal'}, provider=nios_provider) }}"
    - name: Set niosip
      set_fact:
        niosip: "{{ host.ipv4addrs[0].ipv4addr }}"
        nioshostname: "{{ host.name }}"
        niosdhcp: "{{ host.ipv4addrs[0].configure_for_dhcp }}"
        niosmac: "{{ host.ipv4addrs[0].mac }}"
      when: host.ipv4addrs[0].mac is defined

Cheers Spence

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174

1 Answers1

0

Sorry, I must have typed something wrong before as I've tried again and it now seems to work. Here's the correct code for clarification.

- name: Set niosip
  set_fact:
    niosip: "{{ host.ipv4addrs[0].ipv4addr }}"
    nioshostname: "{{ host.name }}"
    niosdhcp: "{{ host.ipv4addrs[0].configure_for_dhcp }}"
    niosmac: "{{ host.ipv4addrs[0].mac }}"
  when: host != [] and host.ipv4addrs[0].mac is defined

- name: Set niosip
  set_fact:
    niosip: "{{ host.ipv4addrs[0].ipv4addr }}"
    nioshostname: "{{ host.name }}"
    niosdhcp: "{{ host.ipv4addrs[0].configure_for_dhcp }}"
  when: host != [] and host.ipv4addrs[0].mac is undefined