如何在 Ansible 中跳过角色执行 [英] How to skip role executing in Ansible

查看:43
本文介绍了如何在 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 时,变量将始终被定义,因此已定义"将始终为真.您可能想要的是以下内容:

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 中跳过角色执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