Search code examples
ansiblenetwork-programmingansible-templatecisco-ios

How to ensure that the dhcp pool name exist before updating options using ansible


Here is my playbook. When I execute it, the command ip dhcp pool {{ item.name }} don't check if the name exist or not. I use the parameter "match: exact" but it doesn't work. So can I use if statement in ansible playbook or is there a way to check the pool name before execute commands. I use when in the playbook to check if item.name is defined but it also don't work.


---
- name: "UPDATE DHCP OPTIONS FOR FNAC-FRANCHISE SWITCHES"
  hosts: all
  gather_facts: false
  vars:
    xx: ["ip dhcp pool VOICEC_DHCP","ip dhcp pool DATA","ip dhcp pool VIDEO_DHCP ","ip dhcp pool WIFI_USER"," ip dhcp pool WIFI_ADM", ]

  tasks:
    - name: "CHECK"
      ios_command:
        commands:
          - show run | include ip dhcp pool
      register: output

    - name: DISPLAY THE COMMAND OUTPUT
      debug:
        var: output.stdout_lines

    - name: transform output
      set_fact:
        pools: "{{ item | regex_replace('ip dhcp pool ', '') }}"
      loop: "{{ output.stdout_lines }}"

    - name: "UPDATE DHCP OPTIONS IN POOL DATA & WIFI_USER"
      ios_config:
        lines:
          - dns-server 10.0.0.1
          - netbios-name-server 10.0.0.1
          - netbios-node-type h-node
        parents: ip dhcp pool {{ item.name }}
        match: exact
      loop: "{{ pools }}"

Here is the output that I have

ok: [ITG] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "stdout": ["ip dhcp pool VIDEO_DHCP\nip dhcp pool WIFI_ADM\nip dhcp pool VOICEC_DHCP\nip dhcp pool DATA\nip dhcp pool WIFI_USER"], "stdout_lines": [["ip dhcp pool VIDEO_DHCP", "ip dhcp pool WIFI_ADM", "ip dhcp pool VOICEC_DHCP", "ip dhcp pool DATA", "ip dhcp pool WIFI_USER"]]}

TASK [DISPLAY THE COMMAND OUTPUT] *********************************************************************************************************************************************
ok: [ITG] => {
    "output.stdout_lines": [
        [
            "ip dhcp pool VIDEO_DHCP",
            "ip dhcp pool WIFI_ADM",
            "ip dhcp pool VOICEC_DHCP",
            "ip dhcp pool DATA",
            "ip dhcp pool WIFI_USER"
        ]
    ]
}

TASK [transform output] *******************************************************************************************************************************************************
ok: [ITG] => (item=[u'ip dhcp pool VIDEO_DHCP', u'ip dhcp pool WIFI_ADM', u'ip dhcp pool VOICEC_DHCP', u'ip dhcp pool DATA', u'ip dhcp pool WIFI_USER']) => {"ansible_facts": {"pools": ["VIDEO_DHCP", "WIFI_ADM", "VOICEC_DHCP", "DATA", "WIFI_USER"]}, "ansible_loop_var": "item", "changed": false, "item": ["ip dhcp pool VIDEO_DHCP", "ip dhcp pool WIFI_ADM", "ip dhcp pool VOICEC_DHCP", "ip dhcp pool DATA", "ip dhcp pool WIFI_USER"]}

There are switches which pool name WIFI_USER doesn't exist and I don't want to create it in the switch if the line "parents: ip dhcp pool {{ item.name }}" does not match.


Solution

  • if i simulate your return value:

    - name: vartest
      hosts: localhost
      vars: #d use just to test
        xx: ["ip dhcp pool VOICEC_DHCP","ip dhcp pool DATA","ip dhcp pool VIDEO_DHCP ","ip dhcp pool WIFI_USER"," ip dhcp pool WIFI_ADM", ]    
    
      tasks:
        - name: trap output
          ios_config:
          parents: show ip dhcp pool
          register: output
    
        - name: transform output
          set_fact:
            pools: "{{ pools | default([]) + [item | regex_replace('ip dhcp pool ', '')] }}"
          loop: "{{ output.stdout_lines[0]  }}"  #you adapt following the result output.stdout or something else
    
       - name: "UPDATE DHCP OPTIONS IN POOL DATA & WIFI_USER"
         ios_config:
           commands:
             - dns-server <ip-addr>
             - netbios-name-server <ip-addr>
             - netbios-node-type h-node
           parents: ip dhcp pool {{ item }}
         loop: "{{ pools }}"