alternative for loop?

23 views
Skip to first unread message

Dick Visser

unread,
Nov 1, 2022, 7:15:08 AM11/1/22
to ansible...@googlegroups.com
Hi

I have a playbook that displays a warning when certain legacy variables are found.
This works, but I am looking for an alternative that does not involve a looped task.
My attempt below works only partly, because select('defined') does not work for a nested variable.
What could be a solution?

---
- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Find legacy vars (with loop)
      debug:
        msg: "Found legacy var {{ item }}"
      loop: "{{ legacy }}"
      when: vars | json_query(item)

    - name: Find legacy vars (without loop)
      debug: var=vars|select('defined')|intersect(legacy)

  vars:
    foo1: asdfasdfas
    foo2: k2jl34k234
    foo3: asdfasdfasf
    foo4: 2aslfdkjasdf
    foo:
      bar:
        baz: argsd2
    bar1: 3asdfkjlasdf
    bar2: zzzuahys
   
    legacy:
      - foo1
      - foo2
      - bar3
      - foo.bar.baz
      - foo6
Reply all
Reply to author
Forward
0 new messages