# pass variables to main.yml of a role

**URL:** <https://forum.ansible.com/t/pass-variables-to-main-yml-of-a-role/35963>\
**Category:** Ansible Project\
**Created:** [November 4, 2021, 5:06am UTC](https://forum.ansible.com/t/pass-variables-to-main-yml-of-a-role/35963 "2021-11-04T05:06:02Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [November 4, 2021, 5:06am UTC](https://forum.ansible.com/t/pass-variables-to-main-yml-of-a-role/35963/1 "2021-11-04T05:06:02Z")

</div>

I have a couple of standalone playbooks which I am trying to transform to role-based execition.

At the start I [get the hostname in a user dialog](https://pastebin.com/qR1zG9BR), then go in with the first play when ther is no ssh available on target yet to set up authorized\_keys file, so I go in for the first play with user/pw combination which I want to pass as variables.

- hosts: target\_host\_group  
vars\_files: defaults.yml  
vars:  
ansible\_ssh\_pass: superawesomepwd  
ansible\_user: user

I am aware that I need to make sure this is safe (probably using vault) yet, but that is not the point of my question here.

What I need to figure out is how to pass the paragraph above in the main.yaml of my initial role (and only that role, not the other ones).

using the syntax above or just

---

<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:** [November 4, 2021, 8:54am UTC](https://forum.ansible.com/t/pass-variables-to-main-yml-of-a-role/35963/2 "2021-11-04T08:54:15Z")

</div>

I found a solution here: [https://www.dasblinkenlichten.com/ansible-roles-and-variables/](https://www.dasblinkenlichten.com/ansible-roles-and-variables/)

_"In the role definition Variables can also be defined as part of a role._  
_There are two places this can be done with variables defined in either the ‘vars’ or the ‘default’ directory of the role. Much in the same way we defined other role functions, we can also define a ‘main.yml’ file in both of these directories that define variable to use as part of the role. For instance let’s assume that we have the following defined as ‘/etc/ansible/roles/webserver/defaults/main.yml’ "_

/project
