1. 操作系統系列?(ansible_os_family)
?
ansible web -m setup -a 'filter=ansible_os_family'
?
2.?操作系統家族為 RedHat 時執行任務
--- - hosts: websrvsremote_user: roottasks:- name: Install package on RedHat systemsyum:name: httpdstate: presentwhen: ansible_os_family == "RedHat"
- 這個任務會在操作系統家族為?
RedHat
?的系統上安裝?httpd
?包。
3.?操作系統家族為 Debian 時執行任務
--- - hosts: websrvsremote_user: roottasks:- name: Install package on Debian systemsapt:name: apache2state: presentwhen: ansible_os_family == "Debian"
- 這個任務會在操作系統家族為?
Debian
?的系統上安裝?apache2
?包。
4.?操作系統為 RedHat 或 Debian 時執行任務
--- - hosts: websrvsremote_user: roottasks:- name: Install common package on RedHat and Debian systemspackage:name: vimstate: presentwhen: ansible_os_family in ["RedHat", "Debian"]
- 這個任務會在操作系統家族為?
RedHat
?或?Debian
?的系統上安裝?vim
?包。
5.?操作系統為 Ubuntu 時執行任務
--- - hosts: websrvsremote_user: roottasks:- name: Install package on Ubuntu systemsapt:name: nginxstate: presentwhen: ansible_distribution == "Ubuntu"
- 這個任務會在操作系統為?
Ubuntu
?的系統上安裝?nginx
?包。
6.?特定版本的操作系統執行任務
--- - hosts: websrvsremote_user: roottasks:- name: Install package on CentOS 8yum:name: httpdstate: presentwhen: ansible_distribution == "CentOS" and ansible_distribution_version == "8"
- 這個任務會在?
CentOS 8
?系統上安裝?httpd
?包。
7.?操作系統家族為 Suse 時執行任務
--- - hosts: websrvsremote_user: roottasks:- name: Install package on Suse systemszypper:name: apache2state: presentwhen: ansible_os_family == "Suse"
- 這個任務會在操作系統家族為?
Suse
?的系統上安裝?apache2
?包。
8.?檢測操作系統是否為 Windows 系統
--- - hosts: windows_machinesremote_user: Administratortasks:- name: Install IIS on Windowswin_chocolatey:name: iisstate: presentwhen: ansible_os_family == "Windows"
- 這個任務會在操作系統家族為?
Windows
?的系統上安裝 IIS。
9.?操作系統家族不為 RedHat 時執行任務
---
- hosts: websrvs
remote_user: root
tasks:
- name: Install package on non-RedHat systems
apt:
name: nginx
state: present
when: ansible_os_family != "RedHat"
- 這個任務會在操作系統家族不為?
RedHat
?的系統上安裝?nginx
?包。
10.?根據操作系統家族執行不同的配置
---
- hosts: websrvs
remote_user: root
tasks:
- name: Configure firewall on RedHat systems
firewalld:
service: http
permanent: yes
state: enabled
when: ansible_os_family == "RedHat"? ? - name: Configure firewall on Debian systems
ufw:
rule: allow
name: 'Apache'
when: ansible_os_family == "Debian"
- 這個任務根據操作系統家族來執行不同的防火墻配置:
RedHat
?使用?firewalld
,Debian
?使用?ufw
。
?