为什么Ansible上的pre_task将Ansible_python_version验证为2.7,然后验证为3.6



在我的剧本中,我正在尝试验证Python版本。在这个操作系统(Linux(上,我有两个版本的Python:

python --version
Python 2.7.5
python3 --version
Python 3.6.8

我设置了一个pre_task来验证Ansible版本和Python版本(示例(:

pre_tasks:
- name: Validate we run with the correct Ansible version
register: data
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."
delegate_to: localhost
run_once: true
- name: Get facts
register: facts
setup:
filter: ansible_python_version
run_once: true
- name: Debug facts
debug:
var: facts

不幸的是,当我运行这个角色时,它失败了,因为它似乎是先读Python 2,然后读Python 3。

这里重要的一点是,我已经删除了适用于Python2的Ansible示例:

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_task时,它失败如下:

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_to: localhost参数,它首先会找到Python2,我认为这是失败的原因(示例(:

pre_tasks:
- name: Validate we run with the correct Ansible version
register: data
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('2.7.5', '>=')
fail_msg: "'Python' version must be 3.6.0 and Ansible 2.8.0 minimal version."
run_once: true
- name: Get facts
register: facts
setup:
filter: ansible_python_version
delegate_to: localhost
run_once: true
- name: Debug facts
debug:
var: facts

输出:

ok: [hostname] => {
"facts": {
"ansible_facts": {
"ansible_python_version": "2.7.5"
},
"changed": false,
"failed": false
}
}

这是一个错误还是我缺少一个配置?

问题的解决方案(如果您已将组vars设置为ansible_python_interpreter: /usr/bin/python3(:

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"
}

如果首选的方式是对解释器进行全局配置,则可以像在[默认值]下的主ansible.cfg中那样进行,例如:

interpreter_python = /usr/bin/python3

请记住,不能同时定义两者。ansible.cfg文件上的global,或者针对每个环境。有关更多信息,请阅读官方文档(口译员发现(。

最新更新