# Role Dependencies and var\_files

**URL:** <https://forum.ansible.com/t/role-dependencies-and-var-files/14023>\
**Category:** Ansible Project\
**Created:** [November 28, 2013, 12:37pm UTC](https://forum.ansible.com/t/role-dependencies-and-var-files/14023 "2013-11-28T12:37:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shih\_Oon\_Liong1](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Shih\_Oon\_Liong1](https://forum.ansible.com/u/Shih_Oon_Liong1)\
**Post date:** [November 28, 2013, 12:37pm UTC](https://forum.ansible.com/t/role-dependencies-and-var-files/14023/1 "2013-11-28T12:37:58Z")

</div>

Hi,

I am kinda confused on what is the right syntax to try out this. Running a role multiple times in a single playbok but a different var\_files each time.

I have a web server role for a LAMP stack that will also install the Apache vhost site. My idea was that I can run this role on the same server if I was installing multiple sites - Ansible would just skip the software installations but just install the vhost configs.

This works fine if I run the playbook multiple times manually. What I want to do is to do this multiple times in one single playbook. My assumption is that I should be using Role Dependencies (amirite?).

My current playbook _roughly_

---

<div class="post-metadata">

**Author:** ![Fred\_Badel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Fred\_Badel](https://forum.ansible.com/u/Fred_Badel)\
**Post date:** [November 28, 2013, 1:09pm UTC](https://forum.ansible.com/t/role-dependencies-and-var-files/14023/2 "2013-11-28T13:09:06Z")

</div>

Hi,

You can use parameterized roles …  
I use the following to create several DB using one role (roughly):

roles/create\_db/tasks/main.yml:

- name: create {{ db\_name }}  
mysql\_db: name={{ db\_name }} encoding=utf8 login\_user=root state=present login\_password={{ mysql\_root\_password }}

then it is called as follow in the playbook:

- hosts: db\_server  
roles:

- { role: create\_db, db\_name: DB\_1 }

- { role: create\_db, db\_name: DB\_2 }

- { role: create\_db, db\_name: DB\_3 }

Typically, for an apache vhost configuration, you could pass the value of “servername" and “documentroot” to the webserver role like this

role:

- { role: webservers, servername: [www.site1.com](http://www.site1.com), documentroot: /var/www/site1 }

- { role: webservers, servername: [www.siteB.com](http://www.siteB.com), documentroot: /var/www2/siteB }

- note 1:  
you should be using {{ my\_var }} instead of $my\_var

-note 2:  
you are mentioning role dependencies, which is meant to configure… dependencies between roles (ie the role webservers requires the role common) and I am not sure that it is related to you question [http://www.ansibleworks.com/docs/playbooks\_roles.html#role-dependencies](http://www.ansibleworks.com/docs/playbooks_roles.html#role-dependencies)

HTH

Fred

---

<div class="post-metadata">

**Author:** ![Shih\_Oon\_Liong1](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Shih\_Oon\_Liong1](https://forum.ansible.com/u/Shih_Oon_Liong1)\
**Post date:** [November 28, 2013, 5:53pm UTC](https://forum.ansible.com/t/role-dependencies-and-var-files/14023/3 "2013-11-28T17:53:34Z")

</div>

I didn’t realized about the parameterized roles. I’ll give that go! But that also cleared up on what role dependencies was suppose to be for.

What I am try to do though is to load up a different config file instead so that instead of me trying to do

var\_files

- config\_file

role:

- { role: webservers, servername: [www.site1.com](http://www.site1.com/), documentroot: /var/www/site1 }

- { role: webservers, servername: [www.siteB.com](http://www.siteb.com/), documentroot: /var/www2/siteB }

I can do something like

var\_files

- config\_file

role:

- { role: webservers, [Config file 1] }

- { role: webservers, [Config file 2] }

Though I guess maybe the var\_files should be in the roles itself perhaps? I could try to make my role take in either individual vars or if a var\_files is defined, to use that instead?

---

<div class="post-metadata">

**Author:** ![Fred\_Badel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Fred\_Badel](https://forum.ansible.com/u/Fred_Badel)\
**Post date:** [November 28, 2013, 8:09pm UTC](https://forum.ansible.com/t/role-dependencies-and-var-files/14023/4 "2013-11-28T20:09:09Z")

</div>

Hi,

I don’t know what you’re trying to achieve exactly … but assuming that all your vhosts have the same configuration file structure, you could use a template and to something like below:

roles/  
webservers/  
tasks/main.yml  
vars/main.yml  
templates/vhost.conf.j2

playbook:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 28, 2013, 8:33pm UTC](https://forum.ansible.com/t/role-dependencies-and-var-files/14023/5 "2013-11-28T20:33:34Z")

</div>

Public service announcement:

Please do not use old style variables.

Ansible 1.4 is already sharing deprecation warnings about this.

user: “{{ user }}”
