我尝试为无业游民的机器编写playbook.yml,但遇到以下问题. Ansible提示我设置这些变量,并将这些变量设置为null/false/no/[just enter],但是无论执行角色都是!如何防止这种行为?如果没有设置任何变量,我只希望不执行任何操作.
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..
--- - 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 }推荐答案
我相信使用vars_prompt时将始终定义变量,因此已定义"将始终为true.您可能想要的是以下几方面的东西:
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:
- name: Deploy Webserver hosts: webservers vars_prompt: - name: run_common prompt: "Product release version" default: "Y" roles: - { role: common, when: run_common == "Y" }要回答您的问题,不,它不会引发错误.我制作了一个稍有不同的版本,并使用ansible 1.4.4对其进行了测试:
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:
- 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" }而Roles/common/tasks/main.yml包含:
And roles/common/tasks/main.yml contains:
- local_action: debug msg="Debug Message"如果运行上面的示例,然后按Enter键并接受默认值,则该角色将被跳过:
If you run the above example and just hit Enter, accepting the default, then the role is skipped:
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但是,如果运行此命令并在出现提示时输入Y或y,则该角色将根据需要执行:
But if you run this and enter Y or y when prompted then the role is executed as desired:
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更多推荐
如何在Ansible中跳过角色执行
发布评论