Question:
I try to write the playbook.yml for my vagrant machine and I’m faced with the following problem.
Ansible prompt me to set these variables and I set these variables to null/false/no/[just enter], but the roles is executed no matter! How can I prevent this behavior? I just want no actions if no vars are set..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
--- - name: Deploy Webserver hosts: webservers vars_prompt: run_common: "Run common tasks?" run_wordpress: "Run Wordpress tasks?" run_yii: "Run Yii tasks?" run_mariadb: "Run MariaDB tasks?" run_nginx: "Run Nginx tasks?" run_php5: "Run PHP5 tasks?" roles: - { role: common, when: run_common is defined } - { role: mariadb, when: run_mariadb is defined } - { role: wordpress, when: run_wordpress is defined } - { role: yii, when: run_yii is defined } - { role: nginx, when: run_nginx is defined } - { role: php5, when: run_php5 is defined } |
Answer:
I believe the variables will always be defined when you use vars_prompt, so “is defined” will always be true. What you probably want is something along these lines:
1 2 3 4 5 6 7 8 9 10 |
- name: Deploy Webserver hosts: webservers vars_prompt: - name: run_common prompt: "Product release version" default: "Y" roles: - { role: common, when: run_common == "Y" } |
Edit: To answer your question, no it does not throw an error. I made a slightly different version and tested it using ansible 1.4.4:
1 2 3 4 5 6 7 8 9 10 |
- name: Deploy Webserver hosts: localohst vars_prompt: - name: run_common prompt: "Product release version" default: "N" roles: - { role: common, when: run_common == "Y" or run_common == "y" } |
And roles/common/tasks/main.yml contains:
1 2 |
- local_action: debug msg="Debug Message" |
If you run the above example and just hit Enter, accepting the default, then the role is skipped:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Product release version [N]: PLAY [Deploy Webserver] ******************************************************* GATHERING FACTS *************************************************************** ok: [localhost] TASK: [common | debug msg="Debug Message"] ************************************ skipping: [localhost] PLAY RECAP ******************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=0 |
But if you run this and enter Y or y when prompted then the role is executed as desired:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Product release version [N]:y PLAY [Deploy Webserver] ******************************************************* GATHERING FACTS *************************************************************** ok: [localhost] TASK: [common | debug msg="Debug Message"] ************************************ ok: [localhost] => { "item": "", "msg": "Debug Message" } PLAY RECAP ******************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 |