0

I have the following playbook in AWX that looks up Infoblox hosts based on their Mac Address and then outputs the information in a more user friendly format.

The current playbook works providing that a single host with that Mac address exists but fails if there are multiple.

---
- hosts: localhost
  connection: local

  vars:
    niosip: ""
    niosmac: ""
    niosdhcp: ""
    nioshostname: ""
    niossearchcatagory: "{{ 'name' if searchcatagory == 'Hostname' else 'ipv4addr' if searchcatagory == 'IP Address' else 'mac' if searchcatagory == 'Mac Address'}}"

  pre_tasks:
    - include_vars: 
        file: creds.yml

  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 != [] 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

    - name: Host not found
      debug:
        msg: 'Cant find related host'
      when: host == []

    - name: Display Display Registration Info
      debug:
        msg:
        - Hostname = {{ nioshostname }}
        - IP = {{ niosip }}
        - Mac Address {{ niosmac }}
        - Registered for DHCP = {{ niosdhcp }}
      when: host != [] and host.ipv4addrs[0].mac is defined

Variables niossearchcatagory and searchcriteria are passed into the playbook via an AWX Survey.

I've searched possible options around using loops or splitting the output down but I'm really at a loss on the best way to process this.

If the output matches this then the playbook works as expected

{
    "changed": false,
    "ansible_facts": {
        "host": [
            {
                "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LnVrLmFjLmJoYW0udGVzdC5zbmF0LWF3eHRlc3Q1:snat-awxtest5.test.com/Internal",
                "ipv4addrs": [
                    {
                        "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQudWsuYWMuYmhhbS50ZXN0LnNuYXQtYXd4dGVzdDUuMTQ3LjE4OC4zMS40Lg:192.168.31.4/snat-awxtest5.test.com/Internal",
                        "configure_for_dhcp": false,
                        "host": "snat-awxtest5.test.com",
                        "ipv4addr": "192.168.31.4",
                        "mac": "10:20:30:40:50:60"
                    }
                ],
                "name": "snat-awxtest5.test.com",
                "view": "Internal"
            },
        ]
    },
    "_ansible_no_log": false
}

And here's an example of the play returning multiple values

{
    "changed": false,
    "ansible_facts": {
        "host": [
            {
                "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LnVrLmFjLmJoYW0udGVzdC5zbmF0LWF3eHRlc3Q1:snat-awxtest5.test.com/Internal",
                "ipv4addrs": [
                    {
                        "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQudWsuYWMuYmhhbS50ZXN0LnNuYXQtYXd4dGVzdDUuMTQ3LjE4OC4zMS40Lg:192.168.31.4/snat-awxtest5.test.com/Internal",
                        "configure_for_dhcp": false,
                        "host": "snat-awxtest5.test.com",
                        "ipv4addr": "192.168.31.4",
                        "mac": "10:20:30:40:50:60"
                    }
                ],
                "name": "snat-awxtest5.test.com",
                "view": "Internal"
            },
            {
                "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LnVrLmFjLmJoYW0udGVzdC5zbmF0LW15d2Vi:snat-myweb.test.com/Internal",
                "ipv4addrs": [
                    {
                        "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQudWsuYWMuYmhhbS50ZXN0LnNuYXQtbXl3ZWIuMTQ3LjE4OC4zMS4yLg:192.168.31.2/snat-myweb.test.com/Internal",
                        "configure_for_dhcp": false,
                        "host": "snat-myweb.test.com",
                        "ipv4addr": "192.168.31.2",
                        "mac": "10:20:30:40:50:60"
                    }
                ],
                "name": "snat-myweb.test.com",
                "view": "Internal"
            },
            {
                "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LnVrLmFjLmJoYW0udGVzdC5zbmF0LXdlYg:snat-web.test.com/Internal",
                "ipv4addrs": [
                    {
                        "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQudWsuYWMuYmhhbS50ZXN0LnNuYXQtd2ViLjE0Ny4xODguMzEuMy4:192.168.31.3/snat-web.test.com/Internal",
                        "configure_for_dhcp": false,
                        "host": "snat-web.test.com",
                        "ipv4addr": "192.168.31.3",
                        "mac": "10:20:30:40:50:60"
                    }
                ],
                "name": "snat-web.test.com",
                "view": "Internal"
            }
        ]
    },
    "_ansible_no_log": false
}

And this results in an error as the variables host.name, host.ipv4addrs etc.. don't exist which I presume is becasue there are multiples.

Any help on how to output each registration would be gratefully received.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • 1
    Hi and welcome to SO. The example data you have provided would always produce an error with your actual playbook . From what I can read, there is nothing such as `host.name` or `host.ipv4addrs` defined. But `host[X].name` ans `host[X].ipv4addrs` exist in both outputs. Either you pasted the wrong playbook or the wrong data. Please take some time to go though [How to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Zeitounator Jun 21 '19 at 12:57
  • Sorry I've pasted the wrong playbook example! I'll update shortly. – spenwebb189 Jun 21 '19 at 14:44
  • I've updated the port with the correct playbook, apologies for posting in error. – spenwebb189 Jun 21 '19 at 14:47
  • 1
    [This would still fail in all cases with your sample data](https://gist.github.com/zeitounator/4c72cd0d7de93b4f4e8a1516d290680c) – Zeitounator Jun 21 '19 at 15:32
  • Ok, sorry I'll have a look through the links you sent. – spenwebb189 Jun 21 '19 at 15:59

0 Answers0