I'm trying to set variables in a playbook so that they're accessible throughout the entire rest of the playbook at the playbook level rather than the host level. An example (where the machine_mode
variable is passed in from command line):
- name: configure and install everything for the new boxes
sudo: True
hosts: new_hosts
vars: [
docker_required_roles: "['app_server', 'other_app_server']",
pip_required_roles: "['some_server', 'other_server']",
docker_required: "{{ machine_mode }} in docker_required_roles",
pip_required: "{{ machine_mode }} in pip_required_roles"
]
pre_tasks:
- name: Gathering ec2 facts
action: ec2_facts
roles:
#install docker and pip when required
- { role: bobbyrenwick.pip, when: "pip_required"}
- { role: angstwad.docker_ubuntu, when: "docker_required"}
Unfortunately, when I run this, it breaks with error while evaluating conditional: dev in pip_required_roles
. Is there some other way I can define those variables so they're accessible through the whole playbook?
It looks like you're misunderstanding where your playbook is falling over.
The error is telling you that it can't evaluate the conditional that you have put on the pip role as you are passing it the string literal "dev in pip_required_roles"
rather than true
or false
.
Instead you need to get Ansible to evaluate your conditional statement properly to give you a boolean for your conditional role. You can do this by changing your vars block like this:
- name: configure and install everything for the new boxes
sudo: True
hosts: all
vars:
- docker_required_roles: ['app_server', 'other_app_server']
- pip_required_roles : ['some_server', 'other_server']
- docker_required : machine_mode in docker_required_roles
- pip_required : machine_mode in pip_required_roles
roles:
#install docker and pip when required
- { role: bobbyrenwick.pip, when: "pip_required"}
- { role: angstwad.docker_ubuntu, when: "docker_required"}
This changes your string literals for docker_require_roles
and pip_required_roles
variables into lists and then the docker_required
and pip_required
variables into conditions using Python's in
operator (notice the lack of double quotes around them).
As a minor note I've also adjusted the vars block to use YAML's list syntax rather than your chosen Python style syntax but that's more of a personal preference than anything else. You could also break down the lists for docker_require_roles
and pip_required_roles
variables to use the YAML syntax as well like this but for some reason that looks slightly more awkward to me:
...
vars:
- docker_required_roles:
- 'app_server'
- 'other_app_server'
- pip_required_roles :
- 'some_server'
- 'other_server'
- docker_required : machine_mode in docker_required_roles
- pip_required : machine_mode in pip_required_roles
roles:
...
As to the wider idea of what you are tying to accomplish it looks like you might be better off doing this with different inventory files and group vars.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With