Ad Hoc 是一個拉丁文常用短語,意思是指定或是特殊的任務,在 Ansible 中如果指定單一任務執行,可以使用 ad hoc 命令快速完成指定的任務。
1. Ad-Hoc 命令
Ad hoc 一次僅能執行一個 task ,對於測試及驗證是個非常方便的好工具。
1.1 執行方式
使用 ansible 命令並指定任務的模組即可快速使用
1
|
ansible <host group> -m <module> -a <module args>
|
-host group : 表示要執行哪台機器或是群組( inventory 清單內 )
-m : 指定的 ansbile 模組
-a : 模組的參數 (有些模組不需要加參數)
1.2 ping 範例
執行最簡單的模組 ping 範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[[email protected] example]$ ansible all -m ping
serverb | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
serverc | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
|
Note
需先配置好 inventory 及 ansible.cfg。
2. 如何有效使用 module 在 Ad-Hoc 命令?
Ad-Hoc 快速又很方便,不過對於不熟的模組要下手就很些棘手,不過透過 ansible-doc 可以快速查閱使用方式及模組使用的範例。
2.1 ansible-doc 查詢
ansible-doc 是快速查詢的手冊,加上 l 參數可以查看目前機器上有哪些模組名稱及簡單說明。
1
2
3
4
5
6
7
8
|
[[email protected] ~]$ ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' server object
a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
a10_service_group Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' service groups
a10_virtual_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' virtual servers
aci_aaa_user Manage AAA users (aaa:User)
aci_aaa_user_certificate Manage AAA user certificates (aaa:UserCert)
...
|
2.1 ansible-doc 查看 ping 模組
使用 ansible-doc 加上模組名稱,查看模組詳細使用方式,試者查詢 ping 模組內容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[[email protected] ~]$ ansible-doc ping
> PING (/usr/lib/python3.6/site-packages/ansible/modules/system/ping.py)
A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to
verify the ability to login and that a usable Python is configured. This is NOT ICMP ping, this is just a trivial test module that requires Python on the
remote-node. For Windows targets, use the [win_ping] module instead. For Network targets, use the [net_ping] module instead.
* This module is maintained by The Ansible Core Team
OPTIONS (= is mandatory):
- data
Data to return for the `ping' return value.
If this parameter is set to `crash', the module will cause an exception.
[Default: pong]
type: str
...
|
3. 模組摘要
Ansible 模組非常的多,除了官方本身開發的模組、協力廠商開發及第三方社區開發的模組,以下僅摘要幾些模組。
3.1 檔案模組:
- copy: 複製本地檔案至控制端機器
- file: 設定檔案權限
- lineinfile: 增減文字至檔案內
3.2 管理套件模組:
- yum : 使用 YUM 套件管理工具
- apt : 使用 APT 套件管理工具
- pip : 安裝 Python 套件
3.3 系統模組:
- firewalld : 管理防火牆開啟關閉 port 或 services
- service : 管理 systemctl 服務
- user : 增刪及管理使用者
3.4 網路模組:
- get_url : 從網路資源下載檔案至被控端機器
- nmcli : 管理系統網路
4. LAB 練習
4.1 LAB
- 下載 Lab 檔案,如果已經下載可以忽略此步驟。
1
|
git clone https://github.com/JeffWen0105/ansible-lab.git
|
- 使用 deploy-adhoc 專案,以 Ad-Hoc 執行 yum_repository 模組,在所有機器增加兩個倉庫來源,並以寫入 ad-hoc.sh,以腳本方式執行。
倉庫來源列表
1
2
3
4
5
6
7
8
9
|
1. BaseOS:
- name: BaseOS
- description: BaseOS
- baseurl: http://mirror01.idc.hinet.net/rockylinux/8.5/BaseOS/x86_64/os/
2. AppStream:
- name: AppStream
- description: AppStream
- baseurl: http://mirror01.idc.hinet.net/rockylinux/8.5/AppStream/x86_64/os/
|
yum_repository 模組範例 :
1
2
3
4
5
|
- name: Add repository
yum_repository:
name: epel
description: EPEL YUM repo
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
|
- name: add BaseOS repo
yum_repository:
name: BaseOS
description: BaseOS
baseurl: http://mirror01.idc.hinet.net/rockylinux/8.5/BaseOS/x86_64/os/
- name: add AppStream repo
yum_repository:
name: AppStream
description: AppStream
baseurl: http://mirror01.idc.hinet.net/rockylinux/8.5/AppStream/x86_64/os/
4.2 LAB 解答
參考解答在 deploy-adhoc/solution 資料夾內。
1
2
|
touch ad-hoc.yml
chmod +x ad-hoc.yml
|
1
2
3
4
5
6
7
|
#!/bin/bash
# add BaseOS repo
ansible all -m yum_repository -a "name=BaseOS description=BaseOS baseurl=http://mirror01.idc.hinet.net/rockylinux/8.5/BaseOS/x86_64/os/"
# add AppStream repo
ansible all -m yum_repository -a "name=AppStream description=AppStream baseurl=http://mirror01.idc.hinet.net/rockylinux/8.5/AppStream/x86_64/os/"
|
Quote
如果使用 HowHow 的 Lab 環境,可以再練習前或是練習後,於 bastion 機器上輸入 rht-vmctl reset <server name>
重置機器,恢復原始環境。
5. 小結
善用 ansible-doc 可以很快速的查閱模組的使用,並透過 ad-hoc 命令執行單一任務驗證。建議多開個終端機一個專門查閱 ansible-doc ,可以幫助使用者快速查閱及使用。