1

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.

  • do a register to capture the stdout of the task and show what you have when name exists and dont exist..fyi you could add check_mode: yes to just check your output but nothings is changed – Frenchy Oct 20 '21 at 14:35
  • not sure the check_mode: yes is functional with linux command, is there a command which could indicate if pool exists? maybe Sh IP DHCP Pool? or Show IP DHCP Pool? – Frenchy Oct 20 '21 at 15:03
  • Yes we can use the "show ip dhcp pool " to check if it exist – Cheikh Ahmed Tidiane FALL Oct 20 '21 at 15:10
  • so you do a task with this command before and you register the output, so you can know all pool which exist..and put them in list if you need help show your stdout of the task – Frenchy Oct 20 '21 at 15:12
  • ```[ "ip dhcp pool VOICEC_DHCP", "ip dhcp pool DATA", "ip dhcp pool VIDEO_DHCP ", "ip dhcp pool WIFI_USER", " ip dhcp pool WIFI_ADM", ]``` ` this is the output of the command – Cheikh Ahmed Tidiane FALL Oct 21 '21 at 08:39

1 Answers1

0

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 }}"
Frenchy
  • 16,386
  • 3
  • 16
  • 39