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

Deployment 如何跳过Ansible中执行的角色,deployment,webserver,vagrant,administration,ansible,Deployment,Webserver,Vagrant,Administration,Ansible,我试图为我的流浪机器编写playbook.yml,但我面临以下问题。 Ansible提示我设置这些变量,我将这些变量设置为null/false/no/[just enter],但不管怎样,角色都会被执行!如何防止这种行为?如果没有设置变量,我只想不采取任何行动 --- - name: Deploy Webserver hosts: webservers vars_prompt: run_common: "Run common tasks?" run_wordpress:

我试图为我的流浪机器编写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 }

我相信当您使用vars_提示符时,变量总是被定义的,所以“is defined”总是正确的。您可能想要的是以下内容:

- 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" }
和roles/common/tasks/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

据我所知,这个示例在run_common与“Y”不匹配时抛出错误。您测试过吗?请参阅上面我编辑的示例,其中显示了双向运行时的输出。