如何跳过在Ansible中执行的角色

问题描述 投票:15回答:1

我尝试为无业游民的机器编写playbook.yml,但遇到以下问题。Ansible提示我设置这些变量,并将这些变量设置为null / false / no / [just enter],但是无论执行角色都是!如何防止这种行为?如果没有设置任何变量,我只希望不执行任何操作。]

---
- 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 }
deployment webserver vagrant administration ansible
1个回答
26
投票

我相信使用vars_prompt时将始终定义变量,因此“已定义”将始终为true。您可能想要的是这些方面的东西:

- 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测试了它:

- 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" }

和角色/公共/任务/main.yml包含:

- local_action: debug msg="Debug Message"

如果运行上面的示例,然后按Enter键,接受默认值,那么将跳过该角色:

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,则将根据需要执行该角色:

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
© www.soinside.com 2019 - 2024. All rights reserved.