Need help with conditionals and a dict (?) or variable...

Hi everyone,

I need help getting a role to run. Especially the part where one host
has a list of users defined, and the other host has not.

In the host_vars for hostA I have this:

you made the default dict, it should be a list

with_items: “{{ ssh_users|default() }}”

ssh_users is already a list so no problem there.

Hi Brian,

thanks for the fast answer.

you made the default dict, it should be a list

  with_items: "{{ ssh_users|default() }}"

ssh_users is already a list so no problem there.

Sorry, but I'm not sure I understand what you mean. Do you mean that
my ssh_users is a list and not a dict? Dict being somewhere along the
lines of:

ssh_users
  - user1: foo
  - user2: bar

Using 'default{()}' returns a dict, not a list, is that what you meant?

I'll answer myself...

- name: This runs if there is something ssh_users
  copy:
    src='...'
    dest='...'
  with_items: "{{ ssh_users|default([1]) }}"
  when: ssh_users is defined
################

​remove the when: it executes PER item, in the default you are forcing 1
item and then skipping it.


  with_items: "{{ ssh_users|default() }}"

​ssh_users is a list:

ssh_users:
  - user1: foo
  - user2: bar

this is how it would be for a dict:

ssh_users:
  user1: foo
  user2: bar

(no -, which signifies 'list item')

​^ this will skip the task due to the loop list being empty

Hi Brian,

- name: This runs if there is something ssh_users
  copy:
    src='...'
    dest='...'
  with_items: "{{ ssh_users|default([1]) }}"
  when: ssh_users is defined
################

​remove the when: it executes PER item, in the default you are forcing 1
item and then skipping it.

If I leave out the 'when'-statement (and use '...|default{}' without
the 1), I do not get any hint in the ansible-playbook output whether
this task is skipped or not.

​ssh_users is a list:

ssh_users:
  - user1: foo
  - user2: bar

this is how it would be for a dict:

ssh_users:
  user1: foo
  user2: bar

(no -, which signifies 'list item')

OK, now I got it. Thanks!

​^ this will skip the task due to the loop list being empty

I am not sure where this line belongs to, but I assume my example is
not totally wrong and will not break with the next release, so Im good.

Thanks, Brian!

Johannes