[英]How to compare kernel (or other) version numbers in Ansible
對於我正在開發的角色,我需要驗證內核版本是否高於特定版本。
我找到了ansible_kernel
的事實,但是有沒有一種簡單的方法可以將它與其他版本進行比較? 我想我可能會在.
的並比較數字,但我什至找不到一個友好的過濾器來分解版本字符串,所以我很茫然。
有一個測試:
{{ ansible_distribution_version is version('12.04', '>=') }}
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}
內核版本低於3時打印主機IP地址
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','<')
**
在 2.5 version_compare 被重命名為 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_facts['distribution_version']版本是否大於等於'12.04',可以使用版本測試。
{{ ansible_facts['distribution_version'] is version('12.04', '>=') }}
在劇本或角色中使用版本時,不要按照常見問題解答中的說明使用 {{ }}
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', '>')
查看Ansible Doc了解更多信息
你有沒有想過改用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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.