# include\_vars or vars\_files in a role

**URL:** <https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925>\
**Category:** Ansible Project\
**Created:** [April 24, 2023, 5:35pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925 "2023-04-24T17:35:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 24, 2023, 5:35pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/1 "2023-04-24T17:35:53Z")

</div>

I got a pb that works like this. any idea how i can convert this to a role?

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 24, 2023, 6:04pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/2 "2023-04-24T18:04:08Z")

</div>

similar to this. This pb i want to break it up into roles

any idea?

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [April 25, 2023, 4:46am UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/3 "2023-04-25T04:46:10Z")

</div>

You can follow the below structure.  
For more information … happy coding…

[https://www.golinuxcloud.com/ansible-roles-directory-structure-tutorial/](https://www.golinuxcloud.com/ansible-roles-directory-structure-tutorial/)

 ![](https://us1.discourse-cdn.com/ansible/original/2X/3/3a147ee37206865423da0d41f079f774bb596044.jpeg)

Cheers

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [April 25, 2023, 5:05am UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/4 "2023-04-25T05:05:25Z")

</div>

the role structure can be as complex as mentioned in the previous post, but it does not have to include everything. In your case I’d say it boild down to this.

you create a structure like this:

base\_folder

> \_\_ playbook.yml
> 
> \_\_ /roles
> 
> \_\_ role1
> 
> \_\_/tasks
> 
> > \_\_main.yml
> 
> \_\_/defaults

> \_\_/main  
> \_\_vcenter\_creds.yml  
> \_\_vars.yml

the playbook.yml

- name: test  
hosts: all  
gather\_facts: no

roles:

- role1

(there are other ways to all the roles though but thius should do the job)

the ./tasks/main.yml

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 25, 2023, 12:59pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/5 "2023-04-25T12:59:29Z")

</div>

for

> \_\_/defaults

> \_\_/main  
> \_\_vcenter\_creds.yml  
> \_\_vars.yml

does these need to be under defaults under each role?

what if other roles need to access these same vars files?

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 25, 2023, 1:16pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/6 "2023-04-25T13:16:08Z")

</div>

this is what i have currently

change\_esxi\_root\_pass

├── hosts

├── main.yml

└── roles

├── change\_esxi\_root

├── disable\_ssh

└── enable\_ssh

├── tasks

│ └── main.yml

└── vars

├── vars.yml

└── vcenter\_creds.yml

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [April 25, 2023, 1:32pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/7 "2023-04-25T13:32:43Z")

</div>

You could use ansible-galaxy to instantiate a role skeleton:

dick.visser@GA0267 tmp$ ansible-galaxy init enable\_ssh

- Role enable\_ssh was created successfully

dick.visser@GA0267 tmp$ tree enable\_ssh/  
enable\_ssh/  
├── README.md  
├── defaults  
│ └── main.yml  
├── files  
├── handlers  
│ └── main.yml  
├── meta  
│ └── main.yml  
├── tasks  
│ └── main.yml  
├── templates  
├── tests  
│ ├── inventory  
│ └── test.yml  
└── vars  
└── main.yml

9 directories, 8 files

btw, different roles to enable and disable SSH to me sounds like too much overhead…

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 25, 2023, 1:41pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/8 "2023-04-25T13:41:52Z")

</div>

but how would i split up enable/disable ssh? can both be in same role? I need to enable ssh before i can change root pass, then disable ssh after its done

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 26, 2023, 1:05pm UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/9 "2023-04-26T13:05:57Z")

</div>

any idea?

---

<div class="post-metadata">

**Author:** ![Haji\_Hoare](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@Haji\_Hoare](https://forum.ansible.com/u/Haji_Hoare)\
**Post date:** [April 30, 2023, 3:28am UTC](https://forum.ansible.com/t/include-vars-or-vars-files-in-a-role/37925/10 "2023-04-30T03:28:42Z")

</div>

Hi,

I think it would be a good idea to create a single role for managing ESXi settings, including changing the root password.  
To make the role reusable, I recommend not specifying the hostname, ID, and password within the role itself.  
In your playbook, you can utilize a `vars_files` directive to include a file like `vcenter_creds.yml` that contains definitions for variables such as `vcenter_hostname`.  
These defined variables can then be accessed and used within your role as well.  
In Ansible, variables defined within the role’s vars/main.yml file have a higher precedence than those defined in the playbook.  
Therefore, to ensure that the playbook-defined variables are used, avoid defining the same variables in the role’s vars/main.yml file.

2023年4月26日水曜日 22:06:20 UTC+9 Tony Wong:
