Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Using --extra-vars for conditional includes

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?

like image 703
MattM Avatar asked Mar 19 '14 00:03

MattM


People also ask

How do you pass extra vars in Ansible?

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.

What is extra vars in Ansible?

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.


1 Answers

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"
like image 170
Rico Avatar answered Oct 01 '22 18:10

Rico