目錄
- 一、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
時表示啟用。
- hosts:指定要操作的主機或主機組;如果是多個組,可以用
- 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
- 作用:指定切換到特權用戶時使用的方法。
- 常用值:
sudo
或su
(默認為 sudo)。 - 示例:
become_method: sudo
1.6 Playbook 的通知與觸發機制
在 Ansible 中,notify
和 handlers
用于實現任務完成后的事件觸發,常用于修改配置文件后重啟服務等操作。
1. notify
- 作用:寫在某個任務中,當該任務執行時,會觸發對應的
handler
。 - 注意:
notify
指定的名稱必須與handler
中的name
完全一致,否則無法觸發。 - 位置:可以寫在任務中任意位置,屬于該任務的一部分。
2. handlers
- 作用:定義被觸發后要執行的動作。
- 規則:
handlers
和tasks
同級,一般寫在 Playbook 的末尾。handlers
的- name
必須與notify
指定的名稱一致。- 即便一個
handler
被多次觸發,它也只會執行一次。 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 變量的定義方式
- 命令行定義變量
使用-e
或--extra-vars
傳入變量。 - Playbook 中定義變量
在 Play 文件中通過vars
或vars_files
定義變量。 - 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模塊
- 作用
debug
模塊用于在 playbook 執行過程中輸出變量值或顯示自定義消息,有助于調試和驗證變量是否符合預期。
- 常用參數
參數 | 作用 |
---|---|
msg | 輸出自定義消息,可以是字符串或包含變量的表達式 |
var | 輸出變量的值,直接指定變量名即可 |
注意:
msg
中引用變量時需要使用{{ }}
var
直接寫變量名,無需{{ }}
- 示例 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_processor | CPU 信息 |
ansible_processor_cores | 每個 CPU 的核心數 |
ansible_processor_count | CPU 數量 |
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.epoch | Unix 時間戳 |
ansible_date_time.timezone | 時區 |
ansible_env | 環境變量 |
ansible_env.PATH | PATH 環境變量 |
ansible_env.HOME | HOME 環境變量 |
案例
- 示例 1:查看所有事實變量
使用 ansible_facts
可以查看目標主機收集的所有事實變量:
- name: 查看所有事實變量hosts: cstasks:- name: 輸出所有事實變量debug:var: ansible_facts
執行該 playbook 后,將看到目標主機的所有事實變量及其值。
- 示例 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
根據系統類型自動選擇安裝
httpd
或apache2
。
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
執行邏輯:依次創建用戶
cs1
、cs2
、cs3
。
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
執行邏輯:
- 先創建 3 個用戶組:
group1/group2/group3
。- 再創建用戶,并將用戶加入對應用戶組。
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
執行邏輯:
- 刪除用戶(并移除其家目錄)。
- 再刪除對應用戶組。