Ansible:通过CLI传递变量时,无法触发具有when条件的任务
在Ansible中,我想使用when条件来在远程机器上执行一些shell命令,这取决于条件匹配与否。我发送带有-e(--extra-var)参数的变量,如下所示。但是,没有任务与我的VAR匹配,虽然应该匹配,但总是跳过。我错过什么了吗?有人帮我吗 易读版本Ansible:通过CLI传递变量时,无法触发具有when条件的任务,ansible,ansible-2.x,ansible-inventory,Ansible,Ansible 2.x,Ansible Inventory,在Ansible中,我想使用when条件来在远程机器上执行一些shell命令,这取决于条件匹配与否。我发送带有-e(--extra-var)参数的变量,如下所示。但是,没有任务与我的VAR匹配,虽然应该匹配,但总是跳过。我错过什么了吗?有人帮我吗 易读版本 ansible 2.9.2 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/module
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
您使用的变量名称与冲突(假定为任务、角色等保留环境变量)
只需重命名为其他名称,它就会按预期工作
示例固定剧本:
---
- 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
感谢您的详细修复。我错过了var,它是ansible本身的保留服务器。谢谢!如果这解决了你的问题,你应该接受答案,让其他人知道找到了解决方案(并最终投票表决,这是说谢谢而不是评论的方式)。