Ansible的 Playbook 模式詳解

目錄

  • 一、Playbook模式
    • 1.1 Playbook 的優勢
    • 1.2 Playbook 的組成
    • 1.3 安裝 httpd 服務案例
    • 1.4 Playbook 命令及常用參數
    • 1.5 Playbook 的語法 —— 權限相關
      • 1. remote_user
      • 2. become
      • 3. become_method
    • 1.6 Playbook 的通知與觸發機制
      • 1. notify
      • 2. handlers
      • 3. 使用示例
      • 4. 使用場景
    • 1.6 Playbook 中的變量
    • 1.7 變量的定義方式
    • 1.8 通過 vars 定義變量示例
    • 1. 為什么使用 vars_files
    • 1.9 變量定義階段
    • 1.10 主 Playbook 調用階段
    • 1.11 Ansible的debug模塊
    • 1.12 Ansible 事實變量(Facts)
      • 1. 操作系統相關
      • 2. 網絡相關
      • 3. 硬件相關
      • 4. 文件系統相關
      • 5. 用戶和組相關
      • 6. 服務相關
      • 7. 其他變量
      • 案例
  • 二、Playbook 中的流程控制
  • Ansible 條件判斷(when)
    • 基礎
      • 1. 基本語法
      • 2. 條件分組(使用括號)
      • 3. 多條件寫法(列表形式)
      • 4. 條件運算
      • 5. 變量判斷實例
    • 2.2 循環(with_items)
      • 1. 批量創建用戶
      • 3. 批量創建用戶和用戶組(循環嵌套)
      • 4. 批量刪除用戶和用戶組(循環嵌套)

一、Playbook模式

Playbook 相比直接調用單個模塊操作遠程服務器,更加強大和靈活。
如果把單個模塊的 ad-hoc 執行類比為在 Linux 系統中輸入一條命令,那么 Playbook 就類似于編寫一份 Shell 腳本 —— 它能夠將多個模塊按順序或邏輯組合起來,實現一整套自動化操作。

需要注意的是,Playbook 依然會用到 ad-hoc 模式中的模塊和參數,只是其編寫方式與 ad-hoc 命令有所不同,更偏向于結構化和可維護的 YAML 格式。

特性ad-hoc 命令(單次執行)Playbook(批量編排)
類比Linux 單條命令Shell 腳本
使用場景臨時性、一次性操作系統化、可復用的自動化任務
寫法形式命令行直接輸入參數與模塊YAML 格式,結構化配置
維護性不易維護,執行記錄不完整易維護,可讀性強,適合版本管理
功能特點快速執行單一任務將多個模塊組合,支持邏輯控制與順序
可擴展性較低,只適合簡單操作較高,可實現復雜的自動化工作流

1.1 Playbook 的優勢

  • 功能更全面:相比 ad-hoc,Playbook 提供更豐富的功能。
  • 可控性強:能清晰定義任務的執行順序及依賴關系。
  • 語法直觀:采用 YAML 格式,結構清晰,易讀易寫。
  • 持久可用:ad-hoc 適合一次性操作,而 Playbook 可長期保存、復用,并適合版本管理。

1.2 Playbook 的組成

一個 Playbook 由一個或多個 play 組成,而每個 play 又由多個 task 任務構成。

  • play:一個完整的部署單元,通常包含以下核心部分:
    • hosts:指定要操作的主機或主機組;如果是多個組,可以用 : 分隔,例如:123:456:789
    • tasks:定義具體要執行的任務(調用模塊及參數)。
    • become:是否以特權用戶(通常是 root)身份執行任務,設置為 yes 時表示啟用。
  • playbook
    • 由一個或多個 play 組成;
    • 每個 play 中可以包含多個 task
    • 通過組合實現復雜的自動化流程。

1.3 安裝 httpd 服務案例

Playbook 文件說明

  • 文件后綴:Playbook 文件以 **.yml** 結尾。
  • 用途:通過 YAML 語法組織多個任務,實現自動化部署與運維操作。
# 定義一個 play(劇本的一個執行單元)
- name: apache          # Play 的名稱,用于描述該 play 的作用hosts: web            # 指定目標主機組,這里是 web,需要在 /etc/ansible/hosts 里定義become: yes           # 是否切換為特權用戶(root)執行,yes 表示啟用tasks:                # 定義任務列表(可以有多個)- name: Install httpd Server  # 任務名稱,用于描述該任務的功能yum:                        # 調用 yum 模塊(用于管理軟件包)name: httpd               # 指定要安裝的軟件包,這里是 httpdstate: present            # 確保 httpd 處于“已安裝”狀態
  • tasks 本質上就是 Ansible 模塊 通過 YAML 格式寫入 Playbook 中。
  • 在生產環境中,為了增強可讀性和可維護性,通常 一個 Playbook 只包含一個 play
  • 如果不同主機需要執行多個 play,可以通過 **include****import_playbook** 來組織多個 Playbook 文件。

1.4 Playbook 命令及常用參數

命令格式

ansible-playbook playbook.yml [選項]

** 常用選項(按使用頻率排序) **

選項/參數說明
--syntax-check語法檢測,檢查 Playbook 是否有語法錯誤
--list-tags列出 Playbook 中定義的所有 tag 標簽
--list-hosts顯示匹配當前 Playbook 的主機列表
--list-tasks列出 Playbook 中的任務列表
-t指定執行特定的標簽(tags)
-T設置 SSH 連接超時時間
-i指定 Inventory 文件(主機清單文件)
-f并發執行的進程數,默認值為 5
--step每執行一個任務后暫停,等待用戶確認
--skip-tags跳過指定標簽的任務(執行除指定標簽外的任務)
-C檢查 Playbook 對受控端是否有修改,進行模擬執行(dry run)

