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

31 views
Skip to first unread message

Johannes Kastl

unread,
Mar 6, 2016, 8:23:21 AM3/6/16
to ansible...@googlegroups.com
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:

#################
ssh_users:
- foo
- bar
#################

The role main.yml contains:
#################
- name: This runs if ssh_users is not set
copy:
src='{{ ansible_hostname }}'
dest='/some/path/'
when: not ssh_users

- name: This runs if there is a dict called ssh_users
copy:
src='{{ item }}'
dest='/some/path/'
with_items: "{{ ssh_users|default({}) }}"
when: ssh_users

#################

The first part is skipped if ssh_users is set, but returns an error if
it is not set. I think I might be able to use default here somehow,
but haven't figured out how. Using quoted variables does not help either.

For the second part I found out that the with_items is parsed even if
the 'when:' statement would return false, so I get an error. That
should be solvable with default somehow, but I can't get it to do what
I want.

One reason might be that I do not know if ssh_users is a dict or not.
https://docs.ansible.com/ansible/playbooks_conditionals.html
is not clear about this, neither is
https://docs.ansible.com/ansible/playbooks_variables.html#id13

I found this, but I can't adjust it to my case:
https://github.com/ansible/ansible/issues/14448

Any help would be highly appreciated, even a RTFM pointing me to the
right FM would help in this case... ;-)

Thanks,
Johannes

signature.asc

Brian Coca

unread,
Mar 6, 2016, 8:52:02 AM3/6/16
to ansible...@googlegroups.com
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/56DC2F3B.40706%40ojkastl.de.
For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca

Johannes Kastl

unread,
Mar 6, 2016, 12:56:26 PM3/6/16
to ansible...@googlegroups.com
Hi Brian,

thanks for the fast answer.

Am 06.03.16 schrieb Brian Coca:
> 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?

> On Sun, Mar 6, 2016 at 8:23 AM, Johannes Kastl <ma...@ojkastl.de> wrote:

>> The role main.yml contains:
>> #################
>> - name: This runs if ssh_users is not set
>> copy:
>> src='{{ ansible_hostname }}'
>> dest='/some/path/'
>> when: not ssh_users

>> The first part is skipped if ssh_users is set, but returns an error if
>> it is not set. I think I might be able to use default here somehow,
>> but haven't figured out how. Using quoted variables does not help either.

How to run this task is ssh_users is not defined in the hostvars?

when: "{{ ssh_users|default([X]) }} == X"

Thanks!

Johannes

signature.asc

Johannes Kastl

unread,
Mar 6, 2016, 1:36:03 PM3/6/16
to ansible...@googlegroups.com
I'll answer myself...

Am 06.03.16 schrieb Johannes Kastl:

> How to run this task is ssh_users is not defined in the hostvars?
>
> when: "{{ ssh_users|default([X]) }} == X"

For the next one searching (may be me if I forget how I did it...):

################
- name: This runs if ssh_users is not set
copy:
src='...'
dest='...'
when: ssh_users is undefined

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

Note the 1 inside the default in the second task. If I leave this
empty ...|default([]) ... there is no hint if this task is skipped.

Brian, is this somehow like it is supposed to be? Or is this breaking
in a future release of ansible?

Johannes

signature.asc

Brian Coca

unread,
Mar 7, 2016, 1:31:53 PM3/7/16
to ansible...@googlegroups.com


- 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


----------
Brian Coca

Johannes Kastl

unread,
Mar 7, 2016, 2:14:27 PM3/7/16
to ansible...@googlegroups.com
Hi Brian,

Am 07.03.16 schrieb Brian Coca:

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


signature.asc
Reply all
Reply to author
Forward
0 new messages