Conditional expression if alice in ['alice', 'bob']

Hi all

So close.

I’m trying to set up users by passing a list of users and a hash of user configs.

#group_vars/all
user_definitions:

  • username: alice
    uid: 1101
  • username: bob
    uid: 1102

#group_vars/web
install_users:

  • alice
  • bob

#main.yml

  • name: Install users
    user: name={{item.username}}
    when: ${item.username} in $install_users
    with_items: user_definitions

fatal: [vbox1.example.com] => Conditional expression must evaluate to True or False: {% if alice in [‘alice’, ‘bob’] %} True {% else %} False {% endif %}

I do not understand what the problem is. From the looks of the error, it looks like the first alice is a variable and the alice and bob are strings.

Any idea what I’m doing wrong?
It feels like it should be something small.

Thanks
Deon

You should never use legacy variables anymore except in one very rare occasion (needing deep references to things in defining other variables – the need for this even much less so on 1.4), but especially should not use them in “when” expressions.

when: item.username in install_users

much simpler!

You will notice the documentation does not make use of $foo when showing examples, so it can be a great reference.

http://ansibleworks.com/docs/playbooks_conditionals.html#id10