# How to pass variable into role

**URL:** <https://forum.ansible.com/t/how-to-pass-variable-into-role/27744>\
**Category:** Ansible Project\
**Created:** [June 8, 2018, 1:45am UTC](https://forum.ansible.com/t/how-to-pass-variable-into-role/27744 "2018-06-08T01:45:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![liubo](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@liubo](https://forum.ansible.com/u/liubo)\
**Post date:** [June 8, 2018, 1:45am UTC](https://forum.ansible.com/t/how-to-pass-variable-into-role/27744/1 "2018-06-08T01:45:03Z")

</div>

SUMMARY

It seems hard to send a param into a role. The rule of `vars` is confusing.

`

# What I want is overwrite `version` in role `b` by `version` in role `a`

# roles/a/default/main.yml

version: v1.0.1

# roles/a/tasks/main.yml

- name: include role  
include\_role:  
name: b  
vars:  
version: “{{ version }}”

# roles/b/default/main.yml

version: “”

# roles/b/tasks/main.yml

- name: ouput version  
debug:  
var: version  
`

EXPECTED RESULTS  
I can pass the `version` variable into role `b` without any variable name changes of role b and a

ACTUAL RESULTS

1. I have to change `version` to `_version` in role `a`.  
Some problems will happened if `_version` is also defined by `b`. And I have to know all inner variables of `b`

2. I have to undefine `version` in role `b`  
Role b can’t have any default value of `version`.

It seems hard to keep independent of role.

> <https://github.com/ansible/ansible/issues/41257>
>
> \##### SUMMARY
> It seems hard to send a param into a role. The rule of \`vars\` is …confusing.
> 
> \##### ISSUE TYPE
> - Bug Report
> 
> \##### COMPONENT NAME
> 
> 
> \##### ANSIBLE VERSION
> 
> \`\`\`
> v2.5.4
> \`\`\`
> 
> \##### CONFIGURATION
> 
> 
> \##### OS / ENVIRONMENT
> 
> 
> \##### STEPS TO REPRODUCE
> 
> 
> 
> \`\`\`yaml
> \# What I want is overwrite \`version\` in role \`b\` by \`version\` in role \`a\`
> \# roles/a/default/main.yml
> version: v1.0.1
> \# roles/a/tasks/main.yml
> \- name: include role 
> include\_role:
> name: b
> vars:
> version: "{{ version }}"
> 
> \# roles/b/default/main.yml
> version: ""
> \# roles/b/tasks/main.yml
> \- name: ouput version
> debug:
> var: version
> \`\`\`
> 
> 
> 
> \##### EXPECTED RESULTS
> 
> I can pass the \`version\` variable into role \`b\` without any variable name changes of role b and a
> 
> \##### ACTUAL RESULTS
> 
> 1. I have to change \`version\` to \`\_version\` in role \`a\`.
> Some problems will happened if \`\_version\` is also defined by \`b\`. And I have to know all inner variables of \`b\`
> 
> 2. I have to undefine \`version\` in role \`b\`
> Role b can't have any default value of \`version\`.
> 
> It seems hard to keep independent of role.
> 
> 
> \`\`\`
> 
> \`\`\`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 15, 2018, 6:09pm UTC](https://forum.ansible.com/t/how-to-pass-variable-into-role/27744/2 "2018-06-15T18:09:16Z")

</div>

'role defaults' are special, they do make them available for the rest  
of the play but a role will always give priority to their own default  
definition.
