環境準備:
方法一、 透過 Container 快速打造環境
- 練習環境可以透過 HowHow所創建的 container,快速完成,Docker-compose下載連結在此 ~
- 輸入 docker-compose up -d 即可開箱使用 。
- 詳細使用請參考 HowHow 的網站 。
方法二、 自己打造環境
- 需要五台機器:
- 分別為 workstation.lab.example.com 控制端
- servera.lab.example.com 至 serverd.lab.example.com 被控端機器
- 每台機器均需創建 devops 角色(具備sudo能力),且可以互相無密碼 login
- 控制端機器創建 student 角色,用來執行 ansible指令
- 考題為 Red Hat 8系列,請準備RedHat、 Centos 或是Rocky Linux
Ansible 模擬考第一題,環境配置
- 請在 Workstation 上安裝 ansible 及組態 ansible.cfg、inventory , 其要求如下:
1. 安裝 ansible 及其必要之套件
2. 執行ansible目錄為 /home/student/ansible
3. 建立 /home/student/ansible/inventory
4. inventory 分別為:
servera.lab.example.com 為 dev 組
serverb.lab.example.com 及 serverc.lab.example.com 為 prod 組
serverd.lab.example.com 為 balancer組
prod 為 webservers 子組
5. 建立 /home/student/ansible/ansible.cfg,remote_user 為 devops
考題解答
- ansible.cfg
- 從題目可看出透過 devops 角色為主要執行者
- 並賦予切換 sudo 能力
- 解答參考如下
[defaults]
inventory= ./inventory
ask_sudo_pass = False
remote_user = devops
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
- inventory
- 其他組配置並不難,依據考題配置即可,然而題目說的prod為webservers子組,意思為 webservers 是個母群組,並且包含prod群組
- 解答參考如下
[dev]
servera.lab.example.com
[prod]
serverb.lab.example.com
serverc.lab.example.com
[balancer]
serverd.lab.example.com
[webservers:children]
prod
- 配置完後,記得先執行一下 ping 確保所有被控機器均能正常回應
ansible all -m ping

- 如果都為顯示成功,表示環境配置已經完成,可以進入下一章考題繼續