Infoblox Guide

This guide describes how to use Ansible with the Infoblox Network Identity Operating System (NIOS). With Ansible integration, you can use Ansible playbooks to automate Infoblox Core Network Services for IP address management (IPAM), DNS, and inventory tracking.

You can review simple example tasks in the documentation for any of the NIOS modules or look at the Use cases with modules section for more elaborate examples. See the Infoblox website for more information on the Infoblox product.

Note

You can retrieve most of the example playbooks used in this guide from the network-automation/infoblox_ansible GitHub repository.

Prerequisites

Before using Ansible nios modules with Infoblox, you must install the infoblox-client on your Ansible control node:

  1. $ sudo pip install infoblox-client

Note

You need an NIOS account with the WAPI feature enabled to use Ansible with Infoblox.

Credentials and authenticating

To use 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:

  1. ---
  2. nios_provider:
  3. host: 192.0.0.2
  4. username: admin
  5. password: ansible

NIOS lookup plugins

Ansible includes the following lookup plugins for NIOS:

  • nios Uses the Infoblox WAPI API to fetch NIOS specified objects, for example network views, DNS views, and host records.
  • nios_next_ip Provides the next available IP address from a network. You’ll see an example of this in Creating a host record.
  • nios_next_network - Returns the next available network range for a network-container.

You must run the NIOS lookup plugins locally by specifying connection: local. See lookup plugins for more detail.

Retrieving all network views

To retrieve all network views and save them in a variable, use the set_fact module with the nios lookup plugin:

  1. ---
  2. - hosts: nios
  3. connection: local
  4. tasks:
  5. - name: fetch all networkview objects
  6. set_fact:
  7. networkviews: "{{ lookup('nios', 'networkview', provider=nios_provider) }}"
  8.  
  9. - name: check the networkviews
  10. debug:
  11. var: networkviews

Retrieving a host record

To retrieve a set of host records, use the set_fact module with the nios lookup plugin and include a filter for the specific hosts you want to retrieve:

  1. ---
  2. - hosts: nios
  3. connection: local
  4. tasks:
  5. - name: fetch host leaf01
  6. set_fact:
  7. host: "{{ lookup('nios', 'record:host', filter={'name': 'leaf01.ansible.com'}, provider=nios_provider) }}"
  8.  
  9. - name: check the leaf01 return variable
  10. debug:
  11. var: host
  12.  
  13. - name: debug specific variable (ipv4 address)
  14. debug:
  15. var: host.ipv4addrs[0].ipv4addr
  16.  
  17. - name: fetch host leaf02
  18. set_fact:
  19. host: "{{ lookup('nios', 'record:host', filter={'name': 'leaf02.ansible.com'}, provider=nios_provider) }}"
  20.  
  21. - name: check the leaf02 return variable
  22. debug:
  23. var: host

If you run this get_host_record.yml playbook, you should see results similar to the following:

  1. $ ansible-playbook get_host_record.yml
  2.  
  3. PLAY [localhost] ***************************************************************************************
  4.  
  5. TASK [fetch host leaf01] ******************************************************************************
  6. ok: [localhost]
  7.  
  8. TASK [check the leaf01 return variable] *************************************************************
  9. ok: [localhost] => {
  10. < ...output shortened...>
  11. "host": {
  12. "ipv4addrs": [
  13. {
  14. "configure_for_dhcp": false,
  15. "host": "leaf01.ansible.com",
  16. }
  17. ],
  18. "name": "leaf01.ansible.com",
  19. "view": "default"
  20. }
  21. }
  22.  
  23. TASK [debug specific variable (ipv4 address)] ******************************************************
  24. ok: [localhost] => {
  25. "host.ipv4addrs[0].ipv4addr": "192.168.1.11"
  26. }
  27.  
  28. TASK [fetch host leaf02] ******************************************************************************
  29. ok: [localhost]
  30.  
  31. TASK [check the leaf02 return variable] *************************************************************
  32. ok: [localhost] => {
  33. < ...output shortened...>
  34. "host": {
  35. "ipv4addrs": [
  36. {
  37. "configure_for_dhcp": false,
  38. "host": "leaf02.example.com",
  39. "ipv4addr": "192.168.1.12"
  40. }
  41. ],
  42. }
  43. }
  44.  
  45. PLAY RECAP ******************************************************************************************
  46. localhost : ok=5 changed=0 unreachable=0 failed=0

