当前位置: 首页 > news >正文

Ansible列出常见操作系统的发行版,Ansible中使用facts变量的两种方式

目录

  • 常规操作系统发行版与操作系统家族
    • 1. RedHat 发行版家族
    • 2. Debian 发行版家族
    • 3. Suse 发行版家族
    • 4. Gentoo 发行版家族
    • 5. Archlinux 发行版家族
    • 6. Mandrake 发行版家族
    • 7. Solaris 发行版家族
    • 8. AIX
    • 9. Alpine 操作系统家族
    • 10. macOS – Darwin
    • 11. FreeBSD
    • 12. HP-UX
    • 13. Windows
    • 14. 其他发行版示例
    • 15. ansible变量的两种使用方式 - ansible_facts字典引用、ansible_xxx_xxx变量名

Ansible是系统管理员、开发工程师、网络工程师等常用的自动化工具,通过编写Playbook,可以执行不同的管理任务。当需要根据不同的操作系统执行对应的操作的时候,就需要在Playbook中提前对目标操作系统进行检测,此时就需要用到ansible的内置变量 - facts的值。通过判断变量 ansible_facts['os_family']或者 ansible_facts[distribution]实现对远程被控主机操作系统家族以及发行版的检测。

在这里插入图片描述

常规操作系统发行版与操作系统家族

1. RedHat 发行版家族

下面的发行版均是来自RedHat 操作系统家族的成员:

  • RedHat
  • CentOS
  • Fedora
  • Scientific
  • CloudLinux
  • OracleLinux
  • Amazon
  • XenServer
  • OpenEuler

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "RedHat" # CentOS 8, CentOS 7,Fedora 33, Amazon Linux 2, e.t.c# ansible_distribution
when: ansible_facts['distribution'] == "CentOS" # CentOS 8,7,6
when: ansible_facts['distribution'] == "Amazon" # Amazon Linux
when: ansible_facts['distribution'] == "Fedora" # Fedora 33,32,31,..
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8") # CentOS 8 only

2. Debian 发行版家族

Debian家族的发行版成员主要如下:

  • Debian
  • Ubuntu

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Debian" # Debian 10,9,..,Ubuntu 20.04,18.04,e.t.c# ansible_distribution
when: ansible_facts['distribution'] == "Debian" # Debian 10,9,8,..
when: ansible_facts['distribution'] == "Ubuntu" # Ubuntu 20.04, 18.04, 16.04,..
when: (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "10") # Only Debian 10
when: (ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] == "20") # Only Ubuntu 20.04

3. Suse 发行版家族

SUSE发行版系列的主要成员如下:

  • SUSE
  • SLED – SUSE Linux Enterprise Desktop
  • SLES – SUSE Linux Enterprise Server

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Suse"# ansible_distribution
when: ansible_facts['distribution'] == "SLES"

4. Gentoo 发行版家族

主要成员如下:

  • Gentoo

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Gentoo"# ansible_distribution
when: ansible_facts['distribution'] == "Gentoo"

5. Archlinux 发行版家族

主要成员如下:

  • Archlinux

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Archlinux"# ansible_distribution
when: ansible_facts['distribution'] == "Archlinux"

6. Mandrake 发行版家族

主要成员如下:

  • Mandriva

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Mandrake"# ansible_distribution
when: ansible_facts['distribution'] == "Mandriva"

7. Solaris 发行版家族

主要成员如下:

  • Solaris
  • Nexenta
  • OmniOS
  • OpenIndiana
  • SmartOS

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Solaris"# ansible_distribution
when: ansible_facts['distribution'] == "OmniOS"
when: ansible_facts['distribution'] == "SmartOS"

8. AIX

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "AIX"

9. Alpine 操作系统家族

主要成员如下:

  • Alpine Linux

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Alpine"# ansible_distribution
when: ansible_facts['distribution'] == "Alpine"

10. macOS – Darwin

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Darwin"

11. FreeBSD

主要成员如下:

  • FreeBSD

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "FreeBSD"

12. HP-UX

主要成员如下:

  • HP-UX

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "HP-UX"

13. Windows

检测操作系统家族和发行版的使用方式如下:

# ansible_os_family
when: ansible_facts['os_family'] == "Windows"

14. 其他发行版示例

  • VMware ESXi
    检测操作系统家族和发行版的使用方式如下:
# ansible_distribution
when: ansible_facts['distribution'] == "VMwareESX"
  • OpenWrt
    检测操作系统家族和发行版的使用方式如下:
# ansible_distribution
when: ansible_facts['distribution'] == "OpenWrt"
  • CoreOS
    检测操作系统家族和发行版的使用方式如下:
# ansible_distribution
when: ansible_facts['distribution'] == "Coreos"
  • ALT Linux
    检测操作系统家族和发行版的使用方式如下:
