Today we are going to use Ansible to create DHCP Ranges in Infoblox. If you haven’t done so, please review the previous post on “Getting started with Infoblox and Ansible.” We are going to create a new container “10.1.0.0/16”; two new templates, one for “Range Template” and the second for “Network Template” with “Range Template” added to it.
Let’s create the Network and DHCP Range templates
Create a /16 in the UI of Infoblox
Create a DHCP Range Template
Create a Network Template
Now that we have created 10.1.0.0/16 container and Templates, let’s work on our Ansible playbook.
Ansible, Infoblox, and URI
We are going to use the URI module since the current Ansible module does not support DHCP range creation with DHCP Failover. Let’s get started with URI module and the Infoblox “func:nextavailablenetwork” API call.
Create a new YAML playbook and call it “uri1.yml” (again I’m not that creative):
---
- hosts: localhost
connection: local
tasks:
- name: Get the next available network from 10.1.0.0/16
uri:
url: "https://192.168.0.200/wapi/v2.7/network"
method: POST
user: admin
password: infoblox
status_code: 201, 302, 200
headers:
Content-Type: "application/json"
body:
network: "func:nextavailablenetwork:10.1.0.0/16,default,24"
body_format: json
validate_certs: no
return_content: yes
register: data
- name: Display new network
debug:
var: data.json
Let me explain the above code:
- Line 8 – this is the URL we are going to use to make the API call to get the network from my GM (192.168.0.200)
- Line 9 – method POST – this will create a new network
- Line 10 and 11 – Username and Password, this is usually a bad practice but I’m using it as an example. You can find additional information here about using Ansible Vault
- Line 16 – We are going to use the “Function” called “next available network” in WAPI. This will ask for the next network and return it for us to use.

Let’s run our playbook and look at the results:
$ ansible-playbook uri1.yml
PLAY [localhost] *********************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************
ok: [localhost]
TASK [Get the next available network from 10.1.0.0/16] *******************************************************************
ok: [localhost]
TASK [Display new network] ***********************************************************************************************
ok: [localhost] => {
"data.json": "network/ZG5zLm5ldHdvcmskMTAuMS4xLjAvMjQvMA:10.1.1.0/24/default"
}
PLAY RECAP ***************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
NOTE: the above it gives us “10.1.1.0/24“, let’s check what the UI looks like:

Let’s now let’s use our Infoblox “templates” to create Network and DHCP Ranges with Ansible Playbook
If you haven’t already done so, please follow the videos above to create the “templates” we are going to use in the next playbook.
Let’s create a new Ansible Playbook for creating a Network and DHCP range in one playbook name it “create_a_new_dhcp_range.yml”:
---
- hosts: localhost
connection: local
tasks:
- name: Get the next available network from 10.1.0.0/16
uri:
url: "https://192.168.0.200/wapi/v2.7/network"
method: POST
user: admin
password: infoblox
status_code: 201, 302, 200
headers:
Content-Type: "application/json"
body:
network: "func:nextavailablenetwork:10.1.0.0/16,default,24"
template: "Network24User"
body_format: json
validate_certs: no
return_content: yes
register: data
- name: Network and DHCP range created with Template Network24User
debug:
var: data.json
Notice the playbook is almost the same except for line 17, where we added “template” to the “body” of the API call. Let’s run it and see the results:
$ ansible-playbook create_a_new_dhcp_range.yml
PLAY [localhost] ************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************
ok: [localhost]
TASK [Get the next available network from 10.1.0.0/16] **********************************************************
ok: [localhost]
TASK [Network and DHCP range created with Template Network24User] ***********************************************
ok: [localhost] => {
"data.json": "network/ZG5zLm5ldHdvcmskMTAuMS4zLjAvMjQvMA:10.1.3.0/24/default"
}
PLAY RECAP ******************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The above shows that “10.1.3.0/24” was created, so let’s see what the UI shows:

Now let’s take a look at the DHCP Range:

Unfortunately, we have a “glitch” in our full automation of a network with DHCP Range. We have to restart services, so let’s add the option to do that to our playbook. Edit the above playbook to look like below:
---
- hosts: localhost
connection: local
tasks:
- name: Get the next available network from 10.1.0.0/16
uri:
url: "https://192.168.0.200/wapi/v2.7/network"
method: POST
user: admin
password: infoblox
status_code: 201, 302, 200
headers:
Content-Type: "application/json"
body:
network: "func:nextavailablenetwork:10.1.0.0/16,default,24"
template: "Network24User"
body_format: json
validate_certs: no
return_content: yes
register: data
- name: Network and DHCP range created with Template Network24User
debug:
var: data.json
- name: Restart Services
uri:
url: "https://192.168.0.200/wapi/v2.7/grid/b25lLmNsdXN0ZXIkMA:Infoblox?_function=restartservices?services=DHCP"
method: POST
user: admin
password: infoblox
status_code: 201, 302, 200
validate_certs: no
We added a second task that will restart services in Infoblox. Now let’s run this playbook and see the results:
$ ansible-playbook create_a_new_dhcp_range.yml
PLAY [localhost] ************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************
ok: [localhost]
TASK [Get the next available network from 10.1.0.0/16] **********************************************************
ok: [localhost]
TASK [Network and DHCP range created with Template Network24User] ***********************************************
ok: [localhost] => {
"data.json": "network/ZG5zLm5ldHdvcmskMTAuMS40LjAvMjQvMA:10.1.4.0/24/default"
}
TASK [Restart Services] ***************************************************************************************
ok: [localhost]
PLAY RECAP ******************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
As we can see, the playbook completed without any issues. Let’s log into the UI and see if we have to restart services:

Conclusion
As you can see, it’s easy to create “Networks” and “DHCP Ranges” in Infoblox with Ansible playbooks.
Files are located here: GitHub
Recent Comments