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.


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 will select first digit, .* everyting. ?= up to .txt.

Ansible task with regex:

– name: Grab in version, assign to variables.
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.*)