1.5 Playbook 的語法 —— 權限相關

在 Ansible Playbook 中,可以通過以下選項控制任務執行的用戶身份和權限:

1. remote_user

  • 作用:指定 Playbook 運行時連接遠程主機的用戶。
  • 位置:可以寫在 play 下的 hosts 同級,也可以在單個 task 中單獨指定。
  • 示例
# 指定使用 root 用戶執行整個 play
remote_user: root

2. become

  • 作用:布爾值選項,控制是否以特權用戶身份執行任務。
  • 取值yes 表示啟用特權用戶(通常是 root)執行。
  • 常用組合:通常與 become_method 一起使用。
  • 示例
# 使用特權用戶執行任務
become: yes

3. become_method

  • 作用:指定切換到特權用戶時使用的方法。
  • 常用值sudosu(默認為 sudo)。
  • 示例
become_method: sudo

1.6 Playbook 的通知與觸發機制

在 Ansible 中,notifyhandlers 用于實現任務完成后的事件觸發,常用于修改配置文件后重啟服務等操作。

1. notify

  • 作用:寫在某個任務中,當該任務執行時,會觸發對應的 handler
  • 注意notify 指定的名稱必須與 handler 中的 name 完全一致,否則無法觸發。
  • 位置:可以寫在任務中任意位置,屬于該任務的一部分。

2. handlers

  • 作用:定義被觸發后要執行的動作。
  • 規則
    1. handlerstasks 同級,一般寫在 Playbook 的末尾。
    2. handlers- name 必須與 notify 指定的名稱一致。
    3. 即便一個 handler 被多次觸發,它也只會執行一次。
    4. handlers 本質上是一種特殊的任務(tasks)。

3. 使用示例

- name: web                        # Play 名稱,用于描述本 play 的作用hosts: web_group                  # 指定目標主機組,需要在 /etc/ansible/hosts 中定義remote_user: root                 # 使用 root 用戶連接遠程主機become: yes                       # 提升權限執行任務(使用 root 權限)tasks:- name: install nginx           # 任務名稱:安裝 nginxyum:                          # 使用 yum 模塊管理軟件包name: nginx                 # 要安裝的軟件包名稱state: present              # 確保 nginx 已安裝,如果未安裝則自動安裝- name: copy nginx conf         # 任務名稱:拷貝 nginx 配置文件copy:                         # 使用 copy 模塊src: nginx.conf             # 源文件路徑(控制節點本地文件)dest: /etc/nginx/nginx.conf # 目標路徑(遠程主機)tags: copyfile                 # 給任務打標簽,可使用 -t copyfile 單獨執行notify: restart                # 當任務執行成功后觸發 handler,名稱必須與 handlers 中的 name 一致handlers:                         # 定義被觸發的特殊任務(handler)- name: restart                 # handler 名稱,與 notify 對應systemd:                      # 使用 systemd 模塊管理服務name: nginx                 # 服務名稱state: restarted            # 重啟服務(如果服務未運行會啟動)

4. 使用場景

  • 修改配置文件后重啟服務
  • 容器或服務需要重新加載配置
  • 任意需要在任務完成后執行特定動作的場景

1.6 Playbook 中的變量

在 Ansible Playbook 中,變量用于管理動態值,可以極大地提高可維護性和可讀性。
例如,如果需要更新多個文件中的配置路徑,每次都寫完整路徑既麻煩又容易出錯,這時使用變量可以只寫簡短標識,使 Playbook 更清晰,維護成本也降低。

1.7 變量的定義方式

  1. 命令行定義變量
    使用 -e--extra-vars 傳入變量。
  2. Playbook 中定義變量
    在 Play 文件中通過 varsvars_files 定義變量。
  3. Inventory 主機信息文件定義變量
    在主機或組變量中定義。

變量優先級:命令行 > Playbook 文件 > Inventory 文件

1.8 通過 vars 定義變量示例

以下示例展示如何使用變量安裝 LAMP 服務(Linux, Apache, MariaDB, PHP):

---
## Playbook: 安裝 LAMP 服務并配置自啟和自動重啟
- name: 安裝 LAMP 服務hosts: web_group                 # 目標主機組,inventory 文件中定義的 web_groupremote_user: root                 # 遠程執行任務的用戶vars:                             # 定義變量package_name:                   # 變量 package_name,用于存放需要安裝的軟件包列表- httpd                       # Apache 服務- mariadb-server               # MariaDB 數據庫服務- php                          # PHP 語言- php-mysqlnd                  # PHP MySQL 擴展(EulerOS 推薦)tasks:                            # 任務列表- name: 安裝 LAMP 軟件包yum:name: "{{ package_name }}"   # 引用變量安裝所有軟件包state: present[root@ansible-manager playbook]# ansible-playbook lamp_plook.yml --syntax-check
playbook: lamp_plook.yml
[root@ansible-manager playbook]# ansible-playbook lamp_plook.yml

1. 為什么使用 vars_files

  • 當變量較少時,可直接在 Playbook 使用 vars 定義。
  • 當變量較多時,集中存放在獨立文件中更清晰、易維護。
  • Playbook 只需引用變量文件即可,便于多人協作和版本控制。

1.9 變量定義階段

文件:my_vars.yml

# my_vars.yml
# 定義所有需要的變量,便于集中管理
httpd_package: httpd                     # Apache 軟件包
mariadb_package: mariadb-server          # MariaDB 軟件包
php_package: php                         # PHP 軟件包
php_mysql_package: php-mysqlnd           # PHP MySQL 擴展(EulerOS 推薦)
my_cnf: /etc/my.cnf                       # 目標 MariaDB 配置文件路徑
bendi_my: /etc/ansible/yml/mysql/conf/my.cnf  # 本地配置文件路徑

