Ansible 在執行時,需要明確定義要使用哪一種角色連線、使用哪一種連線機制、要不要提升root權限、提升權限是否要輸入密碼及 inventory 主機清單擺放位置,在每次執行透過 CLI 實在是很麻煩,透過檔案管理就容易很多。
Ansible 設定檔可以存放在多個路徑,當然也會有優先級之分,主要分成四種設定檔配置方式(依優先低至優先高排序):
Ansible 安裝好後會自動產生這個設定檔,裡面內容大多數都是註解,這個設定檔也是全域設定檔,任何使用者都可以使用,一般不會選擇使用此設定檔。
配置在某個使用者的家目錄底下,僅有該使用者可以使用。
跟者專案資料夾底下做配置,這是最常使用的方式,也建議使用該方式。
臨時性任務要加參數覆蓋或是新增 ansible.cfg 可以使用該方式。
Ansible 能設定的功能非常非常多,不過大多數沒有那麼進階使用者是用不到的,只列出標準常用的內容範例(remote_user 依實際角色為主):
1
2
3
4
5
6
7
8
9
10
|
[defaults]
inventory = ./inventory
remote_user = devops
ask_pass = True
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = True
|
內容說明
defaults 群組:
- inventory : 載入主機清單的路徑
- remote_user : 登入被控端機器的帳號
- ask_pass : 登入帳號是否要輸入密碼 (如果是無密碼 login 改為 False)
privilege_escalation 群組:
- become : 是否提升管理者權限 ( 如果要進行系統等操作必須使用 )
- become_method : 提升的方式, Linux 預設使用 sudo
- become_user : 管理者的角色, Linux 預設為 root
- become_ask_pass : 提升管理者是否需要輸入密碼 (如果有在 sudofile 啟用 nopasswd ,改為 False)
Note
再操作時如果有要輸入遠端登入帳號密碼或是提升管理員的密碼,只要執行 ansible 時輸入一次即可。
建議使用無密碼 login 跟建立 無密碼 sudo 比較容易!
下方這些設定較為進階,有需要在啟用即可。
每一次 task 任務都透過 SSH 連線,預設每一筆連線認證為 60 秒,如果執行任務較多時間較長,可以延長連線認證持久化時間。
編輯 ansible.cfg 並添加下方設定
1
2
|
[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
|
啟動 Pippelining 可以稍微降低 SSH 連線,此功能預設是關閉的,所有機器的 sudofile 需停用 requiretty 功能才可以使用 。
編輯 ansible.cfg 並添加下方設定
1
2
|
[ssh_connection]
pipelining = True
|
如果想知道每一次執行 task 所消耗的 Memory 、 CPU 、 使用 pid等,可以透過啟用插件來查看。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
CGROUP PERF RECAP ************************************************************************************************************************************************************************************************
Memory Execution Maximum: 66.40MB
cpu Execution Maximum: 74.71%
pids Execution Maximum: 9.00
memory:
Gathering Facts (5254002a-3564-920f-c96f-000000000010): 65.17MB
add baseos repo (5254002a-3564-920f-c96f-00000000000a): 53.81MB
add appstream repo (5254002a-3564-920f-c96f-00000000000b): 48.24MB
install http & php (5254002a-3564-920f-c96f-00000000000c): 59.11MB
cpu:
Gathering Facts (5254002a-3564-920f-c96f-000000000010): 74.71%
add baseos repo (5254002a-3564-920f-c96f-00000000000a): 58.02%
add appstream repo (5254002a-3564-920f-c96f-00000000000b): 34.42%
install http & php (5254002a-3564-920f-c96f-00000000000c): 67.90%
pids:
Gathering Facts (5254002a-3564-920f-c96f-000000000010): 8.00
add baseos repo (5254002a-3564-920f-c96f-00000000000a): 9.00
add appstream repo (5254002a-3564-920f-c96f-00000000000b): 9.00
install http & php (5254002a-3564-920f-c96f-00000000000c): 9.00
|
- 編輯 ansible.cfg 並添加下方設定
1
2
3
4
5
|
[defaults]
callback_whitelist = time, profile_tasks, cgroup_pefr_recap
[callback_cgroup_perf_recap]
control_group=ansible_profile
|
- 創建 cgroup
1
|
[student@workstation ~]$ sudo cgcreate -a student:student -t student:student -g cpuacct,memory,pids:ansible_profile
|
- 執行 ansible 並配置 cgexe 使用
1
2
3
4
5
|
[student@workstation example]$ cgexec -g cpuacct,memory,pids:ansible_profile ansible-playbook example-playbook.yaml
PLAY [exmaple] ***************************************************************************************************************************************************************************************************
...
|
Tip
cgroup 管理需 libcgroup-tools 套件
Ansible 組態檔是必要元素之一,可以用的參數非常多,非常不好記憶,其實只要用常用的設定檔參數配置好就行( 只有幾個參數需要記憶就行 ),多數的參數都使用 Ansible 預設即可。