1

I am trying to do a simple integer comparison in a conditional but something is up, and it is not evaluating properly.

Code within playbook:

 - name: Check the version of the current sql database on both dispatchers and save that value.
 command: /usr/bin/mysql -V
 changed_when: false
 register: sqlversion
 - name: Print to the screen the current sql database version.
 debug:
 var: "{{ sqlversion.stdout.split()[4] | regex_search('[0-9]+\\.[0-9]+') | replace(\".\",'') }}"
 register: w
 - name: Show the value of the variable.
 debug:
 var: w
 - name: Test result
 command: w
 when: ( w | int < 55 )

The output of the command module (ultimately to get the 5.5 part of the 5.5.43 number:

mysql Ver 14.14 Distrib 5.5.43, for Linux (x86_64) using readline 5.2

My actual run in which the Test result task "fails" as in it runs when it should not, because of the evaluation problem:

TASK [Check the version of the current sql database on both dispatchers and save that value.] ***********
ok: [server2]
ok: [server1]
TASK [Print to the screen the current sql database version.] ********************************************
ok: [server1] => {
 "55": "55"
}
ok: [server2] => {
 "55": "55"
}
TASK [Show the value of the variable.] ******************************************************************
ok: [server1] => {
 "w": {
 "55": "55",
 "changed": false,
 "failed": false
 }
}
ok: [server2] => {
 "w": {
 "55": "55",
 "changed": false,
 "failed": false
 }
}
TASK [Test result] **************************************************************************************
changed: [server1]
changed: [server2]

This is not right or expected, clearly the last task shows "changed" in that it ran, and evaluated the condition as true when it shouldn't be. Instead with how I coded the conditional, it should be skipped instead! Additionally, if I take away the " | int" then it always skips no matter what the number(s) are.

What's the issue here? There's got to be a way to make this work.

U880D
12.8k7 gold badges40 silver badges88 bronze badges
asked Sep 1, 2022 at 20:55
3
  • Note the dot must be escaped, '[0-9]+\\.[0-9]+' Commented Sep 1, 2022 at 21:00
  • Understood, and fixed that, but no change with the underlying problem. Commented Sep 1, 2022 at 21:13
  • 1
    Do not register w. Put the declaration into the vars. See below. Then, the condition should work as expected. Commented Sep 1, 2022 at 22:12

2 Answers 2

3

Simplify the parsing

w: "{{ sqlversion.stdout|split(',')|first|split()|last }}"

gives

w: 5.5.43

Use the test version. See Comparing versions. For example,

 - debug:
 msg: Version is lower than 5.6
 when: w is version('5.6', '<')

Example of a complete playbook for testing

- hosts: localhost
 vars:
 sqlversion:
 stdout: "mysql Ver 14.14 Distrib 5.5.43, for Linux (x86_64) using readline 5.2"
 w: "{{ sqlversion.stdout|split(',')|first|split()|last }}"
 tasks:
 - debug:
 var: w
 - debug:
 msg: Version is lower than 5.6
 when: w is version('5.6', '<')

If the filter split is not available use the method twice

w: "{{ (sqlversion.stdout.split(',')|first).split()|last }}"
answered Sep 1, 2022 at 22:05
Sign up to request clarification or add additional context in comments.

3 Comments

Vladimir, I tried to implement this, but I got an error right during my debug print of the variable: fatal: [server1]: FAILED! => {"msg": "template error while templating string: no filter named 'split'. String: {{ sqlversion|split(',')|first|split()|last }}"} Not sure if it matters, but I also am forced on this series of machines to use ansible 2.9. I know split must exist as a filter because I used it in my own example, so not sure why it's complaining.
If the filter split is not available use the method twice. I added an example. The filter split is available since 2.11
You are a scholar and a gentleman sir, when using the code from the line because the split filter is not available, it now gives me the results and comparisons that I expected. This is the answer for people forced into older Ansible version usage.
1

... but I also am forced on this series of machines to use Ansible 2.9 ...

As mentioned by Vladimir Botka

If the filter split is not available use the method twice. I added an example. The filter split is available since 2.11

the split filter for manipulation strings is available as of Ansible v2.11 but you can just use the Python method .split().

As a workround in Ansible v2.9 or 2.10 one could also implement a Custom Filter Plugin and so a simple Ansible Jinja2 filter to split a string into a list. By doing this a filter like | split() can made be available in older versions.

answered Sep 2, 2022 at 7:14

1 Comment

Thanks for the information, I will definitely keep it in mind.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.