Reputation: 522
I need to be able to install a MySQL library. Python has 1 package for v2 and another for v3. I need to be able to tell Ansible which package to install.
- 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.*")
The regular expression is valid but Ansible is skipping them both even though this:
- debug:
var: python_version
returns this:
TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
"python_version": "2.7.10"
}
Upvotes: 3
Views: 13511
Reputation: 68124
regex works for me with ansible 2.7.9. The example below
vars:
python_version: "2.7.10"
tasks:
- debug:
msg: Python version 2
when: python_version is regex('^2.*')
gives
"msg": "Python version 2"
Version Comparison is more convenient for complex tests. The example below gives the same result.
- debug:
msg: Python version 2
when:
- python_version is version('2', '>=')
- python_version is version('3', '<')
The test regex
is documented in Ansible 2.8 for the first time. In earlier versions, only the tests search
and match
are documented. In the current source the tests search
and match
are implemented on top of 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')
Upvotes: 7
Reputation: 3148
Like Vladimir said.
Another improvement might be the source of python_version
. When using gather_facts
you can do it without regexp.
---
- hosts: localhost
gather_facts: True
tasks:
- name: do some stuff
debug:
msg: do something because it is python 3
when: ansible_facts.python.version.major is version('3', '=')
- name: do other stuff
debug:
msg: I don't support legacy software
when: ansible_facts.python.version.major is version('2', '=')
Upvotes: 1