'Does import_task with tags apply to blocks inside the imported tasks file?
I have a role that imports a bunch of tasks from individual files. To not have to run all of them all the time, I added a tags parameter to each import_tasks task, assuming I could thereby skip all the tasks inside the imported file. However, it appears that if the file you import has a block: statement, it does not get skipped.
So, this is roughly how my files look:
main_file.yml
- import_tasks: internal_A.yml
tags:
- A
- internal_A
- import_tasks: external_A.yml
tags:
- A
- external_A
- import_tasks: internal_B.yml
tags:
- B
- internal_B
- import_tasks: external_B.yml
tags:
- B
- external_B
internal_A.yml
- name: "Question?"
pause:
prompt: "{{ answer }}(yes/no/q)"
register: prompt_result
- block:
- name: User aborted
debug:
msg: User aborted playbook
- meta: end_play
when: prompt_result.user_input == 'q'
The error I get when running with --skip-tags A:
ERROR! The conditional check 'prompt_result.user_input == 'q'' failed. The error was: error while evaluating conditional (prompt_result.user_input == 'q'): 'prompt_result' is undefined
So, as you can see, it appears that even though I supposedly skip all tasks imported from internal_A.yml, it still reads the file and determines that it should handle the block when-condition, only to find that there is no such variable registered anywhere anymore, since the tasks that registers it has been skipped.
My expectation is that the block itself would also be skipped, meaning the when-condition has no reason to be evaluated, so it wouldn't matter if the variable is declared or not.
I suppose one way to circumvent this is to add a is defined to the condition statement, but my problem isn't really how to make it work (I can do that), but rather understanding exactly what is going on and why it isn't behaving as one would expect. Also, I did not write this code, merely had to adopt it and make the best of it. Believe me, if I had written it, it would not contain any prompts...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
