I want convert an integer variable into a specific string format.
This integer (for example: 56
, 71
, 80
) needs to be converted into a string formatted first_digit.second_digit
(for example 5.6
, 7.1
).
How can I do this?
I tried a mixture of Python and Jinja but it doesn't work.
With:
- name: Install PHP > 5.6
apt:
name:
- "php{{ str(php) | php[0]+'.'+php[1] }}"
when: php > 56
I get this error:
template error while templating string: expected token 'end of print statement', got '['. String: php{{ str(php) | php[0]+'.'+php[1] }}
Given the variable
php: 56
Convert the integer to a string and split the first digit from the rest (this will work while the PHP major version is a single digit)
php_str: "{{ php|string }}"
php_ver: "{{ php_str[0] }}.{{ php_str[1:] }}"
To test the version you can declare a variable. For example,
php_ver_min: '5.5'
and try the debug
- debug:
msg: "Install php{{ php_ver }}"
when: php_ver is version(php_ver_min, '>=')
vars:
php_str: "{{ php|string }}"
php_ver: "{{ php_str[0] }}.{{ php_str[1:] }}"
gives
msg: Install php5.6
Example of a complete playbook for testing
- hosts: localhost
vars:
php: 56
php_ver_min: '5.5'
tasks:
- debug:
msg: "Install php{{ php_ver }}"
when: php_ver is version(php_ver_min, '>=')
vars:
php_str: "{{ php|string }}"
php_ver: "{{ php_str[0] }}.{{ php_str[1:] }}"