问题
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 value, but is there an easy way to compare this to other versions? I thought I might manually explode the version string on the '.'s & compare the numbers, but I can't even find a friendly filter to explode the version string out, so I'm at a loss.
Thanks in advance.
T
回答1:
There is a test for it:
{{ ansible_distribution_version | version_compare('12.04', '>=') }}
{{ sample_version_var | version_compare('1.0', operator='lt', strict=True) }}
回答2:
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
**
回答3:
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"
来源:https://stackoverflow.com/questions/39779802/how-to-compare-kernel-or-other-version-numbers-in-ansible