說明:

  • 集中管理軟件包名、配置路徑
  • 方便在多個 Playbook 中復用

1.10 主 Playbook 調用階段

文件:apache.yml

- name: install lamp  # playbook 名稱,描述這是一個安裝 LAMP(Linux, Apache, MySQL/MariaDB, PHP)環境的任務hosts: web_group    # 指定目標主機組,這里是 web_groupremote_user: root   # 使用 root 用戶執行任務vars_files:- my_vars.yml     # 引入變量文件 my_vars.yml,里面定義了 httpd_package、mariadb_package 等變量tasks:- name: install apacheyum: name: "{{ httpd_package }}"  # 使用變量安裝 Apache 包state: present              # 確保包被安裝(present)- name: install mariadbyum:name: "{{ mariadb_package }}"  # 安裝 MariaDB 包state: present- name: install phpyum:name: "{{ php_package }}"      # 安裝 PHP 包state: present- name: install php_mysqlyum: name: "{{ php_mysql_package }}"  # 安裝 PHP 的 MySQL 擴展包state: present- name: updatecopy:src: "{{ bendi_my }}"           # 本地文件路徑(源文件)dest: "{{ my_cnf }}"            # 遠程目標路徑(復制到服務器)mode: "0644"                    # 設置文件權限為 0644notify: - restart mariadb               # 當該任務執行時觸發 handler 重啟 MariaDB- name: start and enable mariadbservice:name: "{{ mariadb_package }}"   # 操作 MariaDB 服務state: started                  # 啟動服務enabled: yes                    # 設置開機自啟handlers:  # handlers 與 tasks 同級,通常用于被 notify 調用- name: restart mariadbservice:name: "{{ mariadb_package }}"   # 重啟 MariaDB 服務state: restarted

輸出

[root@ansible-manager playbook]# ansible-playbook lamp_vars.yml --syntax-checkplaybook: lamp_vars.yml
[root@ansible-manager playbook]# ansible-playbook lamp_vars.yml PLAY [install lamp] *******************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]TASK [install apache] *****************************************************************************************************************************************************
ok: [10.0.0.13]
ok: [10.0.0.11]
ok: [10.0.0.12]TASK [install mariadb] ****************************************************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.13]
ok: [10.0.0.12]TASK [install php] ********************************************************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.13]
ok: [10.0.0.12]TASK [install php_mysql] **************************************************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.13]
ok: [10.0.0.12]TASK [update mysql conf] **************************************************************************************************************************************************
ok: [10.0.0.13]
ok: [10.0.0.11]
ok: [10.0.0.12]TASK [start and enable mariadb] *******************************************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.13]
ok: [10.0.0.12]PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.12                  : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.13                  : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

1.11 Ansible的debug模塊

  1. 作用

debug 模塊用于在 playbook 執行過程中輸出變量值或顯示自定義消息,有助于調試和驗證變量是否符合預期。

  1. 常用參數
參數作用
msg輸出自定義消息,可以是字符串或包含變量的表達式
var輸出變量的值,直接指定變量名即可

注意:

  • msg 中引用變量時需要使用 {{ }}
  • var 直接寫變量名,無需 {{ }}
  1. 示例 Playbook
- name: 示例 playbookhosts: csremote_user: roottasks:- name: 輸出簡單消息debug:msg: "這是一個簡單的消息"- name: 輸出變量的值debug:var: ansible_distribution- name: 輸出多個變量的值debug:msg: "操作系統: {{ ansible_distribution }},版本: {{ ansible_distribution_version }}"- name: 查看root目錄下的文件shell: ls /rootregister: root_directory_contents- name: 輸出的結果debug:var: root_directory_contents.stdout_lines

變量使用的注意事項:

  • 在 msg 中使用變量時,需要使用雙大括號{{ }}來引用變量。
  • 在 var中直接指定變量名即可,不需要使用雙大括號。

輸出

[root@ansible-manager playbook]# ansible-playbook debug_test.yml --syntax-checkplaybook: debug_test.yml
[root@ansible-manager playbook]# ansible-playbook debug_test.ymlPLAY [案例 playbook] ********************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]TASK [輸出簡單消息] *************************************************************************************************************************************************************
ok: [10.0.0.11] => {"msg": "這是一個消息"
}
ok: [10.0.0.12] => {"msg": "這是一個消息"
}
ok: [10.0.0.13] => {"msg": "這是一個消息"
}TASK [輸出變量的值] *************************************************************************************************************************************************************
ok: [10.0.0.11] => {"ansible_distribution": "openEuler"
}
ok: [10.0.0.12] => {"ansible_distribution": "openEuler"
}
ok: [10.0.0.13] => {"ansible_distribution": "openEuler"
}TASK [輸出多個變量的值] ***********************************************************************************************************************************************************
ok: [10.0.0.11] => {"msg": "操作系統: openEuler, 版本:22.03"
}
ok: [10.0.0.12] => {"msg": "操作系統: openEuler, 版本:22.03"
}
ok: [10.0.0.13] => {"msg": "操作系統: openEuler, 版本:22.03"
}TASK [查看root目錄下的文件] *******************************************************************************************************************************************************
changed: [10.0.0.13]
changed: [10.0.0.11]
changed: [10.0.0.12]TASK [輸出的結果] **************************************************************************************************************************************************************
ok: [10.0.0.11] => {"root_directory_contents.stdout_lines": ["anaconda-ks.cfg","test"]
}
ok: [10.0.0.12] => {"root_directory_contents.stdout_lines": ["anaconda-ks.cfg","test"]
}
ok: [10.0.0.13] => {"root_directory_contents.stdout_lines": ["anaconda-ks.cfg","test"]
}PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.12                  : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.13                  : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

1.12 Ansible 事實變量(Facts)

Ansible 通過事實變量(facts)收集目標主機的系統信息,可用于條件執行、動態配置等。

1. 操作系統相關

變量說明
ansible_distribution操作系統名稱,例如 CentOS、Ubuntu、Red Hat
ansible_distribution_version完整版本號,例如 7.9.2009、20.04
ansible_distribution_major_version主版本號,例如 7、20
ansible_os_family操作系統家族,例如 RedHat、Debian
ansible_distribution_release系統代號,例如 focal、buster

2. 網絡相關

變量說明
ansible_default_ipv4默認 IPv4 地址信息
ansible_default_ipv4.address默認 IPv4 地址
ansible_default_ipv4.netmask子網掩碼
ansible_default_ipv4.gateway默認網關
ansible_default_ipv6默認 IPv6 地址信息
ansible_default_ipv6.address默認 IPv6 地址
ansible_default_ipv6.netmask子網掩碼
ansible_default_ipv6.gateway默認網關
ansible_all_ipv4_addresses所有 IPv4 地址列表
ansible_all_ipv6_addresses所有 IPv6 地址列表
ansible_hostname主機名
ansible_fqdn完全限定域名(FQDN)

3. 硬件相關

變量說明
ansible_processorCPU 信息
ansible_processor_cores每個 CPU 的核心數
ansible_processor_countCPU 數量
ansible_processor_threads_per_core每核心線程數
ansible_processor_vcpus虛擬 CPU 數量
ansible_memory_mb內存信息(MB)
ansible_memory_mb.real物理內存
ansible_memory_mb.swap交換內存
ansible_devices存儲設備信息
ansible_mounts掛載點信息

4. 文件系統相關

變量說明
ansible_filesystems文件系統類型列表
ansible_mounts[].mount掛載點路徑
ansible_mounts[].device設備名稱
ansible_mounts[].fstype文件系統類型
ansible_mounts[].size_total總大小(字節)
ansible_mounts[].size_available可用大小(字節)

5. 用戶和組相關

變量說明
ansible_user_id當前用戶 ID
ansible_group_names當前用戶所屬組列表
ansible_user當前用戶名

6. 服務相關

變量說明
ansible_service_mgr服務管理器類型,例如 systemd、init

7. 其他變量

變量說明
ansible_date_time日期和時間信息
ansible_date_time.date當前日期
ansible_date_time.time當前時間
ansible_date_time.epochUnix 時間戳
ansible_date_time.timezone時區
ansible_env環境變量
ansible_env.PATHPATH 環境變量
ansible_env.HOMEHOME 環境變量

案例

  1. 示例 1:查看所有事實變量

使用 ansible_facts 可以查看目標主機收集的所有事實變量:

- name: 查看所有事實變量hosts: cstasks:- name: 輸出所有事實變量debug:var: ansible_facts

執行該 playbook 后,將看到目標主機的所有事實變量及其值。

  1. 示例 2:使用特定事實變量

可以在任務中根據操作系統或其他事實變量做條件判斷:

- name: 根據操作系統版本安裝軟件hosts: cstasks:- name: 安裝 CentOS 7 的特定軟件yum:name: my-softwarestate: presentwhen: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"- name: 安裝 Ubuntu 20.04 的特定軟件apt:name: my-softwarestate: presentwhen: ansible_distribution == "Ubuntu" and ansible_distribution_major_version == "20"- name: 輸出默認 IPv4 地址debug:msg: "默認 IPv4 地址是 {{ ansible_default_ipv4.address }}"

輸出:

[root@ansible-manager playbook]# ansible-playbook facts_vars.yml --syntax-checkplaybook: facts_vars.yml
[root@ansible-manager playbook]# ansible-playbook facts_vars.yml PLAY [根據操作系統本版安裝軟件] *******************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]TASK [安裝centos7的特定軟件] *****************************************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]TASK [安裝ubuntu 20.04的特定軟件] ************************************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]TASK [輸出默認的ipv4地址] ********************************************************************************************************************************************************
ok: [10.0.0.11] => {"msg": "默認的ipv4地址時 10.0.0.11"
}
ok: [10.0.0.12] => {"msg": "默認的ipv4地址時 10.0.0.12"
}
ok: [10.0.0.13] => {"msg": "默認的ipv4地址時 10.0.0.13"
}PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
10.0.0.12                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
10.0.0.13                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0 

說明:

  • when 條件判斷可以使用事實變量來實現任務的動態執行。
  • 可以使用 debug 輸出特定變量值,幫助調試或獲取信息。

二、Playbook 中的流程控制

Ansible 條件判斷(when)

在 Ansible 中,條件判斷(when)是 Playbook 流程控制的核心語法,用于控制某個任務是否執行。
應用場景包括:

  • 根據不同的操作系統安裝不同的軟件。
  • 只在特定版本系統上執行某些操作。
  • 避免重復執行(如源碼安裝軟件時)。
  • 根據變量值決定執行不同任務。

基礎

1. 基本語法

- name: cs條件判斷              # play 名稱hosts: cs                    # 目標主機組為 cstasks:                       # 定義任務列表- name: Install CentOS Httpd   # 在 CentOS/RedHat 系統上安裝 httpdyum:                          # 使用 yum 模塊安裝軟件包name: httpd                 # 要安裝的軟件包是 httpdstate: present              # 保證軟件包已安裝when: ansible_facts['os_family'] == "RedHat"  # 條件判斷:當 os_family 屬于 "RedHat" 時才執行該任務- name: Install Ubuntu Httpd    # 在 Debian/Ubuntu 系統上安裝 apache2apt:                          # 使用 apt 模塊安裝軟件包name: apache2               # 要安裝的軟件包是 apache2state: present              # 保證軟件包已安裝when: ansible_facts['os_family'] == "Debian"# 條件判斷:當 os_family 屬于 "Debian" 時才執行該任務

