I have this playbook:
roles:
- { role: common}
- { role: mariadb}
- { role: wordpress}
What is want is in every role I want to have first task as some conditional which if true then I want to skip the whole role and playbook continues to next.
Now I can use when
like this:
roles:
- { role: common, when: mvar is False }
But for that I have to evaluate the mvar
in playbook itself but for that mvar
I need all other vars etc. stuff which is in that role itself. So it will be easier for me to do in role.
Is there any way?
If you assign the always tag to a task or play, Ansible will always run that task or play, unless you specifically skip it ( --skip-tags always ). Fact gathering is tagged with 'always' by default. It is only skipped if you apply a tag and then use a different tag in --tags or the same tag in --skip-tags .
By using –limit argument with ansible-playbook command we can exclude a host from playbook execution. If hostname starts with “!” it will excluded from host execution.
By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.
Ansible executes this pattern recursively when you use the roles keyword. For example, if you list role foo under roles: , role foo lists role bar under dependencies in its meta/main. yml file, and role bar lists role baz under dependencies in its meta/main. yml, Ansible executes baz , then bar , then foo .
In each role, in tasks/main.yml
include a file with tasks only when the condition is met:
- include: real_tasks.yml
when: condition_is_met
In addition to @techraf's answer, one can also use include_tasks instead of include.
- include_tasks: real_tasks.yml
when: condition_is_met
Ansible include documentation says:
Notes
Include has some unintuitive behaviours depending on if it is running in a static or dynamic in play or in playbook context, in an effort to clarify behaviours we are moving to a new set modules (include_tasks, include_role, import_playbook, import_tasks) that have well established and clear behaviours. This module will still be supported for some time but we are looking at deprecating it in the near future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With