Ansible and Regex

Upon my Ansible Journey here are some examples that might save you some time.

Ansible Regex Examples:

Title: Separate the version number.
Want to match the version number of a software and assign it a usable variable in Ansible.

Example-12.01.txt

You can use a grep for a pattern, cat or anything that has a dynamic way of grabing the varible.

In ansible task it would be a

- name: Anything goes here.
command: cat /etc/??????
register: version_long

Regex Expression.

(\d.*)(?=.txt)
Explained:
/d will select first digit, .* everyting. ?= up to .txt.

Ansible task with regex:

– name: Grab in version, assign to variables.
set_fact:
versionVariable: “{{ version_long.stdout | regex_search(‘(\d.*)(?=.txt) ‘ ) }}”

12.01 will be assigned to versionVariable.

If you have any questions, comment below. If you get an error with the regex. change (\d.*) to (\\d.*)

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.