Reputation: 7827
I try to control from one unique playbook the call of the same role n times with n different vars:
---
- hosts: myhost
vars:
user: user1
virtualenv: venv_nameV3
roles:
- makeuser
- stack # use virtualenv var so stack role is installed in venv_nameV3
- stack # should need it's own virtualenv value e.g. venv_nameV4
- stack # should need it's own virtualenv value e.g. venv_nameV5
- stack # should need it's own virtualenv value e.g. venv_nameV6
Upvotes: 4
Views: 3688
Reputation: 1943
You cannot insert a proper code block as a comment, so here is the accepted answer written in pure YAML instead of JSON:
My initial answer turns out not to be correct. Here is the corrected one:
---
- hosts: myhost
vars:
user: user1
virtualenv: default_venv_name
roles:
- makeuser
- role: stack
virtualenv: venv_nameV3
- role: stack
virtualenv: venv_nameV4
- role: stack
virtualenv: venv_nameV5
- role: stack
virtualenv: venv_nameV6
Original answer:
---
- hosts: myhost
vars:
user: user1
virtualenv: default_venv_name
roles:
- makeuser
- role: stack
vars:
virtualenv: venv_nameV3
- role: stack
vars:
virtualenv: venv_nameV4
- role: stack
vars:
virtualenv: venv_nameV5
- role: stack
vars:
virtualenv: venv_nameV6
While they are only slightly different, they change how Ansible treats default variables (defined in defaults/main.yml
):
If you leave a variable out when including the role in your playbook, Ansible will use the variable from the previous run of the same role ("variable bleeding").
In the context of the accepted answer, when not setting virtualenv: venv_nameV6
in the last call to the stack
role, virtualenv
will have the value venv_nameV5
(from the previous call to the stack
role), no matter what virtualenv
is set to in the role's defaults/main.yml
file.
If you call your roles like I did in the corrected answer, your role's defaults/main.yml
file will be respected and virtualenv
will be set to whatever you have defined as default.
A minor difference, but quite hard to track down.
Upvotes: 3
Reputation: 26467
You can parameterise roles by adding variables using the following syntax
---
- hosts: myhost
vars:
user: user1
virtualenv: default_venv_name
roles:
- makeuser
- { role: stack, virtualenv: 'venv_nameV3' }
- { role: stack, virtualenv: 'venv_nameV4' }
- { role: stack, virtualenv: 'venv_nameV5' }
- { role: stack, virtualenv: 'venv_nameV6' }
Upvotes: 6