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

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

HI

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

secondary_groups:

Phil

Hi Phil,

Thank you very much for your answer :slight_smile:

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

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

yep

Phil

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 :slight_smile:

Thanks in advance,
Esteban

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 :slight_smile:

Cheers,
Esteban

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.

Hi Brian,

Thank you very much for letting me know :slight_smile:

Cheers,
Esteban