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

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.xdnf.cn/news/16363.html

相关文章:

  • CH341 Linux驱动 没有 /dev/ttyCH341USB0
  • Linux文件系统管理——NFS服务端的安装配置与NFS客户端的安装与挂载实操教程
  • 【AI】联网模式
  • Scrapy分布式爬虫数据统计全栈方案:构建企业级监控分析系统
  • GPU运维常见问题处理
  • 【C++】stack和queue的模拟实现
  • Java基础day17-LinkedHashMap类,TreeMap类和集合工具类
  • 基于POD和DMD方法的压气机叶片瞬态流场分析与神经网络预测
  • 基于遗传算法的多无人车协同侦察与安全保护策略优化
  • CUDA杂记--FP16与FP32用途
  • Redis面试精讲 Day 5:Redis内存管理与过期策略
  • 汇编语言中的通用寄存器及其在逆向工程中的应用
  • 计划任务(at和cron命令介绍及操作)
  • MySQL事务原理
  • 应用程序 I/O 接口
  • 【MySQL 数据库】MySQL基本查询(第二节)
  • 系统性学习C语言-第二十三讲-文件操作
  • 谷歌无法安装扩展程序解决方法(也许成功)
  • Kubernetes 与 Docker的爱恨情仇
  • STM32-定时器的基本定时/计数功能实现配置教程(寄存器版)
  • 【工具】好用的浏览器AI助手
  • 用unity开发教学辅助软件---幼儿绘本英语拼读
  • 【深度学习新浪潮】什么是GUI Agent?
  • java面试复习(spring相关系列)
  • 【机器学习-2】 | 决策树算法基础/信息熵
  • 【RocketMQ】一分钟了解RocketMQ
  • Earth靶机攻略
  • linux线程概念和控制
  • 字符串缓冲区和正则表达式
  • Mingw 与MSYS2 与Cygwin区别