# how to execute a task/action just when a list of items really exist

**URL:** <https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346>\
**Category:** Ansible Project\
**Created:** [May 29, 2015, 1:50pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346 "2015-05-29T13:50:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [May 29, 2015, 1:50pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/1 "2015-05-29T13:50:27Z")

</div>

Hello all,

I am using ansible for automate the users creation in one of our tools so I have the following users file in vars with the user’s info and also, the user can have some secondary groups assigned besides the main group:

[root@admin oort]# cat roles/ON4\_project\_managment/vars/users.yml

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [May 30, 2015, 8:37am UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/2 "2015-05-30T08:37:07Z")

</div>

HI

you need an empty list  
something along the following should help:

secondary\_groups:

Phil

---

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [May 30, 2015, 1:51pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/3 "2015-05-30T13:51:57Z")

</div>

Hi Phil,

Thank you very much for your answer 🙂

Unfortunatelly, I cannot try it until Monday but I will let you know if it works. Also, I understand that I must set up secondary\_groups: in the user.yml file situated on the vars directory when an user does not have secondary groups assigned, right? or am I understand it wrong?

Thanks in advance,  
Esteban

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [May 30, 2015, 1:58pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/4 "2015-05-30T13:58:27Z")

</div>

I meant, in your case with a declaration like  
secondary\_groups:

it is defined and is none

so one possibility is to check against none

another possibility with

secondary\_groups:

you can check against empty list

HTH

Phil

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [May 30, 2015, 1:59pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/5 "2015-05-30T13:59:27Z")

</div>

yep

Phil

---

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [May 30, 2015, 2:44pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/6 "2015-05-30T14:44:33Z")

</div>

Hi Phil,

Thank you very much for your help!

On Monday, I will try to declare **secondary\_groups: []** and I will also try put this conditional **when: item.1 != “”** because I only want that the action/command is just executed when secondary\_groups really contains some secondary groups to assign to the users.

I will let you know by Monday 🙂

Thanks in advance,  
Esteban

---

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [June 1, 2015, 12:33pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/7 "2015-06-01T12:33:56Z")

</div>

Hi Phil,

I tried what you said, I declared secondary groups variable as **secondary\_groups: []** in the users.yml file at the vars directory and then, on the main file for the task, I just defined **when: (item.1 != “”)** and indeed, action is only executed when the secondary groups were assigned for the user and if the user does not have any secondary group assigned it does not do anything, what it is expected.

Thank you very much for your help 🙂

Cheers,  
Esteban

---

<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:** [June 1, 2015, 3:54pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/8 "2015-06-01T15:54:38Z")

</div>

actually, you won't be hitting that when condition, when with\_ detects  
an empty list it just skips the task. the when: is redundant and would  
actually fail as there is no item.1 defined.

---

<div class="post-metadata">

**Author:** ![Esteban\_Freire](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Esteban\_Freire](https://forum.ansible.com/u/Esteban_Freire)\
**Post date:** [June 2, 2015, 7:15am UTC](https://forum.ansible.com/t/how-to-execute-a-task-action-just-when-a-list-of-items-really-exist/18346/9 "2015-06-02T07:15:29Z")

</div>

Hi Brian,

Thank you very much for letting me know 🙂

Cheers,  
Esteban
