python - 为什么 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
不幸的是,当我运行这个角色时它失败了,因为它似乎是先阅读 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_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
}
}
这是一个错误还是我缺少配置?
解决方案
问题的解决方案(如果您已将组 var 设置为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"
}
如果首选方法是让解释器的全局配置可以像在 [defaults] 下的 main ansible.cfg 中那样完成,例如:
interpreter_python = /usr/bin/python3
请记住,您不能同时定义两者。ansible.cfg 文件中的全局变量或每个环境的全局变量。有关更多信息,请阅读官方文档(解释器发现)。
推荐阅读
- cmd - 如何将新创建的文件从多个目录复制到单个目录 uisng ssis 或 cmd?
- java - java - 如何在java中获取字符串格式时间“2019-01-25 14:34:34.123456789”的纪元以来的纳秒?
- c# - KeyValue 列表使用哪种数据结构
- symfony - 在 Syfmony PHPUnit 测试中模拟两个 ObjectRepositories
- python - 无法使用漂亮的搜素从 Python 中的网页中提取图像
- php - 将长字符串分配给变量和随后将较小的字符串附加到变量之间在性能上有什么区别吗?
- windows - Golang os.Rename(
, ) 在 Windows 中不起作用 - node.js - gyp动词`which`失败错误:未找到:python2
- dart - onTap:在初始化程序错误中只能访问静态成员
- python - 保存 ImageField 时出现 IntegrityError:字段“路径”没有默认值