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.
Before using Ansible nios
modules with Infoblox, you must install the infoblox-client
on your Ansible control node:
$ sudo pip install infoblox-client
Note
You need an NIOS account with the WAPI feature enabled to use Ansible with Infoblox.
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:
--- nios_provider: host: 192.0.0.2 username: admin password: ansible
Ansible includes the following lookup plugins for NIOS:
You must run the NIOS lookup plugins locally by specifying connection: local
. See lookup plugins for more detail.
To retrieve all network views and save them in a variable, use the set_fact module with the nios lookup plugin:
--- - hosts: nios connection: local tasks: - name: fetch all networkview objects set_fact: networkviews: "{{ lookup('nios', 'networkview', provider=nios_provider) }}" - name: check the networkviews debug: var: networkviews
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:
--- - hosts: nios connection: local tasks: - name: fetch host leaf01 set_fact: host: "{{ lookup('nios', 'record:host', filter={'name': 'leaf01.ansible.com'}, provider=nios_provider) }}" - name: check the leaf01 return variable debug: var: host - name: debug specific variable (ipv4 address) debug: var: host.ipv4addrs[0].ipv4addr - name: fetch host leaf02 set_fact: host: "{{ lookup('nios', 'record:host', filter={'name': 'leaf02.ansible.com'}, provider=nios_provider) }}" - name: check the leaf02 return variable debug: var: host
If you run this get_host_record.yml
playbook, you should see results similar to the following:
$ ansible-playbook get_host_record.yml PLAY [localhost] *************************************************************************************** TASK [fetch host leaf01] ****************************************************************************** ok: [localhost] TASK [check the leaf01 return variable] ************************************************************* ok: [localhost] => { < ...output shortened...> "host": { "ipv4addrs": [ { "configure_for_dhcp": false, "host": "leaf01.ansible.com", } ], "name": "leaf01.ansible.com", "view": "default" } } TASK [debug specific variable (ipv4 address)] ****************************************************** ok: [localhost] => { "host.ipv4addrs[0].ipv4addr": "192.168.1.11" } TASK [fetch host leaf02] ****************************************************************************** ok: [localhost] TASK [check the leaf02 return variable] ************************************************************* ok: [localhost] => { < ...output shortened...> "host": { "ipv4addrs": [ { "configure_for_dhcp": false, "host": "leaf02.example.com", "ipv4addr": "192.168.1.12" } ], } } PLAY RECAP ****************************************************************************************** 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.
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.
To configure an IPv4 network, use the nios_network module:
--- - hosts: nios connection: local tasks: - name: Create a network on the default network view nios_network: network: 192.168.100.0/24 comment: sets the IPv4 network options: - name: domain-name value: ansible.com state: present provider: "{{nios_provider}}"
Notice the last parameter, provider
, uses the variable nios_provider
defined in the group_vars/
directory.
To create a host record named leaf03.ansible.com
on the newly-created IPv4 network:
--- - hosts: nios connection: local tasks: - name: configure an IPv4 host record nios_host_record: name: leaf03.ansible.com ipv4addrs: - ipv4addr: "{{ lookup('nios_next_ip', '192.168.100.0/24', provider=nios_provider)[0] }}" state: present 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.
To configure a forward DNS zone use, the nios_zone
module:
--- - hosts: nios connection: local tasks: - name: Create a forward DNS zone called ansible-test.com nios_zone: name: ansible-test.com comment: local DNS zone state: present provider: "{{ nios_provider }}"
To configure a reverse DNS zone:
--- - hosts: nios connection: local tasks: - name: configure a reverse mapping zone on the system using IPV6 zone format nios_zone: name: 100::1/128 zone_format: IPV6 state: present provider: "{{ nios_provider }}"
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:
To use the Infoblox dynamic inventory script:
infoblox.yaml
file and save it in the /etc/ansible
directory.infoblox.yaml
file with your NIOS credentials.infoblox.py
file and save it in the /etc/ansible/hosts
directory.infoblox.py
file to make the file an executable:$ 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:
$ 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
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.9/scenario_guides/guide_infoblox.html