如何在Ansible中跳过角色执行

编程入门 行业动态 更新时间:2024-10-11 21:26:07
本文介绍了如何在Ansible中跳过角色执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试为无业游民的机器编写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中跳过角色执行

本文发布于:2023-11-25 20:30:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳过   角色   如何在   Ansible

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!