Is there any way to always run a role? I am creating lock file before starting any deployment to prevent parallel deployment. In case of any failure/success I want to delete the lock file.
- { role: lock-deployment, tags: always }
- { role: fetch-artifactory, tags: always }
- { role: unlock-deployment, tags: always }
I want to run unlock-deployment role irrespective of failure/success.
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 .
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 .
The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .
As ansible delegate_to is a directive, not an individual module, It integrates with other modules and it controls the task execution by deciding which host should run the task at runtime.
problem is I don't want to do block, rescue for every task. I just want to delete lock file in case of failure in any of the task. I tried looking around if role itself can be put into block but didn't find any. ref
You can use block
with always
construct. Roles can be included with include_role
:
tasks:
- include_role:
name: lock-deployment
- block:
- include_role:
name: fetch-artifactory
always:
- include_role:
name: unlock-deployment
This produces your desired flow (fetch-artifactory
contains fail
task to emulate failure):
PLAY [localhost] ***************************************************************************************
TASK [include_role] ************************************************************************************
TASK [lock-deployment : file] **************************************************************************
changed: [localhost]
TASK [include_role] ************************************************************************************
TASK [fetch-artifactory : fail] ************************************************************************
Unaltered: {'msg': u'Failed as requested from task', 'failed': True, 'changed': False}
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Failed as requested from task"}
TASK [include_role] ************************************************************************************
TASK [unlock-deployment : file] **********************************************************************
changed: [localhost]
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