我只是想检查我所有服务器上的 ubuntu 版本。基于这个问题我看到 ansible 有一个,ansible_distribution_version
但是这个剧本没有显示我如何简单地让它打印出 ubuntu 版本,即 ubuntu 14.04、16.04 等
答案1
你可以一次做一件事
---
- hosts: localhost
gather_facts: yes
become: false
tasks:
- name: Distribution
debug: msg="{{ ansible_distribution }}"
- name: Distribution version
debug: msg="{{ ansible_distribution_version}}"
- name: Distribution major version
debug: msg="{{ ansible_distribution_major_version }}"
查看结果:
PLAY [localhost] ***********************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [localhost]
TASK [Distribution] ********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Ubuntu"
}
TASK [Distribution version] ************************************************************************************************************************************************
ok: [localhost] => {
"msg": "18.04"
}
TASK [Distribution major version] ******************************************************************************************************************************************
ok: [localhost] => {
"msg": "18"
}
PLAY RECAP *****************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
或者您可以使用更高级的配置迭代事实:
- hosts: localhost
gather_facts: yes
become: false
tasks:
- name: System details
debug: msg="{{ item }}"
with_items:
- "{{ ansible_distribution }}"
- "{{ ansible_distribution_version }}"
- "{{ ansible_distribution_major_version }}"
更紧凑的结果:
PLAY [localhost] ***********************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [localhost]
TASK [System details] ******************************************************************************************************************************************************
ok: [localhost] => (item=Ubuntu) => {
"msg": "Ubuntu"
}
ok: [localhost] => (item=18.04) => {
"msg": "18.04"
}
ok: [localhost] => (item=18) => {
"msg": "18"
}
PLAY RECAP *****************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
shell
在这两种情况下,使用事实而不是模块来获取信息都是很好的做法command
。
答案2
经过深入研究,找到了 Ubuntu 20.04 版本,然后我们发布了一个使用 ansible 版本 2.5.1 的版本
- hosts: localhost
become: true
gather_facts: yes
tasks:
- name: System details
debug:
msg: "{{ ansible_facts['lsb']['release'] }}"
- name: ubuntu 18
shell: echo "hello 18"
register: ub18
when: ansible_facts['lsb']['release'] == "18.04"
- debug:
msg: "{{ ub18 }}"
- name: ubuntu 20
shell: echo "hello 20"
register: ub20
when: ansible_facts['lsb']['release'] == "20.04"
- debug:
msg: "{{ ub20 }}"
答案3
我明白了,要检查操作系统版本,这里是剧本
---
- hosts: all
gather_facts: False
tasks:
- name: Check Dist Version
shell: cat /etc/os-release
register: response
- debug: msg="{{ response.stdout }}"