Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible run always role

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.

like image 943
Lokesha S Avatar asked Nov 30 '17 14:11

Lokesha S


People also ask

How do I always run an Ansible task?

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 .

How do you call roles in Ansible?

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 .

How do I run a specific task in Ansible role?

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 .

What does Delegate_to do in Ansible?

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.


1 Answers

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]
like image 155
techraf Avatar answered Oct 05 '22 13:10

techraf