help with 'when' please

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

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