How to transfer variables from one role downstream to other dependency roles with capability?

I have a generic webserver role that uses a different nginx role to create new vservers.

webserver/meta/main.yml looks like this:

  allow_duplicates: yes
 dependencies:
   - role: nginx
     name: api vserver
     frontend_port: "{{frontend_port}}"
     domain: "{{api_domain}}"
     backend_host: 127.0.0.1
   - role: nginx
     name: portal vserver
     domain: "{{portal_domain}}"
     backend_host: 127.0.0.1

The problem is that these variables must be defined inside webserver-role/vars/(test|staging).yml

It seems that Ansible will try to load the dependencies before loading the variables.

How can I solve this problem? I do not want to set any configuration features inside low-level roles.

In addition, I do not want to place the configurations inside the book itself, because these configurations are shared between multiple players.

+6
source share
1 answer

This script works with Ansible 2.2.
Vars for dependent roles are specified in the core role vars files:

./ roles / role1/ tasks / main.yml:

 - debug: msg="{{ role_param }}" 

./roles/role2/meta/main.yml:

 allow_duplicates: yes dependencies: - role: role1 role_param: "{{ param1 }}" - role: role1 role_param: "{{ param2 }}" 

./ roles / role 2/ tasks / main.yml:

 - debug: msg=role2 

./roles/role2/vary/main.yml:

 param1: hello1 param2: hello2 

Result:

 PLAY [localhost] *************************************************************** TASK [role1 : debug] *********************************************************** ok: [localhost] => { "msg": "hello1" } TASK [role1 : debug] *********************************************************** ok: [localhost] => { "msg": "hello2" } TASK [role2 : debug] *********************************************************** ok: [localhost] => { "msg": "role2" } 
+3
source

All Articles