when: state is present from variable dict hash list

I have the following defined in group_vars and I’m trying to run a task when the state is defined as present and the syntax is baffling me:

staff:

  • { netid: user001, uid: 12345, comment: “Chris Short”, state: present }

  • { netid: user002, uid: 12346, comment: “Bob Jones”, state: absent }

The play is like this:

  • name: Do Something
    shell: script.sh
    with_items:
  • “{{ oristaff }}”
    when: ori_staff.state == ‘present’

I’ve tried several different ways to ascertain state and none of them have worked so far.

You may have a typo or 2 in your sample case. This works fine for me:

$ cat tst.yml

you are referencing it incorrectly, staff is a list not a dictionary, also I think you want the “current item’s state”:

when: item.state == ‘present’

assuming you want to execute the task only for those entries that have that state.

Thank you, bcoca. Helpful as always!