Ansible run always role

♀尐吖头ヾ 提交于 2019-12-10 10:28:16

问题


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.


回答1:


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]



回答2:


You could use Ansible block and always for this situation

http://docs.ansible.com/ansible/latest/playbooks_blocks.html




回答3:


What about using block and including the yaml file from the role instead of declaring the role as a dependency:

tasks:
  - name: "start deployment"
    block:
      - include: "<path to roles>/lock-deployment/tasks/main.yaml"
      - include: "<path to roles>/fetch-artifactory/tasks/main.yaml"
      - include: "<path to roles>/unlock-deployment/tasks/main.yaml"
    always:
      - name: "remove lock file"
        ...

If you need to include default variables from your roles you could do:

roles:
  - { role: lock-deployment,
      when: False
    }

My solution is not an elegant one, but I would really suggest moving off from roles for those kind of actions and do includes instead.

Also: I have not tested the solution and might not work actually. But it gives an idea to alternate solutions.



来源:https://stackoverflow.com/questions/47575257/ansible-run-always-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!