Reputation: 817
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?
Upvotes: 11
Views: 15732
Reputation: 1148
@Rico's answer is correct except that it only applies when your include statement is part of a task.
Eg.
---
tasks:
- include install_postgres.yml
when: db == "Y"
If your playbook is just a bunch of includes as your 'create_server.yml' seems to be then 'when' wont work.
Upvotes: 4
Reputation: 61621
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"
Upvotes: 15