# which is a correct way to reuse code from roles?

**URL:** <https://forum.ansible.com/t/which-is-a-correct-way-to-reuse-code-from-roles/16412>\
**Category:** Ansible Project\
**Tags:** ansible-project, ubuntu\
**Created:** [October 3, 2014, 6:44am UTC](https://forum.ansible.com/t/which-is-a-correct-way-to-reuse-code-from-roles/16412 "2014-10-03T06:44:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igor\_Homyakov](https://avatars.discourse-cdn.com/v4/letter/i/f4b2a3/32.png) [@Igor\_Homyakov](https://forum.ansible.com/u/Igor_Homyakov)\
**Post date:** [October 3, 2014, 6:44am UTC](https://forum.ansible.com/t/which-is-a-correct-way-to-reuse-code-from-roles/16412/1 "2014-10-03T06:44:04Z")

</div>

Hi Dmitry,

I would suggest you to use my approach bellow:

&nbsp;&nbsp;&nbsp;&nbsp;# Load a variable file based on the OS type, or a default if not found.  
&nbsp;&nbsp;&nbsp;&nbsp;- include\_vars: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_first\_found:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ ansible\_distribution }}.yml"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ ansible\_os\_family }}.yml"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "default.yml"  
&nbsp;&nbsp;&nbsp;&nbsp;- action: '{{ pkg\_mgr }} name={{ item }} state=latest'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: packages

&nbsp;&nbsp;# Ubuntu.yml  
&nbsp;&nbsp;pkg\_mgr: apt  
&nbsp;&nbsp;packages:  
&nbsp;&nbsp;&nbsp;&nbsp;- bash  
&nbsp;&nbsp;&nbsp;&nbsp;- screen  
&nbsp;&nbsp;&nbsp;&nbsp;- curl

Here is a gist: [https://gist.github.com/hostmaster/db37c8e6c2a43997a224](https://gist.github.com/hostmaster/db37c8e6c2a43997a224)

-- Best, Igor
