THE GAME HAS CHANGED

Introducing Infoblox Universal DDI ManagementTM

Watch the launch to discover the new era of management for critical network services. Watch Now

API & Integration, DevOps,NetOps,SecOps

Reply

how to exclude reserved IPs when using 'get next available IP in ansible

[ Edited ]
Techie
Posts: 9
4503     1

for providing the next_available_ip..all the 0 to 10 ips are reservers in every subnet/class ranges. i  need to do exclude these reserved ip for different class/subnet of ip ranges....

so  my plan is to pass the first 3 class of network range as external variable to exclude.

for eg. 

variable= 192.168.10 or 75.10.65

exclude=['{{variable}} .1' , '{{variable}}.2' , '{{variable}}.3',.........'{{variable}}.10']

 

Getting below error when i try to pass the exclue (reserved) ips as variable in ansible lookup module and also in  nios_a_record/nios_host_record modules..

 

vars:
nxt_ip_ntw: "192.168.10.0/24"
tasks:

- name: set fact for the exclude ip class
set_fact:
ip_class: "{{nxt_ip_ntw[:-4]}}"

- name: setfact the exclude as variable
set_fact:
ex_var: "{{'{{ip_class}}1', '{{ip_class}}2', '{{ip_class}}3', '{{ip_class}}4', '{{ip_class}}5', '{{ip_class}}6', '{{ip_class}}7', '{{ip_class}}8', '{{ip_class}}9', '{{ip_class}}10'}}"

 

: ERROR:

fatal: [localhost]: FAILED! => {"msg": "template error while templating string: invalid syntax for function call expression. String: {{ lookup ( 'nios_next_ip', '{{nxt_ip_ntw}}', num=20, 'exclude=[{{ex_var}}]', provider=nios_provider) }}"}

 

how can i pass the exclude as variable please help for API method and also for Ansible lookup method.

 

 

 

Working method in ansible is need to pass as list of item for exclude

- name: return the next 3 available IP addresses for network 192.168.10.0/24 excluding ip addresses - ['192.168.10.1', '192.168.10.2']
  ansible.builtin.set_fact:
    ipaddr: "{{ lookup('community.general.nios_next_ip', '{{nxt_ip_ntw}}', num=3, exclude=['192.168.10.1', '192.168.10.2'],
                provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
var:
nxt_ip_ntw: "192.168.10.0/24"

 

 

Re: how to exclude reserved IPs when using 'get next available IP in ansible

New Member
Posts: 5
4503     1

Hi Gany,

 

The error occurs because of single quotes inserted on the "exclude" parameter of the lookup call.  A simpler solution to the problem is to pass the ex_var as a list variable directly into the lookup call.

 

You can try this:

  vars:
    nxt_ip_ntw: "192.168.10.0/24"

  tasks:
    - name: set fact for the exclude ip class
      set_fact:
        ip_class: "{{nxt_ip_ntw[:-4]}}"

    - name: setfact the exclude as variable
      set_fact:
        ex_var: "['{{ip_class}}1', '{{ip_class}}2', '{{ip_class}}3', '{{ip_class}}4', '{{ip_class}}5', '{{ip_class}}6', '{{ip_class}}7', '{{ip_class}}8', '{{ip_class}}9', '{{ip_class}}10']"

    - name: ansible NIOS call
      set_fact:
        ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '{{nxt_ip_ntw}}', num=3, exclude=ex_var, provider={'host': '10.196.205.100', 'username': 'admin', 'password': 'infoblox'}) }}"

 

We eliminate the syntax error by not using "{{ }}" repeatedly. 

 

Hope this helps. Let me know if you need anything else.

Thanks and Regards

Vedant Sethia

Re: how to exclude reserved IPs when using 'get next available IP in ansible

Techie
Posts: 9
4504     1

Thanks Vedanth. its working Smiley Happy

Showing results for 
Search instead for 
Did you mean: 

Recommended for You