/ansible-gotcha-include-vs-import

A small test case which demonstrates the gotcha with include_tasks vs. import_tasks

Ansible Gotcha: include_tasks vs. import_tasks

This test case demonstrates a small gotcha with include_tasks and import_tasks in combination with variables.

TL;DR

When you use import_tasks the variables from the inventory aren't available yet and only the role defaults and the extra vars are used.

Show Me

Just execute

ansible-playbook -i inventory playbook.yml
ansible-playbook -i inventory -e version=2018.1 playbook.yml
ansible-playbook -i inventory -e version=2019.1 playbook.yml

The last two won't show any errors but the result of the first call will be

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [test : show version] ****************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "version": "2019.1"
}

TASK [test : show version (2018)] *********************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "version": "2019.1"
}

TASK [test : assert version (2018)] *******************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {
    "assertion": "version.startswith(\"2018.\")", 
    "changed": false, 
    "evaluated_to": false, 
    "msg": "Assertion failed"
}
...ignoring

TASK [test : include_tasks] ***************************************************************************************************************************************************************************************************************************************************
included: 2019.yml for localhost

TASK [test : show version (2019)] *********************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "version": "2019.1"
}

TASK [test : assert version (2019)] *******************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "changed": false, 
    "msg": "All assertions passed"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1