Ansible:通过CLI传递var时,when条件无法触发任务

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

在Ansible中,我想使用条件在远程计算机上执行某些Shell命令的条件取决于条件匹配与否。我正在发送带有-e(--extra-var)参数的变量,如下所示。但是没有任务与我的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

感谢从现在开始!

ansible ansible-2.x ansible-inventory
1个回答
0
投票

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

[WARNING]: Found variable using reserved name: environment

您正在使用与reserved one for ansible冲突的var名称(应该为任务,角色,角色等使用env vars)]

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

示例固定剧本:

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