AntonioHS
AntonioHS

Reputation: 1370

Call a role from a included tasks file in Ansible

I'm using Ansistrano to deploy a php application, and I need to use the "after code update" hook to do some final tasks, including some that are created in the form of roles.

Since that hook is an include, I don't see how can I execute some tasks, then execute a role, and then continue to execute some more tasks.

Is that somehow possible in Ansible, to call roles form included tasks files?

Upvotes: 0

Views: 1761

Answers (3)

Davide
Davide

Reputation: 17818

Now (in Ansible v2.2 and newer) you can include a role as a task with the include_role module. See an use example here

Upvotes: 0

Dave Snigier
Dave Snigier

Reputation: 2613

You can specify role dependencies in ansistrano.deploy/meta but that would only allow you to specify one dependency step with Ansistrano unless you split it out into separate roles.

Which is exactly what I suggest that you do. I am not familiar with Capistrano, but reading through the Ansistrano role, it's mostly is just layering on another layer of abstraction through clever use of variables for those more familiar with Capistrano than Ansible.

Use the Capistrano deployment workflow and rollback technique with symlinks to create individual roles and deploy.yml rollback.yml playbooks which call those roles in the order you specify.

Here's an example playbook with all possible steps as roles, but you would only need to create those which you actually use.

---
- hosts: all
  roles:
    - pre-setup
    - setup
    - pre-update
    - update-code
    - pre-symlink-shared
    - symlink-shared
    - pre-symlink
    - symlink
    - pre-cleanup
    - cleanup
    - post-cleanup

Upvotes: 1

udondan
udondan

Reputation: 59989

No, you can not call roles from within tasks. Roles can only be applied to plays or as dependency of other roles.

If your roles are not too complex you might be able to simply include the tasks/main.yml of your role. But if you have role defaults, vars, meta etc this will not work. If you use templates or files you might need to provide a relative or even absolute path to them, instead of simply using the name of the file or template.

Upvotes: 2

Related Questions