当前位置: 首页 > 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.dtcms.com/a/298945.html

相关文章:

  • pytorch学习笔记-自定义卷积
  • 【CTF-WEB-SQL】SQL注入基本流程-错误注入(sql-labs的Less5)(updatexml)
  • 常见代码八股
  • 房价预测|Pytorch
  • 【Android】约束布局总结(1)
  • 数据结构预备知识
  • Linux的生态与软件安装
  • 基于深度学习的图像分类:使用Inception-v3实现高效分类
  • Coze 与 Dify 深度对比:2025 年 AI 智能体平台选型指南
  • 【MySQL】深入浅出事务:保证数据一致性的核心武器
  • GStreamer与OpenCV集成
  • 【RocketMQ】一分钟了解RocketMQ
  • Linux系统调用概述与实现:深入浅出的解析
  • 2025.7.26
  • 50道JavaScript基础面试题:从基础到进阶
  • 【图像分割】记录1:unet, yolov8_seg
  • 【嵌入式电机控制#20】无刷直流电机硬件案例
  • 详解力扣高频SQL50题之619. 只出现一次的最大数字【简单】
  • 【ELasticsearch】节点角色分类与作用解析
  • SQL 通用数据类型
  • C# 判断语句深度解析
  • 详解力扣高频SQL50题之1084. 销售分析 III【简单】
  • 基于 Claude Code 与 BrowserCat MCP 的浏览器自动化全链路构建实践
  • OGG同步Oracle到Kafka不停库,全量加增量
  • 显式等待和隐式等待的区别
  • JavaScript 立即执行函数(IIFE)运行时行为分析笔记
  • 数控滑台的功能与应用
  • 生产环境使用云服务器(centOS)部署和使用MongoDB
  • MongoDB数据模型
  • Zookeeper的简单了解