The output above shows the host record for leaf01.ansible.com and leaf02.ansible.com that were retrieved by the nios lookup plugin. This playbook saves the information in variables which you can use in other playbooks. This allows you to use Infoblox as a single source of truth to gather and use information that changes dynamically. See Using Variables for more information on using Ansible variables. See the nios examples for more data options that you can retrieve.

You can access these playbooks at Infoblox lookup playbooks.

Use cases with modules

You can use the nios modules in tasks to simplify common Infoblox workflows. Be sure to set up your NIOS credentials before following these examples.

Configuring an IPv4 network

To configure an IPv4 network, use the nios_network module:

  1. ---
  2. - hosts: nios
  3. connection: local
  4. tasks:
  5. - name: Create a network on the default network view
  6. nios_network:
  7. network: 192.168.100.0/24
  8. comment: sets the IPv4 network
  9. options:
  10. - name: domain-name
  11. value: ansible.com
  12. state: present
  13. provider: "{{nios_provider}}"

Notice the last parameter, provider, uses the variable nios_provider defined in the group_vars/ directory.

Creating a host record

To create a host record named leaf03.ansible.com on the newly-created IPv4 network:

  1. ---
  2. - hosts: nios
  3. connection: local
  4. tasks:
  5. - name: configure an IPv4 host record
  6. nios_host_record:
  7. name: leaf03.ansible.com
  8. ipv4addrs:
  9. - ipv4addr:
  10. "{{ lookup('nios_next_ip', '192.168.100.0/24', provider=nios_provider)[0] }}"
  11. state: present
  12. provider: "{{nios_provider}}"

Notice the IPv4 address in this example uses the nios_next_ip lookup plugin to find the next available IPv4 address on the network.

Creating a forward DNS zone

To configure a forward DNS zone use, the nios_zone module:

  1. ---
  2. - hosts: nios
  3. connection: local
  4. tasks:
  5. - name: Create a forward DNS zone called ansible-test.com
  6. nios_zone:
  7. name: ansible-test.com
  8. comment: local DNS zone
  9. state: present
  10. provider: "{{ nios_provider }}"

Creating a reverse DNS zone

To configure a reverse DNS zone:

  1. ---
  2. - hosts: nios
  3. connection: local
  4. tasks:
  5. - name: configure a reverse mapping zone on the system using IPV6 zone format
  6. nios_zone:
  7. name: 100::1/128
  8. zone_format: IPV6
  9. state: present
  10. provider: "{{ nios_provider }}"

Dynamic inventory script

You can use the Infoblox dynamic inventory script to import your network node inventory with Infoblox NIOS. To gather the inventory from Infoblox, you need two files:

  • infoblox.yaml - A file that specifies the NIOS provider arguments and optional filters.
  • infoblox.py - The python script that retrieves the NIOS inventory.

To use the Infoblox dynamic inventory script:

  • Download the infoblox.yaml file and save it in the /etc/ansible directory.
  • Modify the infoblox.yaml file with your NIOS credentials.
  • Download the infoblox.py file and save it in the /etc/ansible/hosts directory.
  • Change the permissions on the infoblox.py file to make the file an executable:
  1. $ sudo chmod +x /etc/ansible/hosts/infoblox.py

You can optionally use ./infoblox.py —list to test the script. After a few minutes, you should see your Infoblox inventory in JSON format. You can explicitly use the Infoblox dynamic inventory script as follows:

  1. $ ansible -i infoblox.py all -m ping

You can also implicitly use the Infoblox dynamic inventory script by including it in your inventory directory (etc/ansible/hosts by default). See Working With Dynamic Inventory for more details.

See also