我只是想检查我所有服务器上的 ubuntu 版本。基于这个问题,我看到 ansible 有一个,ansible_distribution_version但这个剧本没有显示我如何简单地打印出 ubuntu 版本,即 ubuntu 14.04、16.04 等
Rob*_*ert 21
你可以在当时做一个
---
- 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 }}"
Run Code Online (Sandbox Code Playgroud)
查看结果:
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
Run Code Online (Sandbox Code Playgroud)
或者您可以使用更高级的配置迭代事实:
- 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 }}"
Run Code Online (Sandbox Code Playgroud)
以及更紧凑的结果:
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
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,是一个很好的做法,使用的事实,而不是获得信息shell或command模块。
| 归档时间: |
|
| 查看次数: |
37393 次 |
| 最近记录: |