Passing dynamic variable into a role

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

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

# roles/nfs_mounts/vars/main.yml

Hello,

Here you pass to Ansible that you nfs_group is ‘web_mounts’ (sting).

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

Pass a variable

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: