附註:
- 此教學課程可在 Oracle 提供的免費實驗室環境中取得。
- 它使用 Oracle Cloud Infrastructure 證明資料、租用戶及區間的範例值。完成實驗室時,請將這些值取代為您雲端環境特定的值。
使用 Oracle Linux Automation Engine 撰寫手冊
簡介
Oracle Linux Automation Engine (Oracle Linux Automation Manager 的一個元件) 是一種自動化工具,用於部署軟體、設定系統,以及以手冊形式協調工作 (例如升級和更新)。Oracle Linux Automation Engine 一開始使用 ansible
套件,即源自開放原始碼的 ansible-core
軟體套件。
下列教學課程介紹使用 Oracle Linux Automation Engine 撰寫手冊。
目標
在本教學課程中,您將瞭解如何:
- 安裝 Oracle Linux Automation Engine
- 建立產品目錄檔案
- 執行 ad hoc 指令
- 撰寫並執行手冊
必要條件
-
至少為兩個 Oracle Linux 系統,配置如下:
- 具備
sudo
權限的非 root 使用者 - 非 root 使用者的 ssh 金鑰組
- 使用無密碼 SSH 登入從一個主機至另一個主機 SSH 的功能
- 具備
部署 Oracle Linux
注意:如果在您自己的租用戶中執行,請先閱讀 linux-virt-labs
GitHub 專案 README.md 並完成先決條件,再部署實驗室環境。
-
在 Luna 桌面上開啟終端機。
-
複製
linux-virt-labs
GitHub 專案。git clone https://github.com/oracle-devrel/linux-virt-labs.git
-
變更至工作目錄。
cd linux-virt-labs/olam
-
安裝所需的集合。
ansible-galaxy collection install -r requirements.yml
-
更新 Oracle Linux 執行處理組態。
cat << EOF | tee instances.yml > /dev/null compute_instances: 1: instance_name: "ol-control-node" type: "control" 2: instance_name: "ol-host" type: "remote" EOF
-
部署實驗室環境。
ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6" -e "@instances.yml"
自由實驗室環境需要額外的變數
local_python_interpreter
,此變數會設定ansible_python_interpreter
代表在 localhost 上執行的播放。環境會安裝 Oracle Cloud Infrastructure SDK for Python 的 RPM 套裝程式 (位於 python3.6 模組底下),因此需要此變數。預設部署資源配置使用 AMD CPU 和 Oracle Linux 8。若要使用 Intel CPU 或 Oracle Linux 9,請在部署命令中新增
-e instance_shape="VM.Standard3.Flex"
或-e os_version="9"
。重要事項:等待手冊順利執行並到達暫停工作。在此階段的手冊中,Oracle Linux 的安裝已完成,且執行處理已就緒。請注意先前的播放,其會列印部署節點的公用和專用 IP 位址,以及執行實驗室時所需的任何其他部署資訊。
設定 Oracle Linux Automation Engine 控制節點
控制節點是執行 Oracle Linux Automation Engine 手冊的系統。執行手冊需要安裝 Oracle Linux Automation Engine 套件。
-
設定等於控制節點之 IP 位址的變數。
export CONTROL="<ip_address_of_ol-control-node>"
-
開啟終端機,然後將 SSH 金鑰組複製到控制節點。
scp -rp ~/.ssh/id_rsa* oracle@$CONTROL:~/.ssh/
-
設定 SSH 私密金鑰的權限。
ssh oracle@$CONTROL "chmod 600 ~/.ssh/id_rsa"
-
透過 SSH 連線至 ol-control-node 系統。
ssh oracle@$CONTROL
-
安裝 Oracle Linux Automation Engine 套裝程式和相依性。
sudo dnf install -y ansible-core
ansible-core
套裝軟體位於 Oracle Linux 的ol8_appstream
儲存庫中。 -
測試套裝軟體安裝。
ansible --version
輸出將會顯示命令版本、組態詳細資訊以及 Python 版本相依性。
注意:如果輸出顯示錯誤:Ansible 需要將地區設定編碼設為 UTF-8;偵測到「無」。,代表
ansible
的地區設定設定不正確。設定這兩個環境變數來修正問題:export LC_ALL="en_US.UTF-8" export LC_CTYPE="en_US.UTF-8"
建立產品目錄
產品目錄檔案包含屬於您基礎架構的主機詳細資訊,或是您將使用 Oracle Linux Automation Engine 管理的主機。這些詳細資訊指示如何在執行特設命令或手冊時連線至這些主機。
收集 IP 位址或主機名稱資訊
若要建立 Oracle Linux Automation Engine 產品目錄,您必須判斷所管理系統或系統的 IP 位址或可解析主機名稱。這些步驟會根據系統的類型和部署位置而有所不同。
在提供的免費實驗室環境中,我們需要使用 Luna Lab 資源頁籤找到之 ol-host
系統的 IP 位址。
建立產品目錄檔案
Oracle Linux Automation Engine 將預設產品目錄置於 /etc/ansible/hosts
。它也允許專案層級存貨檔案。使用專案層次產品目錄時,執行特設命令或手冊時,您必須使用 -i
選項提供產品目錄檔案的路徑。
從 Oracle Linux Automation Engine 控制節點上的終端機:
-
建立專案目錄
mkdir ~/ol-automation
-
移至新目錄,然後使用您選擇的文字編輯器開啟新的產品目錄檔案。在這裡,我們將使用
vi
。cd ~/ol-automation
vi inventory
-
輸入
i
以輸入vi
插入模式。 -
增加所提供之 ol-host 系統的公用 IP 位址。此檔案只能包含 IP 位址 。
範例:
130.61.100.96
-
儲存並關閉檔案。如果使用
vi
,您可以輸入ESC
、:wq!
和ENTER
。 -
驗證產品目錄檔案。
ansible-inventory -i inventory --list
輸出會顯示產品目錄資訊。
範例輸出:
{ "_meta": { "hostvars": {} }, "all": { "children": [ "ungrouped" ] }, "ungrouped": { "hosts": [ "130.61.100.96" ] } }
參照
all
會參照產品目錄檔案中的每個主機,而ungrouped
區段則適用於不屬於所列群組的任何主機。 -
編輯產品目錄並將主機置於名為
development
的群組內。vi inventory
-
輸入
i
以輸入vi
插入模式。 -
使用下方的 IP 位址在括號內新增群組名稱,如範例所示。
範例:
[development] 130.61.100.96
-
儲存並關閉檔案。如果使用
vi
,您可以輸入ESC
、:wq!
和ENTER
。 -
再次執行驗證步驟以查看新增至輸出的群組。
ansible-inventory -i inventory --list
輸出會顯示群組的更新資訊。
範例輸出:
{ "_meta": { "hostvars": {} }, "all": { "children": [ "development", "ungrouped" ] }, "development": { "hosts": [ "130.61.100.96" ] } }
執行特設指令
Oracle Linux Automation Engine 有數個執行中的模組,不需要撰寫手冊。其中最基本的是 ping
模組,此模組會根據產品目錄中提供的詳細資訊嘗試建立 SSH 連線。
從 Oracle Linux Automation Engine 控制節點上的終端機:
-
執行
ping
模組。ansible all -i inventory -m ping
all
選項會指示命令針對-i
選項指定之產品目錄檔案中列出的所有主機執行。此指令也接受個別主機名稱或群組。-m
選項會指定要執行的模組。 -
出現提示時鍵入
yes
,接受 ECDSA 金鑰指紋。根據您的環境,
ping
可能因下列輸出而失敗:範例輸出:
130.61.100.96 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '130.61.100.96' (ECDSA) to the list of known hosts.\r\noracle@130.61.100.96: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true }
本機使用者帳戶
oracle
不存在於我們嘗試管理的主機上,因此發生此失敗。使用變數ansible_user
將有效的遠端使用者新增至產品目錄以修正此問題。在產品目錄檔案中,指定主機名稱參照,並將 IP 位址指定給變數ansible_host
。 -
新增遠端使用者至產品目錄檔案。
vi inventory
-
輸入
i
以輸入vi
插入模式。 -
新增兩個變數,如範例所示。
範例:
[development] ol-host ansible_host=130.61.100.96 ansible_user=opc
免費實驗室環境提供預先設定的系統,用於測試包含
opc
使用者的遠端管理。opc
是在 Oracle Cloud Infrastructure (OCI) 中 Oracle Linux 執行處理上建立的預設使用者。我們可以在主機名稱使用ol-host
範例名稱。 -
儲存並關閉檔案。如果使用
vi
,您可以輸入ESC
、:wq!
和ENTER
來執行此操作。 -
使用主機名稱而非
all
重新執行ping
模組。ansible ol-host -i inventory -m ping
命令以類似顯示的結果順利執行。
範例輸出:
ol-host | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
撰寫手冊
手冊是一組以正確的 YAML 語法撰寫的指示,可在單一主機或主機群組執行。這些檔案的預設副檔名為 .yml
或 .yaml
。
我們的第一個手冊將鎖定已建立產品目錄檔案中的 all
主機。此範例手冊由列印除錯訊息的單一工作所組成。
從 Oracle Linux Automation Engine 控制節點上的終端機:
-
建立新的手冊檔 。
vi hello.yml
-
輸入
i
以輸入vi
插入模式。 -
新增以下的文字到手冊檔 。
範例:
--- - hosts: all tasks: - name: Print message debug: msg: Hello from Oracle Linux
播放簿應一律以
---
為開頭,後面接著指定要管理之主機的hosts
行。all
值表示此手冊將對產品目錄中列出的每個主機採取動作。或者,您可以指示手冊只針對特定主機或群組清單執行工作,方法是列出以冒號或逗號分隔的每個目標。接著,工作會使用除錯模組來列印訊息。name
行會在執行手冊時,讓 Oracle Linux Automation Engine 在終端機中顯示輸出。 -
儲存並關閉檔案。如果使用
vi
,您可以輸入ESC
、:wq!
和ENTER
來執行此操作。 -
針對產品目錄執行 hello.yml 手冊。
ansible-playbook -i inventory hello.yml
您可以改為使用
-u username
選項在命令行傳送遠端使用者名稱,而不是在產品目錄檔案中使用ansible_user
變數。ansible-playbook -i inventory hello.yml -u opc
如果一切都正常運作,命令就會順利完成並列印除錯訊息。
範例輸出:
PLAY [all] ********************************************************************* TASK [Gathering Facts] ********************************************************* [WARNING]: Platform linux on host ol-host is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/referen ce_appendices/interpreter_discovery.html for more information. ok: [ol-host] TASK [Print message] *********************************************************** ok: [ol-host] => { "msg": "Hello from Oracle Linux" } PLAY RECAP ********************************************************************* ol-host : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
由於產品目錄僅定義單一主機
ol-host
,因此播放簿會執行一次。執行此手冊中所列的作業之前,Oracle Linux Automation Engine 會執行預設工作收集資訊,稱為事實。此工作會提取遠端主機的相關資訊,而手冊稍後可能會利用這些資訊透過條件自訂工作行為。 -
執行下列 ad hoc 命令以列印主機的 fact 完整清單。
ansible ol-host -i inventory -m setup
接下來的步驟
此訊息和事實輸出會確認您已順利使用 Oracle Linux Automation Engine 執行您的第一個手冊。
相關連結
其他學習資源
探索 docs.oracle.com/learn 上的其他實驗室,或存取 Oracle Learning YouTube 頻道上的更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。
如需產品文件,請造訪 Oracle Help Center 。
Write a Playbook with Oracle Linux Automation Engine
F52963-04
Copyright ©2021, Oracle and/or its affiliates.