# Passing dynamic variable into a role

**URL:** <https://forum.ansible.com/t/passing-dynamic-variable-into-a-role/22116>\
**Category:** Ansible Project\
**Created:** [July 25, 2016, 8:32pm UTC](https://forum.ansible.com/t/passing-dynamic-variable-into-a-role/22116 "2016-07-25T20:32:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [July 25, 2016, 8:32pm UTC](https://forum.ansible.com/t/passing-dynamic-variable-into-a-role/22116/1 "2016-07-25T20:32:00Z")

</div>

For each of our servers (webs and perls), we have different mount points for each type of servers. So for web servers, I want to use the entries for “web\_mounts” and perls would be “perl\_mounts.”

If I hard code “web\_mounts” into the “with\_items,” it works fine. Otherwise, it tells me “nfs\_group” is undefined.

# Top Level

```auto
roles:
 - { role: webs, nfs_group: "web_mounts" }

```

```auto

```

```auto
# roles/nfs_mounts/vars/main.yml

```

---

<div class="post-metadata">

**Author:** ![Artyom\_Aleksandrov](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@Artyom\_Aleksandrov](https://forum.ansible.com/u/Artyom_Aleksandrov)\
**Post date:** [August 5, 2016, 9:52pm UTC](https://forum.ansible.com/t/passing-dynamic-variable-into-a-role/22116/2 "2016-08-05T21:52:22Z")

</div>

Hello,

Here you pass to Ansible that you nfs\_group is ‘web\_mounts’ (sting).

```auto
roles:
 - { role: webs, nfs_group: "web_mounts" }

```

Pass a variable

```auto
roles:
 - { role: webs, nfs_group: "{{ web_mounts }}" }

```

But I suggest you to change a way.  
Divide you hosts on groups: Web\_Hosts, Perl\_Hosts and specify the same variable name in group\_vars directory on inventory file but with different value based on you needs for specific group.

Then you can use one variable name for any number of groups.

Example:
