How to compare kernel (or other) version numbers in Ansible

最后都变了- 提交于 2020-01-02 00:56:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!