gpt4 book ai didi

Ansible 测试变量以什么开头

转载 作者:行者123 更新时间:2023-12-02 00:14:33 26 4
gpt4 key购买 nike

我需要能够安装 MySQL 库。 Python 有 1 个包用于 v2,另一个包用于 v3。我需要能够告诉 Ansible 要安装哪个包。

- name: Ensure MySQL-python is installed
pip:
name: MySQL-python
state: present
become: true
when: python_version is regex("^2.*")

- name: Ensure mysqlclient is installed
pip:
name: mysqlclient
state: present
become: true
when: python_version is regex("^3.*")

正则表达式是有效的,但 Ansible 会跳过它们,即使这样:

- debug:
var: python_version

返回这个:

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
"python_version": "2.7.10"
}

最佳答案

regex使用 ansible 2.7.9 为我工作。下面的例子

      vars:
python_version: "2.7.10"
tasks:
- debug:
msg: Python version 2
when: python_version is regex('^2.*')

给予

    "msg": "Python version 2"

Version Comparison对于复杂的测试更方便。下面的示例给出了相同的结果。

        - debug:
msg: Python version 2
when:
- python_version is version('2', '>=')
- python_version is version('3', '<')

测试 regex 记录在 Ansible 2.8 中首次。在早期版本中,仅记录了测试 searchmatch。在当前source searchmatch 测试是在 regex

之上实现的
    def match(value, pattern='', ignorecase=False, multiline=False):
return regex(value, pattern, ignorecase, multiline, 'match')

def search(value, pattern='', ignorecase=False, multiline=False):
 return regex(value, pattern, ignorecase, multiline, 'search')

关于Ansible 测试变量以什么开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com