Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include tasks from another role in ansible playbook

I'm designing a kind of playbook lib with individual tasks

so in the usual roles repo, I have something like:

roles ├── common │   └── tasks │       ├── A.yml │       ├── B.yml │       ├── C.yml │       ├── D.yml │       ├── login.yml │       ├── logout.yml │       └── save.yml ├── custom_stuff_workflow │   └── tasks │       └── main.yml └── other_stuff_workflow     └── tasks         └── main.yml 

my main.yml in custom_stuff_workflow then contain something like:

---  - include: login.yml - include: A.yml - include: C.yml - include: save.yml - include: logout.yml 

and this one in the other workflow:

---  - include: login.yml - include: B.yml - include: A.yml - include: D.yml - include: save.yml - include: logout.yml 

I can't find a way to do it in a natural way: one way that worked was having all tasks in a single role and tagging the relevant tasks while including a custom_stuff_workflow

The problem I have with that is that tags cannot be set in the calling playbook: it's only to be set at command line as I'm distributing this ansible repo with many people in the company, I can't rely on command line invocations (it would be nice to have a #! header in yml to be processed by ansible-playbook command)

I could also copy the relevant tasks (inside common in the above tree) in each workflow, but I don't want to repeat them around

Can someone see a solution to achieve what I'd like without repeating the tasks over different roles?

I guess the corner stone of my problem is that I define tasks as individual and it looks not natural in ansible...

Thanks a lot

PS: note that the tasks in the workflow have to be done in specific order and the only natural steps to abstract would be the login and save/logout

PPS: I've seen this question How do I call a role from within another role in Ansible? but it does not solve my problem as it's invoking a full role and not a subset of the tasks in a role

like image 333
Louis Avatar asked May 12 '15 13:05

Louis


People also ask

How do you add roles in Ansible playbook?

In Ansible 1.4 and later you can configure a roles_path to search for roles. Use this to check all of your common roles out to one location, and share them easily between multiple playbook projects. See Configuration file for details about how to set this up in ansible. cfg.

How do I run multiple roles in Ansible?

Step 1 — Navigate to /etc/ansible/roles directory and create the roles for prerequisites, MongoDB and NodeJS. You should now see three roles in your 'roles' directory. Step 2 — Write main. yml for prerequisites which installs Git.

What is the difference between roles and tasks in Ansible?

tl;dr A task is a single declaration (operation); a role is one of many ways for grouping tasks. A task in Ansible is a basic unit of work, a kind of counterpart to a line of code in other languages.

What is Include_tasks in Ansible?

Note. This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name include_tasks even without specifying the collections: keyword.


1 Answers

Just incase someone else bumps into this, version 2.2 of Ansible now has include_role.You can now do something like this.

--- - name: do something   include_role:     name: common     tasks_from: login 

Check out the documentation here.

like image 187
uLan Avatar answered Sep 20 '22 21:09

uLan