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 .'s 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.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
TobyG
  • 1,692
  • 3
  • 21
  • 36
  • 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 '16 at 21:56
  • Can you post your playbook? (whatever you have) – helloV Sep 29 '16 at 22:20

5 Answers5

40

There is a test for it:

{{ ansible_distribution_version is version('12.04', '>=') }}

{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
Bert Peters
  • 1,505
  • 13
  • 30
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • 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 03 '16 at 10:36
  • 2
    See [this](http://stackoverflow.com/a/11887885/2795592) answer about StrictVersion vs LooseVersion. – Konstantin Suvorov Oct 03 '16 at 11:07
  • 3
    FYI, in Ansible 2.5 version_compare was renamed to version. – James Nov 06 '20 at 09:04
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

**

Fuji Komalan
  • 1,979
  • 16
  • 25
  • 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 '19 at 15:25
  • 1
    version isn't a filter at least in ansible 2.9, see usage at https://github.com/systemli/ansible-role-sshd/pull/24/files – timfeirg Dec 28 '19 at 14:26
5

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) }}
...
ProfHase85
  • 11,763
  • 7
  • 48
  • 66
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

Milad Jahandideh
  • 490
  • 1
  • 4
  • 13
-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"
brunorey
  • 2,135
  • 1
  • 18
  • 26
sebastienvg
  • 303
  • 1
  • 7
  • 5
    Using shell instead of modules is ansible anti-pattern. – Konstantin Suvorov Sep 30 '16 at 06:25
  • Also, won't comparing strings not necessarily place the version in the correct order? (I've not tests this) – TobyG Oct 03 '16 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 '18 at 02:55