在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
从现在开始谢谢!
答案 0 :(得分: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