輸出

[root@ansible-manager playbook]# ansible-playbook when_playbook.yml --syntax-checkplaybook: when_playbook.yml
[root@ansible-manager playbook]# ansible-playbook when_playbook.ymlPLAY [條件判斷] ***************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]TASK [install centos httpd] ***********************************************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.13]
ok: [10.0.0.12]TASK [apt] ****************************************************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.12                  : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.13                  : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

根據系統類型自動選擇安裝 httpdapache2

2. 條件分組(使用括號)

- name: 條件判斷                  # play 名稱hosts: web_group               # 目標主機組為 web_grouptasks:                         # 定義任務列表- name: install centos httpd   # 在 RedHat 系列系統上安裝 httpdyum:                          # 使用 yum 模塊name: httpd                 # 要安裝的軟件包是 httpdstate: present              # 確保安裝when: ansible_facts['os_family'] == 'RedHat'# 條件判斷:當 os_family 屬于 "RedHat" 時才執行- name: install ubuntu httpd    # 在 Debian 系列系統上安裝 apache2apt:                          # 使用 apt 模塊name: apache2               # 要安裝的軟件包是 apache2state: present              # 確保安裝when: ansible_facts['os_family'] == 'Debian'# 條件判斷:當 os_family 屬于 "Debian" 時才執行- name: "shut down centos6 and debian 7 systems"  command: /sbin/shutdown -t nowwhen: (ansible_facts['distribution'] == 'CentOS' and ansible_facts['distribution_major_version'] == "6") or(ansible_facts['distribution'] == 'Debian' and ansible_facts['distribution_major_version'] == "7")# 條件判斷:# - 如果系統是 CentOS 且主版本號是 6 → 執行關機命令# - 如果系統是 Debian 且主版本號是 7 → 執行關機命令

輸出

[root@ansible-manager playbook]# ansible-playbook when2_playbook.yml --syntax-checkplaybook: when2_playbook.yml
[root@ansible-manager playbook]# ansible-playbook when2_playbook.ymlPLAY [條件判斷] ***************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]TASK [install centos httpd] ***********************************************************************************************************************************************
ok: [10.0.0.13]
ok: [10.0.0.12]
ok: [10.0.0.11]TASK [apt] ****************************************************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]TASK [shut down centos6 and debian 7 systems] *****************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
10.0.0.12                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
10.0.0.13                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0 

使用 括號 將多個條件分組,避免邏輯混亂。

3. 多條件寫法(列表形式)

- name: 條件判斷                  # play 名稱hosts: web_group               # 目標主機組為 web_grouptasks:                         # 定義任務列表- name: install centos httpd   # 在 RedHat 系列系統上安裝 httpdyum:                          # 使用 yum 模塊name: httpd                 # 要安裝的軟件包是 httpdstate: present              # 確保 httpd 已安裝when: ansible_facts['os_family'] == 'RedHat'# 條件判斷:當 os_family 屬于 "RedHat" 時執行該任務- name: install ubuntu httpd    # 在 Debian 系列系統上安裝 apache2apt:                          # 使用 apt 模塊name: apache2               # 要安裝的軟件包是 apache2state: present              # 確保 apache2 已安裝when: ansible_facts['os_family'] == 'Debian'# 條件判斷:當 os_family 屬于 "Debian" 時執行該任務- name: "shut down centos6 systems"   # 針對 CentOS 6 系統執行關機操作command: /sbin/shutdown -t now      # 執行關機命令when:                               # 多條件寫法(列表形式)- ansible_facts['distribution'] == "CentOS"            # 系統是 CentOS- ansible_facts['distribution_major_version'] == "6"   # 且主版本號_

輸出

[root@ansible-manager playbook]# ansible-playbook when3_playbook.yml --syntax-checkplaybook: when3_playbook.yml
[root@ansible-manager playbook]# ansible-playbook when3_playbook.yml PLAY [條件判斷] ***************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]TASK [install centos httpd] ***********************************************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.13]
ok: [10.0.0.12]TASK [apt] ****************************************************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]TASK [shut down centos6 systems] ******************************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
10.0.0.12                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
10.0.0.13                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0 

相當于 and 邏輯,只有全部條件滿足時任務才會執行。

4. 條件運算

- name: 條件判斷                  # play 名稱hosts: web_group               # 目標主機組tasks:- name: run only on RedHat 6+  # 任務名稱shell: echo "only on red hat 6, derivatives, and later"when: ansible_facts['os_family'] == "RedHat" and(ansible_facts['distribution_major_version'] | int) >= 6# 條件判斷:# 1. os_family 必須是 "RedHat"(如 RHEL、CentOS、Rocky 等)# 2. 系統主版本號 (distribution_major_version) 必須 >= 6# → 滿足條件時才執行

輸出

[root@ansible-manager playbook]# ansible-playbook when4_playbook.yml --syntax-checkplaybook: when4_playbook.yml
[root@ansible-manager playbook]# ansible-playbook when4_playbook.yml PLAY [條件判斷] ***************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]TASK [shell] **************************************************************************************************************************************************************
changed: [10.0.0.13]
changed: [10.0.0.11]
changed: [10.0.0.12]PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.12                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.13                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

