Include tasks from another role in a view inaccessible

I am developing a kind of library with individual tasks

so in normal repo roles, 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 contains something like:

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

and the other in a different 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 this in a natural way: one of the methods that worked was that all the tasks were performed in one role and marked with the corresponding tasks, including custom_stuff_workflow

The problem with this is that the tags cannot be installed in the calling playbook: it should only be installed on the command line since I am distributing this unresolved repo to many people in the company, I cannot rely on command line calls (it would be nice, if the header #! in yml was processed by the ansible-playbook command)

I could also copy the corresponding tasks (inside the common tree 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 would like without repeating tasks on different roles?

I think that the cornerstone of my problem is that I define tasks as individual, and it looks unnatural in the inaccessible ...

Thank you so much

PS: note that tasks in the workflow must be performed in a certain order, and the only natural steps for abstraction can be login and saving / logging out

PPS: I saw this question. How do I assign a role from another role in Ansible? , but it doesn’t solve my problem, since it causes a full role, not a subset of tasks in a role

+45
ansible ansible-playbook
May 12, '15 at 13:34
source share
2 answers

Just run someone else, version 2.2 from Ansible now has include_role . Now you can do something like this.

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

Check out the documentation here .

+20
Apr 6 '17 at 7:53 on
source share

Yes, Ansible doesn’t really like tasks as separate components. I think he wants you to use roles, but I understand why you do not want to use roles for simple reusable tasks.

I currently see two possible solutions:

1. Make these task files into roles and use dependencies

Then you can do something like this, for example. custom_stuff_workflow

 dependencies: - { role: login } 

See: https://docs.ansible.com/playbooks_roles.html#role-dependencies

2. Usage includes hardcoded paths to task files

 - include: ../../common/tasks/login.yml 

This worked very well in the short test tutorial I just did. Keep in mind, you can also use options, etc. Including.

See: https://docs.ansible.com/playbooks_roles.html#task-include-files-and-encouraging-reuse

Hope I understood this question correctly and it helps.

+38
May 13 '15 at 13:59
source share



All Articles