# when: state is present from variable dict hash list

**URL:** <https://forum.ansible.com/t/when-state-is-present-from-variable-dict-hash-list/20917>\
**Category:** Ansible Project\
**Created:** [March 18, 2016, 12:59pm UTC](https://forum.ansible.com/t/when-state-is-present-from-variable-dict-hash-list/20917 "2016-03-18T12:59:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Short](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Short](https://forum.ansible.com/u/Chris_Short)\
**Post date:** [March 18, 2016, 12:59pm UTC](https://forum.ansible.com/t/when-state-is-present-from-variable-dict-hash-list/20917/1 "2016-03-18T12:59:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Uditha\_Desilva](https://avatars.discourse-cdn.com/v4/letter/u/f475e1/32.png) [@Uditha\_Desilva](https://forum.ansible.com/u/Uditha_Desilva)\
**Post date:** [March 18, 2016, 1:34pm UTC](https://forum.ansible.com/t/when-state-is-present-from-variable-dict-hash-list/20917/2 "2016-03-18T13:34:37Z")

</div>

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

$ cat tst.yml

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 18, 2016, 1:48pm UTC](https://forum.ansible.com/t/when-state-is-present-from-variable-dict-hash-list/20917/3 "2016-03-18T13:48:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chris\_Short](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Short](https://forum.ansible.com/u/Chris_Short)\
**Post date:** [March 18, 2016, 1:57pm UTC](https://forum.ansible.com/t/when-state-is-present-from-variable-dict-hash-list/20917/4 "2016-03-18T13:57:28Z")

</div>

Thank you, bcoca. Helpful as always!
