Ahmet DAL
Ahmet DAL

Reputation: 4700

Ansible wildcard condition

I am using ansible version 1.6.6. My remote server CentOs 6.4 minimal. On remote server, Python 2.6.6 is installed. So I want to install 2.7.* on remote server.

I have to upgrade it to 2.7.*. I found a tutorial to install it. It is on right here Python2.7 Installation on Centos via yum

My some other remote servers might be ubuntu or another Centos which is already installed Python version 2.7.*. So this must be conditional. But I don't know what python 2.7 version is really gonna be installed. It might be 2.7.3, 2.7.4 or later. I have to use some wildcard condition like;

- name: Check python version
  command: python -V
  register: python_version

- debug: var=python_version.stderr
- name: Install Python2.7
  ......
  when: (ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux') and python_version.stderr != 2.7.*

Is there a way to use wildcard condition for such situation?

Upvotes: 0

Views: 6276

Answers (2)

JR.
JR.

Reputation: 304

This sounds like an excellent use case for Ansible's match function: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings

In your particular example you would use:

when: python_version.stderr is not match("2.7\..*") and (ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux')

Upvotes: 1

Ahmet DAL
Ahmet DAL

Reputation: 4700

I just found it on deep search in Ansible documentation; http://docs.ansible.com/playbooks_conditionals.html#register-variables;

it can be done with function find('your_part') like;

when:  python_version.stderr.find('2.7') != 1 and (ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux')

This will check whether it contains 2.7 string in it.

Upvotes: 4

Related Questions