使用 Jinja2 過濾器(如 **|int** 做數值比較。

5. 變量判斷實例

- name: 條件判斷                  # play 名稱hosts: web_group               # 目標主機組 web_groupvars:                          # 定義變量區域a: "3"                       # 定義變量 a,值為 "3"tasks:                         # 任務列表- name: createfile            # 第一個任務copy:                       # 使用 copy 模塊content: "test3"          # 文件內容寫入 "test3"dest: /home/an/a.txt      # 文件路徑when: a == "3"              # 條件判斷:當 a 的值等于 "3" 時才執行該任務- name: createfile            # 第二個任務copy:                       # 使用 copy 模塊content: "test4"          # 文件內容寫入 "test4"dest: /home/an/a.txt      # 文件路徑when: a == "4"              # 條件判斷:當 a 的值等于 "4" 時才執行該任務

輸出

[root@ansible-manager playbook]# ansible-playbook when5_playbook.yml --syntax-checkplaybook: when5_playbook.yml
[root@ansible-manager playbook]# ansible-playbook when5_playbook.yml PLAY [條件判斷] ***************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]TASK [createfile] *********************************************************************************************************************************************************
changed: [10.0.0.11]
changed: [10.0.0.13]
changed: [10.0.0.12]TASK [createfile] *********************************************************************************************************************************************************
skipping: [10.0.0.11]
skipping: [10.0.0.12]
skipping: [10.0.0.13]PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.12                  : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
10.0.0.13                  : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

如果變量 a="3",則寫入 test3;如果 a="4",則寫入 test4

2.2 循環(with_items)

在 Ansible 中,循環是非常常見的操作場景,用于 批量執行相同類型的任務,比如批量安裝軟件、創建用戶、復制文件等。
早期常用 with_items,新版 Ansible 推薦使用 loop,但 with_items 依然很常見。

1. 批量創建用戶

- name: 批量創建用戶                  # play 名稱hosts: web_group                   # 目標主機組remote_user: root                  # 使用 root 用戶執行任務tasks:                             # 任務列表- name: 配置創建用戶操作         # 任務名稱user:                          # 使用 user 模塊name: "{{ item }}"           # 循環變量 item,對應用戶名state: present               # 確保用戶存在(如果不存在則創建)with_items:                     # 循環列表- an1                        # 用戶名 1- an2                        # 用戶名 2- an3                        # 用戶名 3

輸出

[root@ansible-manager playbook]# ansible-playbook for2_playbook.yml --syntax-checkplaybook: for2_playbook.yml
[root@ansible-manager playbook]# ansible-playbook for2_playbook.yml PLAY [批量創建用戶] *************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]TASK [配置創建用戶操作] ***********************************************************************************************************************************************************
changed: [10.0.0.13] => (item=an1)
changed: [10.0.0.12] => (item=an1)
changed: [10.0.0.11] => (item=an1)
changed: [10.0.0.13] => (item=an2)
changed: [10.0.0.12] => (item=an2)
changed: [10.0.0.11] => (item=an2)
changed: [10.0.0.13] => (item=an3)
changed: [10.0.0.11] => (item=an3)
changed: [10.0.0.12] => (item=an3)PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.12                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.13                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

執行邏輯:依次創建用戶 cs1cs2cs3

3. 批量創建用戶和用戶組(循環嵌套)

- name: 批量創建用戶和用戶組           # play 名稱hosts: all                         # 目標主機組remote_user: root                  # 使用 root 用戶執行任務tasks:                             # 任務列表- name: create group             # 任務名稱:創建用戶組group:                         # 使用 group 模塊name: "{{ item }}"           # 循環變量 item,對應用戶組名稱state: present               # 確保用戶組存在with_items:                     # 循環列表- group1- group2- group3- name: create user              # 任務名稱:創建用戶user:                          # 使用 user 模塊name: "{{ item.name }}"      # 用戶名group: "{{ item.group }}"    # 用戶所屬組state: present               # 確保用戶存在with_items:                     # 循環列表(字典)- {name: a1, group: group1}  # 用戶 a1 加入 group1- {name: a2, group: group2}  # 用戶 a2 加入 group2- {name: a3, group: group3}  # 用戶 a3 加入 group3

輸出

[root@ansible-manager playbook]# ansible-playbook for3_users.yml --syntax-checkplaybook: for3_users.yml
[root@ansible-manager playbook]# ansible-playbook for3_users.ymlPLAY [批量創建用戶和用戶組] *********************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]TASK [create group] *******************************************************************************************************************************************************
changed: [10.0.0.11] => (item=group1)
changed: [10.0.0.13] => (item=group1)
changed: [10.0.0.12] => (item=group1)
changed: [10.0.0.13] => (item=group2)
changed: [10.0.0.11] => (item=group2)
changed: [10.0.0.12] => (item=group2)
changed: [10.0.0.13] => (item=group3)
changed: [10.0.0.11] => (item=group3)
changed: [10.0.0.12] => (item=group3)TASK [create user] ********************************************************************************************************************************************************
changed: [10.0.0.13] => (item={'name': 'a1', 'group': 'group1'})
changed: [10.0.0.11] => (item={'name': 'a1', 'group': 'group1'})
changed: [10.0.0.12] => (item={'name': 'a1', 'group': 'group1'})
changed: [10.0.0.13] => (item={'name': 'a2', 'group': 'group2'})
changed: [10.0.0.11] => (item={'name': 'a2', 'group': 'group2'})
changed: [10.0.0.12] => (item={'name': 'a2', 'group': 'group2'})
changed: [10.0.0.13] => (item={'name': 'a3', 'group': 'group3'})
changed: [10.0.0.12] => (item={'name': 'a3', 'group': 'group3'})
changed: [10.0.0.11] => (item={'name': 'a3', 'group': 'group3'})PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.12                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.13                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  [root@ansible-node02 ~]# cat /etc/passwd
an1:x:1000:1001::/home/an1:/bin/bash
an2:x:1001:1002::/home/an2:/bin/bash
an3:x:1002:1003::/home/an3:/bin/bash
a1:x:1003:1004::/home/a1:/bin/bash
a2:x:1004:1005::/home/a2:/bin/bash
a3:x:1005:1006::/home/a3:/bin/bash

執行邏輯:

  1. 先創建 3 個用戶組:group1/group2/group3
  2. 再創建用戶,并將用戶加入對應用戶組。

4. 批量刪除用戶和用戶組(循環嵌套)

- name: 批量刪除用戶和用戶組         # play 名稱hosts: all                         # 目標主機組remote_user: root                  # 使用 root 用戶執行任務tasks:                             # 任務列表- name: drop user                # 任務名稱:刪除用戶user:                          # 使用 user 模塊name: "{{ item.name }}"      # 用戶名group: "{{ item.group }}"    # 用戶所屬組remove: yes                  # 同時刪除用戶家目錄和郵件池等文件state: absent                # 確保用戶不存在(刪除用戶)with_items:                     # 循環列表(字典)- {name: a1, group: group1}  # 刪除用戶 a1,所屬 group1- {name: a2, group: group2}  # 刪除用戶 a2,所屬 group2- {name: a3, group: group3}  # 刪除用戶 a3,所屬 group3- name: drop group               # 任務名稱:刪除用戶組group:                         # 使用 group 模塊name: "{{ item }}"           # 用戶組名state: absent                # 確保組不存在(刪除組)with_items:                     # 循環列表- group1                     # 刪除 group1- group2                     # 刪除 group2- group3                     # 刪除 group3

輸出

[root@ansible-manager playbook]# ansible-playbook for4_users.yml --syntax-checkplaybook: for4_users.yml
[root@ansible-manager playbook]# ansible-playbook for4_users.ymlPLAY [批量刪除用戶和用戶組] *********************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Platform linux on host 10.0.0.11 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.11]
[WARNING]: Platform linux on host 10.0.0.13 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.13]
[WARNING]: Platform linux on host 10.0.0.12 is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [10.0.0.12]TASK [drop user] **********************************************************************************************************************************************************
changed: [10.0.0.11] => (item={'name': 'a1', 'group': 'group1'})
changed: [10.0.0.12] => (item={'name': 'a1', 'group': 'group1'})
changed: [10.0.0.13] => (item={'name': 'a1', 'group': 'group1'})
changed: [10.0.0.11] => (item={'name': 'a2', 'group': 'group2'})
changed: [10.0.0.13] => (item={'name': 'a2', 'group': 'group2'})
changed: [10.0.0.12] => (item={'name': 'a2', 'group': 'group2'})
changed: [10.0.0.11] => (item={'name': 'a3', 'group': 'group3'})
changed: [10.0.0.13] => (item={'name': 'a3', 'group': 'group3'})
changed: [10.0.0.12] => (item={'name': 'a3', 'group': 'group3'})TASK [drop group] *********************************************************************************************************************************************************
changed: [10.0.0.13] => (item=group1)
changed: [10.0.0.11] => (item=group1)
changed: [10.0.0.12] => (item=group1)
changed: [10.0.0.13] => (item=group2)
changed: [10.0.0.12] => (item=group2)
changed: [10.0.0.11] => (item=group2)
changed: [10.0.0.13] => (item=group3)
changed: [10.0.0.12] => (item=group3)
changed: [10.0.0.11] => (item=group3)PLAY RECAP ****************************************************************************************************************************************************************
10.0.0.11                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.12                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.0.13                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

