# help with 'when' please

**URL:** <https://forum.ansible.com/t/help-with-when-please/30823>\
**Category:** Ansible Project\
**Created:** [July 19, 2019, 10:47pm UTC](https://forum.ansible.com/t/help-with-when-please/30823 "2019-07-19T22:47:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![william.dossett](https://avatars.discourse-cdn.com/v4/letter/w/b782af/32.png) [@william.dossett](https://forum.ansible.com/u/william.dossett)\
**Post date:** [July 19, 2019, 10:47pm UTC](https://forum.ansible.com/t/help-with-when-please/30823/1 "2019-07-19T22:47:12Z")

</div>

Try and keep this short and simple - I obviously don’t know how to use when I guess…

so I do some stuff trying users to login to linux instances from a list of users.

`

- centos
- ec2-user
- somotheruser

`

I have the cumulative results registered. Then I try to run this task:

`

- name: assign to remote user  
remote\_user: “{{ item [‘user’] }}”

# so the problem is that when the below evaluates, item[‘user’] is still centos

when: item[‘ping’] is defined  
with\_items: “{{result.results}}”  
user:  
name: bmcdisc  
password: “lestsayitsasecret”

`

so my ‘when’ tests if ping is defined in the result.results because it is on the attempted connection that succeeds.

But the problem is that the ec2-user succeeds… but when I run that task, it says it is skipping the first and last as they are not defined, but running with -vvv I see

`

\<10.5.162.167\> ESTABLISH SSH CONNECTION FOR USER: centos

`

so it seems like I don’t have ‘when’ in the right place (i have tried moving it up and down etc, no luck), or don’t understand how it works or something - I am on

ansible 2.9.0.dev0

as the kind ansible folks fixed the issue with trying to assign users to remote\_user for me this week and just merged the code so that is working.

Can anyone tell me why?

Thank you

Bill

---

<div class="post-metadata">

**Author:** ![Ehab\_Arman](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ehab\_Arman](https://forum.ansible.com/u/Ehab_Arman)\
**Post date:** [July 20, 2019, 6:55pm UTC](https://forum.ansible.com/t/help-with-when-please/30823/2 "2019-07-20T18:55:17Z")

</div>

Hi William,

The when location inside the task won’t change how it works, It will always check the condition first before executing the task.

I’m not sure what you stored inside your register, it differs according to the task used. Try to use debug to check the content of your register

make sure it has reg.results.ping stored first