# ansible_distribution
when: ansible_facts['distribution'] == "Altlinux"

15. ansible变量的两种使用方式 - ansible_facts字典引用、ansible_xxx_xxx变量名

除了上述的通过ansible_facts内置变量查询操作系统发行版的方式之外,还可以通过setup模块输出的完整变量名的方式来查询。具体如下:

列出所有的facts变量:

albertqee@ZBG7W:postgresql-17.5$ ansible all -i 'u24u04s1,' -m setup | egrep family
[WARNING]: Platform linux on host u24u04s1 is using the discovered Python
interpreter at /usr/bin/python3.12, but future installation of another Python
interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-
core/2.18/reference_appendices/interpreter_discovery.html for more information."ansible_os_family": "Debian",
albertqee@ZBG7W:postgresql-17.5$

在playbook中使用两种方式列出操作系统发行版:

#*************************************************************************
#    > File Name: main.yml
#    > Author: Albert Qee
#    > Created Time: 2025年07月25日 星期五 22时25分19秒
#    > Description: get os family
#    > Usage: ansible-playbook -i 'u24u04s1,' main.yml 
# ************************************************************************- hosts: all remote_user: rootgather_facts: truetasks:- name: display ip addressdebug:msg: "The IP address is {{ ansible_default_ipv4['address'] }}"  - name: display os familydebug:var: ansible_facts['os_family']          # ansible_facts[]字典的形式引用,此时索引名不带前缀的ansible_- name: another way to disply os familydebug:var: ansible_os_family                   # 直接使用setup模块输出的完整变量名的形式引用变量,此时变量名带前缀的ansible_

上述playbook的执行结果如下:

albertqee@ZBG7W:potgresql$ ansible-playbook -i 'u24u04s1,' main.yml PLAY [all] ***********************************************************************************************************************************************************************************TASK [Gathering Facts] ***********************************************************************************************************************************************************************
[WARNING]: Platform linux on host u24u04s1 is using the discovered Python interpreter at /usr/bin/python3.12, but future installation of another Python interpreter could change the meaning
of that path. See https://docs.ansible.com/ansible-core/2.18/reference_appendices/interpreter_discovery.html for more information.
ok: [u24u04s1]TASK [display ip address] ********************************************************************************************************************************************************************
ok: [u24u04s1] => {"msg": "The IP address is 192.168.122.125"
}TASK [display os family] *********************************************************************************************************************************************************************
ok: [u24u04s1] => {"ansible_facts['os_family']": "Debian"            # 通过ansible_facts['os_family]这种ansible_facts字典的方式引用
}TASK [another way to disply os family] *******************************************************************************************************************************************************
ok: [u24u04s1] => {"ansible_os_family": "Debian"                     # 通过直接的完整变量名的方式引用
}PLAY RECAP ***********************************************************************************************************************************************************************************
u24u04s1                   : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   albertqee@ZBG7W:potgresql$
http://www.lryc.cn/news/600702.html

相关文章:

  • 定义域第一题
  • InfluxDB Flux 查询协议实战应用(二)
  • 修改site-packages位置与pip配置
  • 网络:应用层
  • docker安装问题汇总
  • 一文速通《多元函数微分学》
  • AI Agent开发学习系列 - langchain之LCEL(4):Memory
  • x86汇编语言入门基础(三)汇编指令篇5 串操作
  • 【架构】Docker简单认知构建
  • JAVA学习-练习试用Java实现“深度优先搜索(DFS):实现八数码问题的解法(最短路径搜索)”
  • LangChain4j低阶+高阶Api+日志配置+监听器+重试机制+超时机制
  • 【LeetCode 热题 100】131. 分割回文串——回溯
  • 算法竞赛阶段二-数据结构(35)数据结构单链表模拟实现
  • Android-广播详解
  • golang实现一个定时引擎,功能包括按照corntab的时间任务实时增加、修改、删除定时任务
  • 常见sql深入优化( 二)
  • 一文学会c++list
  • 激光雷达-相机标定工具:支持普通相机和鱼眼相机的交互式标定
  • 二叉搜索树(Binary Search Tree)详解与java实现
  • Linux 如何统计系统上各个用户登录(或者登出)记录出现的次数?
  • Android-三种持久化方式详解
  • 摘录-打造第二大脑
  • J2EE模式---表现层集成模式
  • C++ TAP(基于任务的异步编程模式)
  • Web后端进阶:springboot原理(面试多问)
  • React入门学习——指北指南(第五节)
  • JavaScript手录06-函数
  • 【RK3568 PWM 子系统(SG90)驱动开发详解】
  • 数据赋能(336)——技术平台——智能化运营
  • Java动态调试技术原理