33

For a role I'm developing I need to verify that the kernel version is greater than a particular version.

I've found the ansible_kernel fact, but is there an easy way to compare this to other versions? I thought I might manually explode the version string on the dots (.) and compare the numbers, but I can't even find a friendly filter to explode the version string out, so I'm at a loss.

2
  • for splitting, since ansible 2.0, you can {{ variable.split('.') }}; you can then use a loop using with_together to compare major, minor and patch version each other
    – guido
    Sep 29, 2016 at 21:56
  • Can you post your playbook? (whatever you have)
    – helloV
    Sep 29, 2016 at 22:20

5 Answers 5

40

There is a version test for it:

{{ ansible_distribution_version is version('12.04', '>=') }}
    
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}

Prior to Ansible 2.5, all tests were also provided as filters, so, the same was achievable with a filter, named version_compare, but in current versions of Ansible, the test was renamed and, overall, the tests and filters have been clearly disambiguated

{{ ansible_distribution_version | version_compare('12.04', '>=') }}    
    
{{ sample_version_var | version_compare('1.0', operator='lt', strict=True) }}
2
  • 1
    Thanks @konstantin-suvorov. I've now found the docs on this, but can't see find an explanation of what 'strict' does. Do you know where I can find this info?
    – TobyG
    Oct 3, 2016 at 10:36
  • 2
    See this answer about StrictVersion vs LooseVersion. Oct 3, 2016 at 11:07
7

To Print the host IP address if the kernel version is less than 3

Ansible Version : 2.0.0.2

---
- hosts: all
  vars:
   kernel_version: "{{ ansible_kernel }}"
  tasks:
   - name: 'kernel version from facts'
     debug:
      msg: '{{ansible_all_ipv4_addresses}} {{ansible_kernel}}'
     when: ansible_kernel |  version_compare('3','<')

**

In 2.5 version_compare was renamed to version

**

2
  • 1
    Though this is a late response, I feel as though this is the better answer. Can you edit your answer to include a link to the documentation as @konstantin-suvorov did to make the best answer?
    – Cody B
    Apr 18, 2019 at 15:25
  • 1
    version isn't a filter at least in ansible 2.9, see usage at github.com/systemli/ansible-role-sshd/pull/24/files
    – timfeirg
    Dec 28, 2019 at 14:26
6

For ansible>=2.9 this won't work, as the test syntax is now strictly separated from filters.

https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html

The working solution would be:

{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
0

To compare a version number, such as checking if the ansible_facts['distribution_version'] version is greater than or equal to ‘12.04’, you can use the version test.

{{ ansible_facts['distribution_version'] is version('12.04', '>=') }}

When using version in a playbook or role, don’t use {{ }} as described in the FAQ

vars:
    my_version: 1.2.3

tasks:
    - debug:
        msg: "my_version is higher than 1.0.0"
      when: my_version is version('1.0.0', '>')

Check Ansible Doc for more info

-4

Have you thought of using shell module instead? for example:

   - name: Get Kernel version
     shell: uname -r | egrep '^[0-9]*\.[0-9]*' -o
     register: kernel_shell_output

   - debug: msg="{{ kernel_shell_output.stdout}}"

   - name: Add cstate and reboot bios if kernel is 4.8
     shell: echo "do what yo need to do"
     when: kernel_shell_output.stdout == "4.8"
3
  • 6
    Using shell instead of modules is ansible anti-pattern. Sep 30, 2016 at 6:25
  • Also, won't comparing strings not necessarily place the version in the correct order? (I've not tests this)
    – TobyG
    Oct 3, 2016 at 10:31
  • Shell is one of things that you should generally try to avoid (ie. there by monsters, there)... unless there's "no other way." When there are builtin filters, like version_compare, there's obviously "a better way."
    – RVT
    Jul 14, 2018 at 2:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.