Ansible:通过 CLI 传递 vars 时无法触发带有 when 条件的任务

Ansible: Can not trigger task with when condition when passing vars through CLI

在 Ansible 中,我想使用 when 条件在远程机器上执行一些 shell 命令取决于条件匹配与否。我正在发送带有 -e (--extra-var) 参数的变量,如下所示。但是没有任务与我的变量匹配并且总是跳过虽然应该匹配。我错过了什么 ?有人帮我吗?

Ansible 版本;

ansible 2.9.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

命令:

ansible-playbook -i hosts test.yml  -e environment=test -e  type=react

主机文件:

[first]
localhost ansible_connection=ssh user=root

我的剧本:

cat  test.yml
------------------------------------------
- name : Conditional Test
  hosts: all
  gather_facts: no
  tasks:
    - name: mkdir react prod environments
      shell: cd /etc && mkdir ProdReact
      when:
        - environment == "prod"
        - type == "react"

    - name: mkdir react  for test environments
      shell: cd /etc && mkdir React
      when:
        - environment == "test"
        - type == "react"

    - name: mkdir nodejs  for prod environments
      shell: cd /etc && mkdir ProdNodejs
      when:
        - environment == "prod"
        - type == "nodejs"

    - name: mkdir nodejs  for test environments
      shell: cd /etc && mkdir Nodejs
      when:
        - environment == "test"
        - type == "nodejs"

执行的输出:



[WARNING]: Found variable using reserved name: environment


PLAY [Deploy] ***************************************************************************************************************************

TASK [mkdir react prod environments] ****************************************************************************************************
skipping: [localhost]

TASK [mkdir react  for test environments] ***********************************************************************************************
skipping: [localhost]

TASK [mkdir nodejs  for prod environments] **********************************************************************************************
skipping: [localhost]

TASK [mkdir nodejs  for test environments] **********************************************************************************************
skipping: [localhost]

PLAY RECAP ******************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=0    skipped=4    rescued=0    ignored=0

从现在开始感谢!

问题的原因就在你输出的第一行

[WARNING]: Found variable using reserved name: environment

您使用的变量名称与 reserved one for ansible 冲突(应该为任务、游戏、角色...保存环境变量)

只需重命名为其他名称,它将按预期工作。

固定剧本示例:

---
- name: Conditional Test
  hosts: localhost
  gather_facts: false
  tasks:
    - name: mkdir react prod app_envs
      debug:
        msg: prod and react
      when:
        - app_env == "prod"
        - app_type == "react"

    - name: mkdir react  for test app_envs
      debug:
        msg: test and react
      when:
        - app_env == "test"
        - app_type == "react"

    - name: mkdir nodejs  for prod app_envs
      debug:
        msg: prod and nodejs
      when:
        - app_env == "prod"
        - app_type == "nodejs"

    - name: mkdir nodejs  for test app_envs
      debug:
        msg: test and nodejss
      when:
        - app_env == "test"
        - app_type == "nodejs"

给出:

$ ansible-playbook play.yml -e app_env=test -e app_type=nodejs

PLAY [Conditional Test] *******************************************************************************************************************************************************************************************

TASK [mkdir react prod app_envs] **********************************************************************************************************************************************************************************
skipping: [localhost]

TASK [mkdir react  for test app_envs] *****************************************************************************************************************************************************************************
skipping: [localhost]

TASK [mkdir nodejs  for prod app_envs] ****************************************************************************************************************************************************************************
skipping: [localhost]

TASK [mkdir nodejs  for test app_envs] ****************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "test and nodejss"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0