1

I am not able to skip tasks using when conditions with loops in Ansible. I don't see any skip output in the playbook task but it says skipped in the play recap.

My playbook tasks:

    - name: Capture the Existing DHCP Config
      set_fact:
        existing_dhcp_config: "{{ output['stdout'][0] | regex_findall('forwarding-options dhcp-relay server-group site-dhcp \\S+') }}"

    - name: View the existing DHCP Config
      debug:
        var: existing_dhcp_config

    - name: Desired DHCP Config Based on Site Standards
      set_fact:
        desired_dhcp_config: "{{ lookup('file', './DHCP-Config/{{ inventory_hostname }}-dhcp.cfg') | regex_findall('forwarding-options dhcp-relay server-group site-dhcp \\S+') }}"
    
    - name: View Desired DHCP Config
      debug:
        var: desired_dhcp_config

    - name: Compare Existing and Desired DHCP Configs to Remove Stale Config
      set_fact:
        dhcp_config_to_remove: "{{ existing_dhcp_config | difference(desired_dhcp_config) }}"

    - name: View the Difference Against Existing and Desired DHCP Configs
      debug:
        var: dhcp_config_to_remove
      when: dhcp_config_to_remove != ""

    - name: Delete Non-Site Standards DHCP Config
      junos_config:
        commands: delete {{ item }}
        comment: "{{ chg_ticket }}"
        confirm_commit: yes
      loop: "{{ dhcp_config_to_remove }}"
      when: dhcp_config_to_remove != ""

    - name: Verify the Final DHCP Config on JUNOS Devices
      junos_command:
        commands: show configuration | display set
      register: final_output

    - name: View the Final DHCP Config
      debug:
        msg: "{{ final_output['stdout'][0] | regex_findall('forwarding-options dhcp-relay server-group site-dhcp \\S+') }}"

Output from the playbook:

TASK [Capture the Existing DHCP Config] ******************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]

TASK [View the existing DHCP Config] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
    "existing_dhcp_config": [
        "forwarding-options dhcp-relay server-group site-dhcp 10.1.1.2"
    ]
}

TASK [Desired DHCP Config Based on Site Standards] *******************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]

TASK [View Desired DHCP Config] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
    "desired_dhcp_config": [
        "forwarding-options dhcp-relay server-group site-dhcp 10.1.1.2"
    ]
}

TASK [Compare Existing and Desired DHCP Configs to Remove Stale Config] **********************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]

TASK [View the Difference Against Existing and Desired DHCP Configs] *************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
    "dhcp_config_to_remove": []
}

TASK [Delete Non-Site Standards DHCP Config] *************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Verify the Final DHCP Config on JUNOS Devices] *****************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]

TASK [View the Final DHCP Config] ************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
    "msg": [
        "forwarding-options dhcp-relay server-group site-dhcp 10.1.1.2"
    ]
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
junos-1                : ok=23   changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

In the following 2 tasks, I don't see any skipped descriptions in the play run even tough I have defined when statements for them:

TASK [View the Difference Against Existing and Desired DHCP Configs]
TASK [Delete Non-Site Standards DHCP Config]
2
  • 1
    There are 2 things you should check in ansible.cfg depending on your ansible version: for an old version your might have setstdout_callback = skippy (deprecated) that you can switch back to default. For more recent versions, you might have display_skipped_hosts = no that you have to turn back to yes. Jan 2, 2021 at 14:38
  • on the Ansible documentation, it says that the default behavior is display_skipped_hosts = yes, actually it is not showing skipped with only the task with loop TASK [Delete Non-Site Standards DHCP Config] and for others it is there.
    – Anonymous
    Jan 2, 2021 at 17:05

1 Answer 1

1

Those two does not have a skipped statement because they do not skip explicitly.

In

when: dhcp_config_to_remove != ""

you are checking that this variable is an empty string, which it is not, as it is an empty list:

ok: [junos-1] => {
    "dhcp_config_to_remove": []
}

So your condition is indeed not met, and the skip is actually coming from something else.

And as pointed in the comments, there is actually two possibility for an host to not show some skipped tasks:

  1. You have an explicit skip via a conditional like when, but you have display_skipped_hosts = no in your configuration
  2. You are looping on an empty list, so the task will be skipped and show no information, even with display_skipped_hosts = yes

So what you can do in here to make an explicit skip is to test the length of your list

when: dhcp_config_to_remove | length

As this test will yield a 0 for an empty list and that 0 evaluates to False; when any other number would evaluate to True, this will do the trick.


Still, when you loop on an empty list, the skip of the loop will happen before the skip of the when.

For example:

- hosts: all
  gather_facts: no

  tasks:
    - debug:
      when: false
      loop: "{{ undefined_list | default([]) }}"

Yields the recap:

PLAY [all] *******************************************************************************************************

TASK [debug] *****************************************************************************************************

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

But this is the expected behaviour.

3
  • skipped worked for the first task but remains the same for the second task, displaying an empty prompt on the task but mentions skipped in play recap.
    – Anonymous
    Jan 2, 2021 at 22:41
  • 1
    Seems indeed to be the case when you have an empty list, possibly because the loop is interpreted before the when Jan 2, 2021 at 23:06
  • Is there a way to sort that out?
    – Anonymous
    Jan 2, 2021 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.