# Parse a variable using a regex and use parsed variable in a "when"

**URL:** <https://forum.ansible.com/t/parse-a-variable-using-a-regex-and-use-parsed-variable-in-a-when/36257>\
**Category:** Ansible Project\
**Created:** [January 27, 2022, 11:08pm UTC](https://forum.ansible.com/t/parse-a-variable-using-a-regex-and-use-parsed-variable-in-a-when/36257 "2022-01-27T23:08:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![o\_haya1](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@o\_haya1](https://forum.ansible.com/u/o_haya1)\
**Post date:** [January 27, 2022, 11:08pm UTC](https://forum.ansible.com/t/parse-a-variable-using-a-regex-and-use-parsed-variable-in-a-when/36257/1 "2022-01-27T23:08:52Z")

</div>

Hi,

I am just starting working with Ansible, and am trying to modify an existing playbook that is used to install MySQL. I need to have it check a Mysql version string, and if it is greater than or equal to a specific version, use that as a conditional in a “when”.

I already have the version string and output it just to see what the version string looks like:

- name: “Ansible | Print a variable”  
debug:  
msg: “The play\_mysql\_version is {{ play\_mysql\_version }}”

The output is:

{  
“msg”: “The play\_mysql\_version is 8.0.28-1.el7”,  
“changed”: false,  
“\_ansible\_verbose\_always”: true,  
“\_ansible\_no\_log”: false  
}

i.e., the version string is " 8.0.28-1.el7 ".

Later in the same playbook, I have a step where I want to have a “when” that checks that the “numeric part” of the version string that is in play\_mysql\_version, i.e. “8.0.28”, is greater than, or equal to, “8.0.28”.

The reason that I need to do this is that, starting at version 8.0.28, there is an additional RPM/dependency that needs to be installed.

So I think I need something (maybe using set\_fact and a regex?) to parse the “8.0.28-1.el7” to get the substring “8.0.28”, and then a “when” that compares that parsed substring (“8.0.28”) to “8.0.28”.

Can someone suggest how to do this?

For the parsing part, I have tried using a set\_fact and a regex like “^(.\*)-1.el7$”, but I think that I am getting the code format wrong.

Also, even if I am able to extract the version, I am not sure how to do the ‘when’.

Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 29, 2022, 12:52am UTC](https://forum.ansible.com/t/parse-a-variable-using-a-regex-and-use-parsed-variable-in-a-when/36257/2 "2022-01-29T00:52:57Z")

</div>

See "Comparing versions"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#comparing-versions](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#comparing-versions)

Test it first, e.g.

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Install additional dependency  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: play\_mysql\_version is version ('8.0.28','\>=')

---

<div class="post-metadata">

**Author:** ![o\_haya1](https://avatars.discourse-cdn.com/v4/letter/o/a9adbd/32.png) [@o\_haya1](https://forum.ansible.com/u/o_haya1)\
**Post date:** [January 30, 2022, 6:37pm UTC](https://forum.ansible.com/t/parse-a-variable-using-a-regex-and-use-parsed-variable-in-a-when/36257/3 "2022-01-30T18:37:01Z")

</div>

That works. One “gotcha” was that I had used to extract out the numeric part of the original version string, using regex\_search() but the result from that was an array,so I to use a “first” to get the actual string, which I then used with the version().

Thanks,  
Jim
