Select Page

Summary

In the first post, “Getting started with Infoblox and Ansible“, I showed you how to set up an Ubuntu box and install Ansible. I also started with your first “playbook” to talk to your Infoblox NIOS Grid Master to return all of the “members” in your Infoblox Grid.

Now, let’s configure your Ubuntu box to use variables and store your username and password in a separate file. While we’re at it, we’ll also give you some examples of using Infoblox and Ansible “lookup”.

Credentials and authenticating

To use the Infoblox nios modules in playbooks, you need to configure the credentials to access your Infoblox system. The examples in this guide use credentials stored in <playbookdir>/group_vars/nios.yml. Replace these values with your Infoblox credentials:

---
nios_provider:
  host: 192.168.0.200
  username: admin
  password: infoblox

Now, let’s add “nios” to the static host

sudo vi /etc/ansible/hosts
<shift-G> (move to the bottom of the file)
"i" (to enter interactive mode) and the following
[nios]
192.168.0.200
<esc>
:wq

Let’s get started

Let’s start building playbooks using the “lookup” module. We will name all the playbooks starting with “lk_” to keep them separate from the others we will build later. Fire up your favorite Ubuntu editor and get started with Infoblox and Ansible nios “lookup”:

We are going to create an “alias” for the “ansible-playbook” (because I can’t see typing ansible-playbook over and over):

alias ap=ansible-playbook

Ok, now open your favorite editor and create your first playbook using nios “lookup” following file “lk_member.yml“:

---
- hosts: nios
  connection: local
  tasks:
    - name: fetch all member objects
      set_fact:
        members: "{{ lookup('nios', 'member', provider=nios_provider) }}"

    - name: check the members
      debug:
        var: members

Run the playbook to get all the members using “lk_member.yml”:

$ ap lk_member.yml 

PLAY [nios] ***************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [192.168.0.200]

TASK [fetch all member objects] **************************************************************************************************
ok: [192.168.0.200]

TASK [check the members] *********************************************************************************************************
ok: [192.168.0.200] => {
    "members": [
        {
            "_ref": "member/b25lLnZpcnR1YWxfbm9kZSQw:infoblox.localdomain",
            "host_name": "infoblox.localdomain"
        },
        {
            "_ref": "member/b25lLnZpcnR1YWxfbm9kZSQx:m1.tiamat.net",
            "host_name": "m1.tiamat.net"
        }
    ]
}

PLAY RECAP ****************************************************************************************************************************
192.168.0.200              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Now we have the script from the previous post without credentials. Let’s create a new file called “lk_hosts.yml“. We are going to “lookup” a host record called “sif1.ansible.com” with the following playbook.

---
- hosts: nios
  connection: local
  tasks:
    - name: get host list using FQDN string
      set_fact:
       host_list: "{{ lookup('nios', 'record:host', filter={'name':'sif1.ansible.com'}, provider=nios_provider) }}"
    - name: display host list using FQDN string
      debug:
       msg: "{{ host_list }}"

[et_bloom_inline optin_id=”optin_2″]

Now run the “lk_hosts.yml“:

$ ap lk_hosts.yml 

PLAY [nios] ***************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [192.168.0.200]

TASK [get host list using FQDN string] *********************************************************************************************
ok: [192.168.0.200]

TASK [display host list using FQDN string] *****************************************************************************************
ok: [192.168.0.200] => {
    "msg": {
        "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5hbnNpYmxlLnNpZjE:sif1.ansible.com/default",
        "ipv4addrs": [
            {
                "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLmFuc2libGUuc2lmMS4xOTIuMTY4LjIwMC4xNC4:192.168.200.14/sif1.ansible.com/default",
                "configure_for_dhcp": false,
                "host": "sif1.ansible.com",
                "ipv4addr": "192.168.200.14"
            }
        ],
        "name": "sif1.ansible.com",
        "view": "default"
    }
}

PLAY RECAP ****************************************************************************************************************************
192.168.0.200              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


Let’s say you want to search for all your host records that start with “sif”. Create a new playbook and call it “lk_search_hosts.yml“:

---
- hosts: nios
  connection: local
  tasks:
    - name: get host list using partial string
      set_fact:
       host_list: "{{ lookup('nios', 'record:host', filter={'name~':'sif'}, provider=nios_provider) }}"
    - name: display host list using partial string
      debug:
       msg: "{{ host_list }}"

Once we run this playbook it will return everything that starts with “sif” in the host name:

$ ap lk_search_hosts.yml 

PLAY [nios] **************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.0.200]

TASK [get host list using partial string] ********************************************************************************************************************
ok: [192.168.0.200]

