Reputation: 38949
I have a conditionally run role in a playbook. Like this:
- { role: some_role, when: "some_var == 'blah'" }
It's annoying that even though the conditional is on the role, ansible still decides to go through every task in the role and mention:
TASK: [some_role | do something] ********************************
skipping: [some_host]
Why it needs to go into each task in the role is beyond me, but that's been something I've just lived with. Until now. I just added a task to some_role
that looks like this:
- name: do something
some_module:
something: "{{ item.0.some_sub_attr }}"
something_else: "{{ item.1 }}"
with_subelements:
- elem1
- elem2
Now, for some ungodly reason ansible is trying to evaluate elem1
(even though it's skipping the task) and because elem1
is undefined, it's erroring out the whole thing with:
fatal: [some_host] => subelements lookup expects a dictionary, got 'elem1'
How can I avoid this happening? Ideally, I'd love to get rid of all the skips on tasks and just have it tell me it's skipping the role, but if that's unavoidable, can I at least do something to stop having it evaluate variables on tasks that are being skipped anyway?
Upvotes: 2
Views: 2231
Reputation: 13940
First, the reason it shows as "skipped" instead of silently not executing is because (at least in Ansible 1.x) the task execution order is determined before anything is evaluated (same reason that vars in task names don't work), and each host has its own set of variables. Just because a conditional caused it to be skipped for one host doesn't mean it might not for another, therefore, all tasks will always show up, even if they're skipped for all hosts. The playbook runner has been largely rewritten for 2.x, and true conditional includes/roles were on the roadmap (though I haven't looked lately to see if the current 2.0 code has that or not)- that would likely solve your issue.
Assuming you want something that'll work in 1.x- have you considered using either role defaults or a default filter on the problem variables to prevent them from breaking? There's also the big hammer of setting error_on_undefined_vars=False in ansible.cfg, but I wouldn't recommend that...
Upvotes: 1