I am using Ansible to deploy an environment that may have services distributed or not. I would like to conditionally include playbooks based on arguments I pass to ansible-playbook.
create_server.yml
---
- include: launch_ec2_instance.yml
- include install_postgres.yml
when {{db}} == "Y"
- include install_redis.yml
when {{redis}} == "Y"
Here is how I am calling create_server.yml
ansible-playbook create_server.yml -i local --extra-vars "db=Y redis=N"
Is it possible to do this and if so, how?
To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.
Ansible extra vars is a feature that allows you to include more flexibility in your Ansible playbooks by providing you with the ability to specify dynamic values when executing the playbook. Ansible extra vars are helpful when: You have a variable whose value may change more than once when running the playbook.
Yes. It's possible. You are missing a colon(:) on your when statement.
---
- include: launch_ec2_instance.yml
- include install_postgres.yml
when: {{ db }} == "Y"
- include install_redis.yml
when: {{ redis }} == "Y"
You can also omit the braces ({{ }}):
---
- include: launch_ec2_instance.yml
- include install_postgres.yml
when: db == "Y"
- include install_redis.yml
when: redis == "Y"
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