when ... is defined not woring as expected?

26 views
Skip to first unread message

Kai

unread,
Dec 12, 2016, 9:18:42 AM12/12/16
to Ansible Project
Hi there!

ansible-playbook (v. 2.2.0.0) throws an error message that I do not
understand in my conext: "'pkg_repo_list' is undefined"

The task reads:
----------------------------------------
- name: ensure defined apt keys
apt_key:
state: present
id: "{{pkg_item.id}}"
keyserver: "{{pkg_item.server}}"
with_items: "{{pkg_repo_list.repo_keys}}"
loop_control:
loop_var: pkg_item
become: True
when: (pkg_repo_list is defined) and (pkg_repo_list.repo_keys is
defined)
----------------------------------------

The task is the first of two tasks enclosed in a block:
- block:
[...]
when: pkg_repo_list is defined

(Because of this conditioned block the "when" in the tasks first read
"when: pkg_repo_list.repo_keys is defined", which resulted in the same
error message.)

Shouldn't this task be skipped (in fact: the complete block) and thus
result in no error? What is it that I do not seen here?

Cheers, Kai

Brian Coca

unread,
Dec 12, 2016, 9:23:16 AM12/12/16
to ansible...@googlegroups.com
when executes for each item in the loop and cannot short circuit the with_ as documented here http://docs.ansible.com/ansible/playbooks_conditionals.html#loops-and-conditionals


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

Kai

unread,
Dec 12, 2016, 9:53:22 AM12/12/16
to ansible...@googlegroups.com
Hi Brian,

thanks a lot for the pointer / the solution. A double-default does the
trick in this case :)

with_items: "{{ (pkg_repo_list|default({})).repo_keys | default([]) }}"

Cheers, Kai
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CACVha7fymop6Sn%2BM7bSX6Ew%2B5xPfo3r8dGnkxXQGvosm7hLsDA%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CACVha7fymop6Sn%2BM7bSX6Ew%2B5xPfo3r8dGnkxXQGvosm7hLsDA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages