Issues with 'when' filter on with_items loop

27 views
Skip to first unread message

Jon Langemak

unread,
Nov 2, 2016, 12:26:59 PM11/2/16
to Ansible Project
Hi All, been tearing my hair out this AM on this one so I think Im just doing something wrong.  Long story short, I have two lists in my playbook.  One that contains a set of commands from a device and another that contains the set of commands I wish to configure.  What I'd like to do is diff them to come up with which commands should not be present on the device.  In other words, Im comparing the active state of the device to the configuration and getting the delta.  This seems to be working well and Im able to generate both lists and compare them using the jinja filter 'difference'.  Here's a sample playbook...

---
- hosts: 127.0.0.1
  connection: local
  gather_facts: false
  vars:
    device_config:
      - "set vlan name test1"
      - "set vlan name test2"
      - "set hostname testdevice4"
    intended_config:
      - "set vlan name test1"
      - "set vlan name test3"
      - "set hostname testdevice6"

  tasks:
    - name: debug list differences
      debug:
        var: "{{item}}"
      with_items:
          device_config | difference(intended_config)


The output of this is a list of commands that are present in the device_config list, but not in the intended_config.  This works great!  However, I want to filter this output to only include items that are of a particular type.  So for instance, assume that I change the task to look like this...
  tasks:
    - name: debug list differences
      debug:
        var: "{{item}}"
      when:
        '"vlan" in item'
      with_items:
          device_config | difference(intended_config)

In this case, I want to only return lines that are different, and include the string 'vlan'.  However, when I run this, the playbook comes back and tells me that the step was skipped...
"skip_reason": "Conditional check failed",

So I can't sort this out.  I know that typically you use 'in' to search for an entire match of a list item, but I swear I've done this before and it works.  Any ideas on what Im doing wrong?

Thanks!


Reply all
Reply to author
Forward
0 new messages