執行邏輯:

  1. 刪除用戶(并移除其家目錄)。
  2. 再刪除對應用戶組。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/922318.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/922318.shtml
英文地址,請注明出處:http://en.pswp.cn/news/922318.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

猿輔導Java后臺開發面試題及參考答案

int 與 Integer 的區別是什么?若創建數量龐大的數字時使用 Integer,會對重復數字創建新對象嗎?int 是 Java 中的基本數據類型,直接存儲數值,占用 4 個字節,默認值為 0,不需要通過 new 關鍵字創建…

代碼隨想錄學習摘抄day9(回溯1-11)

一個樸實無華的目錄定義:回溯法也可以叫做回溯搜索法,它是一種搜索的方式。應用場景:回溯法解決的問題都可以抽象為樹形結構代碼模板題型第77題. 組合思路:每次從集合中選取元素,可選擇的范圍隨著選擇的進行而收縮&…

Altium Designer(AD24)打開工程文件的幾種方法

??《專欄目錄》 目錄 1,概述 2,源文件 2,菜單欄 4,工具欄 5,注意事項 1,概述 本文介紹幾種打開工程文件的方法。 2,源文件 找到工程的源文件存儲路徑,找到.PrjPcb的源工程文件,雙擊打開。 2,菜單欄 第1步:執行File→Open, 第2步:找到工程文件的存儲路徑,并選中…

Linux嵌入式自學筆記(基于野火EBF6ULL):2.點燈與ubuntu安裝

一、點燈登錄root:賬號:root ; 密碼:root點燈命令:echo 0 > /sys/class/leds/red/brightness #關閉red燈 echo 0 > /sys/class/leds/blue/brightness #關閉blue燈 echo 0 > /sys/class/leds/green/brightness …

【Java實戰?】Java實戰:MyBatis-Plus 開啟MySQL數據庫高效操作之旅

目錄 一、MyBatis-Plus 環境集成 1.1 項目依賴引入 1.2 數據庫配置 1.3 代碼生成器使用 二、核心 CRUD 操作實現 2.1 基礎查詢 2.2 數據新增與修改 2.3 復雜查詢場景 三、性能優化與高級特性 3.1 緩存配置 3.2 樂觀鎖實現 3.3 字段自動填充 四、實戰案例:用戶管理模塊開發 4.1…

