roles and tasks reusability

39 views
Skip to first unread message

lemaille...@gmail.com

unread,
May 23, 2017, 10:10:32 AM5/23/17
to Ansible Project
Hello everyone,

I did not found a elegant way of sharing tasks list between roles.
I explain my problem :
1 I have a first role in which i include tasks (IE create_user.yml) so i put the file in my_firt_role/tasks/create_user.yml
2 I am writing an other role in which i would like to use the tasks create_user.yml too

The only solutions i found to use it in both playbooks are :
hardcode the path of a common dir in my roles includes ( /var/ansible/common/create_user.yml)
duplicate the tasks in each playbook
I was expecting a way to configure tasks lookup directories (like role_path for roles) in ansible.cfg
I try also to add a role dependencies but it seems that the pulled roles are not sharing  tasks to upper roles.

Am I missing an Ansible feature or good practice ?
Any ideas to improve tasks re-usability and avoid duplication ?

Regards,

Efflam

lemaille...@gmail.com

unread,
May 23, 2017, 11:55:04 AM5/23/17
to Ansible Project
I finaly found a workaround :
ansible seems to lookup in tasks directory (even symlinks)
/etc/ansible/specific_utils/
├── utils_stuff1.yml
└── utils_stuff2.yml
/etc/ansible/common/
├── common_stuff1.yml
└── common_stuff2.yml

/path/to/your/ansible/workdir
├── tasks -> ../../../../etc/ansible/common
└── your_role
         └── tasks
               ├── main.yml
               └── tasks -> ../../../../../../../etc/ansible/specific_utils

Daniel JD

unread,
May 30, 2017, 8:35:39 AM5/30/17
to Ansible Project
You could use the include_role module like this:

include_role:
  name: my_flirt_role
  tasks_from: create_user
Reply all
Reply to author
Forward
0 new messages