Role Dependencies and var_files

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

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, documentroot: /var/www/site1 }

  • { role: webservers, servername: 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

HTH

Fred

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, documentroot: /var/www/site1 }

  • { role: webservers, servername: 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?

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:

Public service announcement:

Please do not use old style variables.

Ansible 1.4 is already sharing deprecation warnings about this.

user: “{{ user }}”