为什么Ansible上的pre_任务会将Ansible_python_版本验证为2.7版,然后验证为3.6版
在我的剧本中,我试图验证Python版本。在这个操作系统(Linux)上,我有两个Python版本:为什么Ansible上的pre_任务会将Ansible_python_版本验证为2.7版,然后验证为3.6版,python,ansible,Python,Ansible,在我的剧本中,我试图验证Python版本。在这个操作系统(Linux)上,我有两个Python版本: python --version Python 2.7.5 python3 --version Python 3.6.8 我设置了一个pre_任务来验证Ansible版本和Python版本(示例): 不幸的是,当我运行这个角色时,它失败了,因为它似乎是先读取Python2,然后读取Python3 这里要提到的重要一点是,我删除了Ansible for Python2样本: ansible -
python --version
Python 2.7.5
python3 --version
Python 3.6.8
我设置了一个pre_任务来验证Ansible版本和Python版本(示例):
不幸的是,当我运行这个角色时,它失败了,因为它似乎是先读取Python2,然后读取Python3
这里要提到的重要一点是,我删除了Ansible for Python2样本:
ansible --version
ansible 2.9.1
config file = /ripl-nginx/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Aug 7 2019, 17:28:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
当我执行pre_任务时,它失败如下:
fatal: [hostname -> localhost]: FAILED! => {
"assertion": "ansible_python_version is version('3.6.0', '>=')",
"changed": false,
"evaluated_to": false,
"msg": "'Python' version must be 3.6.0 and Ansible 2.8.0 minimal version."
}
如果我将验证修改为2.7.5,例如:
- ansible_python_version is version('2.7.5', '>=')
它可以完美地工作:
ok: [hostname] => {
"facts": {
"ansible_facts": {
"ansible_python_version": "3.6.8"
},
"changed": false,
"failed": false
}
}
我注意到,如果我删除delegate\u to:localhost
参数,它首先会找到Python2,我认为这是失败的原因(示例):
输出:
ok: [hostname] => {
"facts": {
"ansible_facts": {
"ansible_python_version": "2.7.5"
},
"changed": false,
"failed": false
}
}
这是一个错误还是我缺少配置?问题的解决方案(如果您将组变量设置为ansible\u python\u解释器:/usr/bin/python3
):
输出样本:
TASK [Validate we run with the correct Ansible version] *************************************************************************************************************************************************************************************
ok: [hostname -> hostvars[inventory_hostname]['ansible_python_version']] => {
"changed": false,
"msg": "All assertions passed"
}
TASK [Debug] ********************************************************************************************************************************************************************************************************************************
ok: [hostname] => {
"hostvars[inventory_hostname]['ansible_python_version']": "3.6.8"
}
如果首选的方法是对解释器进行全局配置,则可以像在[defaults]下的主ansible.cfg中那样进行,例如:
interpreter_python = /usr/bin/python3
请记住,不能同时定义这两个。在ansible.cfg文件上或针对每个环境创建全局文件。有关更多信息,请阅读官方文档()
pre_tasks:
- name: Validate we run with the correct Ansible version
delegate_to: hostvars[inventory_hostname]['ansible_python_version']
assert:
that:
- ansible_version is defined
- ansible_version.full is version('2.8.0.0', '>=')
- ansible_python_version is defined
- ansible_python_version is version('3.6.0', '>=')
fail_msg: "'Python' version must be 3.6.0 and Ansible 2.8.0 minimal version."
run_once: True
TASK [Validate we run with the correct Ansible version] *************************************************************************************************************************************************************************************
ok: [hostname -> hostvars[inventory_hostname]['ansible_python_version']] => {
"changed": false,
"msg": "All assertions passed"
}
TASK [Debug] ********************************************************************************************************************************************************************************************************************************
ok: [hostname] => {
"hostvars[inventory_hostname]['ansible_python_version']": "3.6.8"
}
interpreter_python = /usr/bin/python3