On 23. des. 2016 22:13, Spike wrote:
> thanks for the explanation Kai, makes sense, it effectively does the same
> thing I've done by setting a default value, but indeed I didn't understand
> how when worked in conjunction with the with_items.
>
> Here's however something else related that's happening and I'm not
> understanding: why does this happens also if I move the task in a block and
> apply the when to that?
You should really read the documentation, it's documented.
https://docs.ansible.com/ansible/playbooks_blocks.html
> * - block:*
> * - name: install sw requested for this box*
> * apt:*
> * name: "{{item}}"*
> * state: present*
> * install_recommends: yes*
> * with_items: "{{additional_sw}}"*
> * when: additional_sw is defined*
>
> shouldn't the task only be considered if the block is executed? why in this
> case i'm still getting the same error? and in fact, I also see the problem
> elsewhere with an include, which is even stranger:
All the tasks in a block inherit the when, so it's the same as adding a
when on all the task.
> *#tasks/main.yml*
> * - include: interfaces.yml*
> * when: server_interfaces is defined*
> * tags:*
> * - netwrk*
>
> *#tasks/interfaces.yml*
>
> * - name: generate new interfaces file*
> * template:*
> * src: interfaces.j2*
> * dest: /etc/network/interfaces*
> * owner: root*
> * group: adm*
> * mode: 0640*
> * notify:*
> * - restart network*
>
> * - name: setup rt_tables*
> * lineinfile:*
> * dest: /etc/iproute2/rt_tables*
> * regexp: "^{{
item.id <
http://item.id>}} "*
> * line: "{{
item.id <
http://item.id>}} {{item.tname}}"*
> * with_items: "{{server_rt_tables}}"*
> * when: server_rt_tables is defined*
>
> If I don't define server_interfaces I would expect the interfaces.yml to be
> skipped. Is that not correct? Instead what I'm seeing is the "generate new
> interfaces" task being skipped while the setup rt_tables throws the same
> error. It seems like the tasks are "interpreted" even tho they are supposed
> to be skipped, which doesn't make sense to me.
You should read the entire page that I linked to in my previous mail
https://docs.ansible.com/ansible/playbooks_conditionals.html
All the task in the include inherits the when.
So all task is executed with the when applied, it's same as manually
adding when to every single task.
> any thoughts?
When alway get applied to the tasks, not the include or the block.
And the Ansible documentation is pretty good, I do recommend reading it,
you will pick up many useful things :-)
--
Kai Stian Olstad