TASK [display host list using partial string] ****************************************************************************************************************
ok: [192.168.0.200] => {
    "msg": [
        {
            "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5hbnNpYmxlLnNpZjE:sif1.ansible.com/default",
            "ipv4addrs": [
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLmFuc2libGUuc2lmMS4xOTIuMTY4LjIwMC4xNC4:192.168.200.14/sif1.ansible.com/default",
                    "configure_for_dhcp": false,
                    "host": "sif1.ansible.com",
                    "ipv4addr": "192.168.200.14"
                }
            ],
            "name": "sif1.ansible.com",
            "view": "default"
        },
        {
            "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5hbnNpYmxlLnNpZjI:sif2.ansible.com/default",
            "ipv4addrs": [
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLmFuc2libGUuc2lmMi4xOTIuMTY4LjIwMC4xNS4:192.168.200.15/sif2.ansible.com/default",
                    "configure_for_dhcp": false,
                    "host": "sif2.ansible.com",
                    "ipv4addr": "192.168.200.15"
                }
            ],
            "name": "sif2.ansible.com",
            "view": "default"
        },
        {
            "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5hbnNpYmxlLnNpZjM:sif3.ansible.com/default",
            "ipv4addrs": [
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLmFuc2libGUuc2lmMy4xOTIuMTY4LjIwMC4xNi4:192.168.200.16/sif3.ansible.com/default",
                    "configure_for_dhcp": false,
                    "host": "sif3.ansible.com",
                    "ipv4addr": "192.168.200.16"
                }
            ],
            "name": "sif3.ansible.com",
            "view": "default"
        },
        {
            "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmxvY2FsLmJha3NoLnNpZi1uZXRtcmk:sif-netmri.baksh.local/default",
            "ipv4addrs": [
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQubG9jYWwuYmFrc2guc2lmLW5ldG1yaS4xNjkuMjU0LjEuMS4:169.254.1.1/sif-netmri.baksh.local/default",
                    "configure_for_dhcp": false,
                    "host": "sif-netmri.baksh.local",
                    "ipv4addr": "169.254.1.1",
                    "mac": "00:0c:29:55:91:11"
                },
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQubG9jYWwuYmFrc2guc2lmLW5ldG1yaS4xOTIuMTY4LjAuMjAxLg:192.168.0.201/sif-netmri.baksh.local/default",
                    "configure_for_dhcp": false,
                    "host": "sif-netmri.baksh.local",
                    "ipv4addr": "192.168.0.201",
                    "mac": "00:0c:29:55:91:11"
                }
            ],
            "ipv6addrs": [
                {
                    "_ref": "record:host_ipv6addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQubG9jYWwuYmFrc2guc2lmLW5ldG1yaS5mZTgwOjoyMGM6MjlmZjpmZTU1OjkxMTEu:fe80%3A%3A20c%3A29ff%3Afe55%3A9111/sif-netmri.baksh.local/default",
                    "configure_for_dhcp": false,
                    "host": "sif-netmri.baksh.local",
                    "ipv6addr": "fe80::20c:29ff:fe55:9111"
                }
            ],
            "name": "sif-netmri.baksh.local",
            "view": "default"
        }
    ]
}

PLAY RECAP ***************************************************************************************************************************************************
192.168.0.200              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

As you can see, the above returned everything that started with “sif”. Now let’s work on my favorite thing: regex! We are going to use a regex to return “sif1,sif2, and sif3”. This playbook is called “lk_search_hosts_regex.yml“:

---
- hosts: nios
  connection: local
  tasks:
    - name: get host list using partial string
      set_fact:
       host_list: "{{ lookup('nios', 'record:host', filter={'name~':'sif[1,2,3]'}, provider=nios_provider) }}"
    - name: display host list using partial string
      debug:
       msg: "{{ host_list }}"

This will return sif1 to sif3. Now, in my case, I only have three entries with sif(1,2,3).ansible.com. If I had more, for example, sif(1,2).infoblox.com, it would return those as well.

$ ap lk_search_hosts_regex.yml 

PLAY [nios] **************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.0.200]

TASK [get host list using partial string] ********************************************************************************************************************
ok: [192.168.0.200]

TASK [display host list using partial string] ****************************************************************************************************************
ok: [192.168.0.200] => {
    "msg": [
        {
            "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5hbnNpYmxlLnNpZjE:sif1.ansible.com/default",
            "ipv4addrs": [
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLmFuc2libGUuc2lmMS4xOTIuMTY4LjIwMC4xNC4:192.168.200.14/sif1.ansible.com/default",
                    "configure_for_dhcp": false,
                    "host": "sif1.ansible.com",
                    "ipv4addr": "192.168.200.14"
                }
            ],
            "name": "sif1.ansible.com",
            "view": "default"
        },
        {
            "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5hbnNpYmxlLnNpZjI:sif2.ansible.com/default",
            "ipv4addrs": [
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLmFuc2libGUuc2lmMi4xOTIuMTY4LjIwMC4xNS4:192.168.200.15/sif2.ansible.com/default",
                    "configure_for_dhcp": false,
                    "host": "sif2.ansible.com",
                    "ipv4addr": "192.168.200.15"
                }
            ],
            "name": "sif2.ansible.com",
            "view": "default"
        },
        {
            "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmNvbS5hbnNpYmxlLnNpZjM:sif3.ansible.com/default",
            "ipv4addrs": [
                {
                    "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLmFuc2libGUuc2lmMy4xOTIuMTY4LjIwMC4xNi4:192.168.200.16/sif3.ansible.com/default",
                    "configure_for_dhcp": false,
                    "host": "sif3.ansible.com",
                    "ipv4addr": "192.168.200.16"
                }
            ],
            "name": "sif3.ansible.com",
            "view": "default"
        }
    ]
}

PLAY RECAP ***************************************************************************************************************************************************
192.168.0.200              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


Conclusion

I know my examples mostly focused around “lookup” and “host”, however you can use the above examples with the following modules and additional examples below:

Some other examples:

Look up an A Record:
        arecord: "{{ lookup('nios', 'record:a', filter={'name~':'sifbaksh'}

Lookup Fixed Address:
        fixaddr: "{{ lookup('nios', 'fixedaddress', provider=nios_provider) }}"
        fixaddr: "{{ lookup('nios', 'fixedaddress', filter {'ipv4addr':'10.10.0.3'},return_fields=['mac','options'], provider=nios_provider) }}"

You get the idea. Post comments if you need additional examples.

The next post will be around using “nios_next_network” and “lookup” and also creating a network/networks.
Files are located here: GitHub

Sif Baksh Administrator
Principal Solutions Architect
As Principal Solutions Architect, Sif Baksh is responsible for the design of large-scale Core Services and Security systems. With 25 years of engineering experience in the computer and communications industry, Sif brings a depth of understanding of complex solutions for large and small organizations.
follow me