如何在 Ansible 中比较内核(或其他)版本号
How to compare kernel (or other) version numbers in Ansible
对于我正在开发的角色,我需要验证内核版本是否大于特定版本。
我找到了 ansible_kernel
事实,但是有没有一种简单的方法可以将其与其他版本进行比较?我想我可能会在 .
上手动分解版本字符串并比较数字,但我什至找不到友好的过滤器来分解版本字符串,所以我很茫然。
您是否考虑过使用 shell 模块?例如:
- 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"
它有一个test:
{{ ansible_distribution_version is version('12.04', '>=') }}
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
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
**
对于 ansible>=2.9
这将不起作用,因为测试语法现在与过滤器严格分开。
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html
可行的解决方案是:
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
...
对于我正在开发的角色,我需要验证内核版本是否大于特定版本。
我找到了 ansible_kernel
事实,但是有没有一种简单的方法可以将其与其他版本进行比较?我想我可能会在 .
上手动分解版本字符串并比较数字,但我什至找不到友好的过滤器来分解版本字符串,所以我很茫然。
您是否考虑过使用 shell 模块?例如:
- 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"
它有一个test:
{{ ansible_distribution_version is version('12.04', '>=') }}
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
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
**
对于 ansible>=2.9
这将不起作用,因为测试语法现在与过滤器严格分开。
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html
可行的解决方案是:
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
...