Search code examples
ansibleyamlinfoblox

Ansible variable interpolation not working in brackets


I'm using Ansible to grab an VIP from InfoBlox and running into an issue when trying to build my exclude list. I need to exclude the first 3 IP's as they are reserved, however the CIDR range is a dynamic variable so I need to build this exclude list on the fly. Here's a representation of what I have so far:

---
- name: Assign VIP to FQDN in Grid Manager
  hosts: all
  connection: local
  gather_facts: false

  vars:
    CIDR: 10.0.0.

  tasks:

  - set_fact:
      EXCLUDES: "{{ EXCLUDES | default([]) + [item] }}"
    with_sequence: start=1 end=3 format=''{{ CIDR }}%d''

  - set_fact:
      formated_excludes: "{{ EXCLUDES |join(', ') }}"

  - debug:
      msg: "{{ formated_excludes }}"

  - set_fact:
      VIP:  "{{ lookup('nios_next_ip', '10.0.0.0/25', exclude=[formated_excludes]) | first }}"

  - name: Print next IP address
    debug:
      msg: "The next available IP is {{ VIP }}"

Here are the results of the play:

PLAY [Assign VIP to FQDN in Grid Manager] *****************************************************************************************************************

TASK [set_fact] *******************************************************************************************************************************************
ok: [localhost] => (item='10.0.0.1')
ok: [localhost] => (item='10.0.0.2')
ok: [localhost] => (item='10.0.0.3')

TASK [set_fact] *******************************************************************************************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************************************************************************
ok: [localhost] => {
    "msg": "'10.0.0.1', '10.0.0.2', '10.0.0.3'"
}

TASK [set_fact] *******************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'nios_next_ip'. Error was a <class 'ansible.errors.AnsibleError'>, original message: Invalid value for exclude: \"'10.0.0.1', '10.0.0.2', '10.0.0.3'\": Invalid IPv4 address"}

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

The formatting appears to be fine, however the lookup won't accept it. For comparison, using the following works fine:

  - set_fact:
      VIP:  "{{ lookup('nios_next_ip', '10.0.0.0/25', exclude=['10.0.0.1', '10.0.0.2', '10.0.0.3']) | first }}"

Here are the results:

PLAY [Assign VIP to FQDN in Grid Manager] *****************************************************************************************************************

TASK [set_fact] *******************************************************************************************************************************************
ok: [localhost]

TASK [Print next IP address] ******************************************************************************************************************************
ok: [localhost] => {
    "msg": "The next available IP is 10.0.0.4"
}

PLAY RECAP ************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Any thoughts on why this variable isn't working would be much appreciated!


Solution

  • Parameter exclude is "List of IP's ...", but the filter join makes a string from the list

    - set_fact:
        formated_excludes: "{{ EXCLUDES |join(', ') }}"
    

    Use the list EXCLUDES in the parameter

    - set_fact:
        VIP:  "{{ lookup('nios_next_ip', '10.0.0.0/25', exclude=EXCLUDES) | first }}"