開學季干貨——知識梳理與經驗分享

技術文章大綱:開學季干貨——知識梳理與經驗分享目標受眾分析明確文章面向的學生群體(如大學生、高中生) 分析不同群體的核心需求(課程準備、時間管理、工具使用) 結合技術場景(如數字筆記、在線協作&#…

Linux《線程(上)》

通過之前的學習我們已經了解了操作系統當中的基本的概念包括進程、基礎IO、磁盤文件存儲等,但是到目前為止我們還未了解到線程相關的概念,這就使得當前我們對操作系統的認知還不是完整的,現在我們是還是無法理解一個進程當中是如何同時的執行…

為什么知識復用時缺乏場景化指導影響實用性

知識復用時因缺乏場景化指導而嚴重影響實用性,其根本原因在于知識的價值本質上根植于其應用情境。脫離了場景的“純知識”往往是抽象、片面且難以行動的。這導致了認知鴻溝的產生、隱性知識的流失、決策風險的增加、以及學習遷移效率的低下。當使用者面對一份缺乏“…

擁抱直覺與創造力:走進VibeCoding的新世界

引言 在傳統觀念里,編程是一項高度理性、邏輯嚴密的活動,開發者需要像建筑師一樣,用代碼一行行地精確構建數字世界。然而,隨著人工智能技術的飛速發展,一種全新的編程理念和體驗正在興起——它就是 VibeCoding&#xf…

HTTP的Web服務測試在Python中的實現

在Web開發領域,對HTTP Web服務進行測試是確保服務穩定性和可靠性的關鍵步驟。Python作為一種功能強大的編程語言,提供了多種工具和庫來簡化這一過程。本文將介紹如何在Python中實現HTTP的Web服務測試。首先,Python的requests庫是測試HTTP Web…

Android Studio 構建項目時 Gradle 下載失敗的解決方案

一、問題原因分析根據錯誤日志:下載地址 https://services.gradle.org/distributions/gradle-8.1-bin.zip 連接超時(10秒)。可能原因:網絡環境限制(如公司防火墻、地區網絡屏蔽)。代理配置未生效或配置錯誤…

mysql 與 MongoDB 的分片

MySQL 和 MongoDB 作為不同類型數據庫的代表(關系型 vs 文檔型),其分片機制在設計理念、實現方式和適用場景上存在顯著差異。兩者的分片核心目標一致——通過水平擴展(Scale Out)解決單節點存儲容量和性能瓶頸,但因數據模型、事務支持和分布式設計理念的不同,形成了截然…

Coze源碼分析-資源庫-創建知識庫-前端源碼-核心邏輯與接口

創建知識庫邏輯 1. 表單驗證系統 文件位置:frontend/packages/data/knowledge/knowledge-modal-base/src/create-knowledge-modal-v2/features/add-type-content/coze-knowledge/index.tsx 知識庫創建表單的驗證規則: // 知識庫名稱驗證規則 const nameV…

歐拉函數 | 定義 / 性質 / 應用

注:本文為 “歐拉函數” 相關合輯。 略作重排,未整理去重。 如有內容異常,請看原文。 歐拉函數最全總結 jiet07 已于 2024-10-22 10:00:54 修改 一、歐拉函數的引入 首先引入互質關系: 如果兩個正整數,除了 111 以…

ubuntu git push每次都要輸入密碼怎么解決只輸入一次密碼

在 Ubuntu 下使用 Git 時,如果每次 push 都需要重復輸入密碼,可以通過配置 Git 憑證存儲來解決。以下是幾種常用方法: 🔑 方法一:使用 Git 憑證緩存(推薦) 設置憑證緩存(默認 15 分鐘…

【機械故障】使用fir濾波器實現數據擬合

使用fir濾波器實現數據擬合 提示:學習筆記 使用fir濾波器實現數據擬合使用fir濾波器實現數據擬合一、問題建模二、 構建矩陣方程(關鍵步驟)三、最小二乘解四、重要注意事項4.1 濾波器長度 M4.2 數據的預處理4.3 延遲問題4.4 性能評估一、問題…

STC8H系列-高級PWM-兩相步進電機-細分驅動

兩相步進電機, STC8H系列 用高級PWM實現SPWM細分驅動 /************* 功能說明 ************** 用B組高級PWM細分驅動2相4線小型步進電機, 支持1、2、4、8、16、32、64細分, 比如1.8度的電機4細分到0.45度. 本程序用于演示SPWM多細分直接驅動2相4線小型步進電機…

內網環境下ubuntu 20.04搭建深度學習環境總結

2025年9月更新,隨著人工智能的發展,現在深度學習環境配置越來越簡單了,常用的pytorch、paddle(3.x)等深度學習庫安裝的時候自帶了cuda和cudnn的python包,不需要在操作系統層面自己安裝,配置環境…

深入 Linux 文件系統:從數據存儲到萬物皆文件

深入 Linux 文件系統:從數據存儲到萬物皆文件 Linux 文件系統是一個精妙而復雜的工程,它像一座圖書館,不僅存放著書籍(數據),還有一套高效的卡片索引系統(元數據)來管理它們。本文將…

C++, ffmpeg, libavcodec-RTSP拉流,opencv實時預覽

文章目錄RTSPStreamPlayer.cppRTSPStreamPlayer.hmain.cpp編譯運行在ffmpeg_rtsp原有的rtsp拉流項目基礎上加入了udp連接rtsp&#xff0c;日志模塊&#xff0c;opencv實施預覽等功能。RTSPStreamPlayer.cpp #include "RTSPStreamPlayer.h" #include <iostream>…