附註:

使用 Oracle Linux Automation Engine 執行 Oracle Linux 工作

簡介

Oracle Linux Automation Engine 可讓管理員透過一系列的手冊和工作,使用基礎架構即程式碼 (IaC) 組態管理工具,將 Oracle Linux 的初始設定自動化,並執行其他管理工作。

目標

在本教學課程中,您將瞭解如何:

必要條件

部署 Oracle Linux Automation Engine

注意:如果在您自己的租用戶中執行,請先閱讀 linux-virt-labs GitHub 專案 README.md 並完成先決條件,再部署實驗室環境。

  1. 在 Luna 桌面上開啟終端機。

  2. 複製 linux-virt-labs GitHub 專案。

    git clone https://github.com/oracle-devrel/linux-virt-labs.git
    
  3. 變更至工作目錄。

    cd linux-virt-labs/olam
    
  4. 安裝所需的集合。

    ansible-galaxy collection install -r requirements.yml
    
  5. 更新 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"
    olam_type: olae
    EOF
    
  6. 建立產品目錄檔案。

    cat << EOF | tee hosts > /dev/null
    localhost ansible_connection=local ansible_connection=local ansible_python_interpreter=/usr/bin/python3.6
    EOF
    
  7. 部署實驗室環境。

    ansible-playbook create_instance.yml -i hosts -e "@instances.yml"
    

    免費實驗室環境需要額外的 localhost ansible_python_interpreter 變數,因為它會為 Python 適用的 Oracle Cloud Infrastructure SDK 安裝 RPM 套件。安裝此套裝軟體的位置位於以您 Oracle Linux 版本為基礎的系統預設 Python 模組底下。使用產品目錄變數可避免影響 localhost 以外的主機上執行的播放。

    預設部署資源配置使用 AMD CPU。您可以在命令行中傳送新的資源配置變數定義,以變更執行處理的資源配置。

    例如:-e instance_shape="VM.Standard3.Flex"

    同樣地,Oracle Linux 映像檔的預設版本會使用 `default_vars.yml 檔案中定義的變數 os_version。您可以在命令行中傳送 Oracle Linux 主要版本,即可修改這個值。

    例如:-e os_version="9"

    重要事項: 請等待播放手冊順利執行,然後到達暫停工作。在手冊的這個階段,Oracle Linux 的安裝已完成,實例已就緒。請注意,上一個播放會列印其部署節點的公用和專用 IP 位址。

撰寫初始安裝手冊

許多手冊都運用了包含索引鍵值組的變數和變數檔案,讓實際的手冊工作在程式碼保持靜態時能夠動態進行。播放手冊包含可在執行時期變成播放部分的變數,播放手冊會在執行工作時使用其值。

Oracle Linux Automation Engine 可讓您在數個位置定義這些變數,每個變數都有特定的優先順序。播放手冊層級的變數是使用 varsvars_files 指令在播放手冊中定義。vars 指令將變數指定為播放的一部分,而 vars_files 指令則包含包含變數的外部檔案。開發人員可以從其他遊戲動態或靜態方式建立這些變數,因為我們會在本範例中定義系統組態,然後再執行手冊。

  1. 開啟新的終端機,並透過 SSH 連線至 ol-control-node 系統。

    ssh oracle@<ip_address_of_instance>
    
  2. 檢查 Oracle Linux Automation Engine 命令是否可用。

    ansible --version
    
  3. 建立工作專案目錄。

    mkdir -p ~/ol-playbook
    
  4. 為專案建立變數目錄與檔案。

    mkdir ~/ol-playbook/vars
    
    touch ~/ol-playbook/vars/defaults.yml
    
  5. 將變數和值新增至檔案。

       
    cat << EOF | tee ~/ol-playbook/vars/defaults.yml > /dev/null
    ---
    username: oracle
    user_default_password: oracle
    ssh_key_file: id_rsa
    ssh_private_key_file: "{{ lookup('file', lookup('env','HOME') + '/.ssh/' + ssh_key_file + '.pub') }}"
    additional_packages: ['git']
    EOF
       
    

    此資訊說明每個變數,以及我們如何使用:

    • username:執行手冊時建立的 sudo 使用者名稱。在此範例中,使用者的名稱將是 oracle
    • user_default_passwordoracle 使用者建立時的預設密碼。執行 sudo 指令時需要密碼。
    • ssh_key_file:設定使用者之 SSH 金鑰組的名稱。
    • ssh_private_key_file:將使用者的 SSH 公開金鑰複製到指定路徑的遠端使用者 authorized_key 檔案。此範例使用 lookup 外掛程式在本機使用者 $HOME/.ssh/ 目錄中尋找公開金鑰 id_rsa.pub
    • additional_packages:新增要以陣列格式安裝之任何其他套裝軟體的名稱。陣列中的每個套裝軟體都應該以單引號括住,並以逗號分隔。如果安裝 Appstream 模組 (例如 container-tools),陣列看起來會像是 ['git',' @container-tools:ol8']
  6. 建立手冊檔 。

       
    cat << EOF | tee ~/ol-playbook/setup.yml > /dev/null
    ---
    - hosts: all
      become: yes
      vars_files:
        - vars/defaults.yml
    
      tasks:
    
      - name: Generate new ssh keypair
        community.crypto.openssh_keypair:
          path: "~/.ssh/{{ ssh_key_file }}"
          size: 2048
          comment: olam ssh keypair
        become: true
        become_user: "{{ username }}"
        delegate_to: localhost
    
      - name: Add a user account with access to sudo
        ansible.builtin.user:
          name: "{{ username }}"
          password: "{{ user_default_password | password_hash('sha512') }}"
          comment: Default Oracle user
          groups: wheel
          append: yes
          update_password: on_create
    
      - name: Set the authorized key for the user using a local public key file
        ansible.posix.authorized_key:
          user: "{{ username }}"
          state: present
          key: "{{ ssh_private_key_file }}"
    
      - name: install additional packages
        ansible.builtin.dnf:
          name: "{{ additional_packages }}"
          state: latest
    EOF
       
    

    小冊子的特定工作與模組名稱旨在讓小冊子能夠自我記錄。這些項目會指示播放的位置和人員:

    • hosts: all:此行指定來自產品目錄的主機將執行工作。
    • become: yes:指示此區段內依預設以 sudo 權限執行的作業。
    • vars_files" 此指令會載入包含此教學課程之手冊組態的變數檔案。

安裝必要的集合

ansible-core 套裝軟體包含一組用於管理主機的最小模組,這些模組組織在 ansible.builtin 集合中。集合是分送執行目標工作之手冊、角色、模組或 Plugin 的方法。ansible-core 需要下載並安裝內建項目以外所需的任何模組或集合。

當上述手冊使用 ansible.posix 集合時,我們需要安裝此集合與其他集合。若要這樣做,最簡單的方法是建立包含所有相依性的需求檔案。

  1. 建立必修課程檔案。

    cat << 'EOF' | tee ~/ol-playbook/requirements.yml > /dev/null
    ---
    collections:
      - name: ansible.posix
      - name: community.crypto
    EOF
    
  2. 安裝集合。

    ansible-galaxy collection install -r ~/ol-playbook/requirements.yml
    

    輸出顯示從 Galaxy 網站擷取壓縮的封存檔,然後將其安裝到 .ansible/collections. 下的主目錄中。

    注意:如果輸出顯示 ERROR: Ansible requires the locale encoding to be UTF-8; Detected None.,表示 ansible 的地區設定設定不正確。設定這兩個環境變數來修正問題:

    export LC_ALL="en_US.UTF-8"
    export LC_CTYPE="en_US.UTF-8"
    

執行手冊

執行此手冊之前,必須先為此專案建立產品目錄檔案,此專案指向我們計畫管理的遠端 Oracle Linux 執行處理。

  1. 將 ol-host 的 IP 位址指定給遠端系統的變數。

    此變數可讓您更輕鬆地撰寫建立產品目錄檔案的命令檔。

    export REMOTE=<ip_address_of_instance>
    
  2. 在專案目錄中建立新的產品目錄檔案。

    cat << EOF | tee ~/ol-playbook/inventory > /dev/null
    [production]
    ol-host ansible_host=$REMOTE
    EOF
    
  3. 變更至專案工作目錄。

    cd ~/ol-playbook
    
  4. 使用 ad hoc ping 命令測試連線。

    ansible ol-host -i ~/ol-playbook/inventory -m ping -u opc
    
    • -u:傳送 ssh 連線的使用者名稱。在免費的實驗室環境中,我們使用 opc 使用者 (Oracle Cloud Infrastructure (OCI) 中 Oracle Linux 執行處理上提供的預設使用者)。在提示符號處輸入 yes 以核准 ECDSA 金鑰指紋以繼續。

    命令以類似顯示的結果順利執行。

    範例輸出:

    ol-host | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "ping": "pong"
    }
    
  5. 執行此手冊 。

    ansible-playbook -i inventory setup.yml -u opc
    

    該指令應該可以成功執行,並顯示每個播放的 changed 結果。

連線至遠端主機

如果手冊成功執行,我們可以使用 username 變數中定義的使用者連接遠端系統。

  1. 透過 SSH 連線至 ol-host 系統。

    ssh oracle@$REMOTE
    

    如果您變更了 ssh_key_file 變數的值,則必須將 `-i' 選項傳送至 ssh,指向指定組的私密金鑰檔案。

    範例:

    ssh -i ~/.ssh/<local_ssh_private_key> <username>@<ip_address_of_host>
    
  2. 驗證所要求的套裝軟體安裝。

    登入之後,您可以確認該手冊已安裝 git 套裝軟體。

    git --version
    
  3. 中斷遠端主機連線。

    exit
    

新增儲存區域

Oracle Linux 為您部署關鍵任務應用軟體提供安全、可延展且可靠的平台。新增額外的儲存區域會用來安裝新的 Oracle 產品或第三方應用程式。Oracle Linux 提供輔助套裝程式,可處理這些額外儲存區域的佈建。

Oracle Linux YUM 伺服器提供 Oracle 提供之眾多儲存區域的詳細資訊。

  1. 將這些額外的工作新增到現有的手冊檔案中 。

    cat << EOF | tee -a setup.yml > /dev/null
      - name: Add the EPEL repository
        ansible.builtin.dnf:
          name: oracle-epel-release-el8
          state: present
        when:
          - ansible_distribution == 'OracleLinux'
          - ansible_facts['distribution_major_version'] == '8'
    
      - name: Add the EPEL repository
        ansible.builtin.dnf:
          name: oracle-epel-release-el9
          state: present
        when:
          - ansible_distribution == 'OracleLinux'
          - ansible_facts['distribution_major_version'] == '9'
    
      - name: Install the htop utility package
        ansible.builtin.dnf:
          name: htop
          enablerepo: ol8_developer_EPEL
          state: present
        when:
          - ansible_distribution == 'OracleLinux'
          - ansible_facts['distribution_major_version'] == '8'
    
      - name: Install the htop utility package
        ansible.builtin.dnf:
          name: htop
          enablerepo: ol9_developer_EPEL
          state: present
        when:
          - ansible_distribution == 'OracleLinux'
          - ansible_facts['distribution_major_version'] == '9'
    EOF
    

    此手冊會在定義的播放中新增數個工作。第一組會將 EPEL 儲存區域新增至 Oracle Linux 執行處理上的現有軟體儲存區域。同時,第二組播放將 htop 套裝軟體安裝在相同的目標主機實例上。

    值得注意的參數:

    • state: present:確定參照的套裝軟體已經在系統上,或使用 dnf 進行安裝。
    • enablerepo:僅在目前作業的持續時間內啟用特定儲存區域 (如果停用)。
  2. 請重新執行手冊以執行其他作業。

    ansible-playbook -i inventory setup.yml -u opc
    

    播放手冊已成功完成,執行時會顯示一些警告 。您現在可以忽略這些警告,因為它們不會影響目前的播放簿 。此手冊以 okchanged 狀態報告正確完成的工作。已變更狀態表示播放簿在要求最新版本的套裝軟體時新增 htop 套裝軟體或更新 dnf 快取以修改主機,而不只是檢查它是否存在。ok 表示任務完成,不需要任何動作。

  3. 驗證儲存庫和套裝軟體的安裝。

    ssh oracle@$REMOTE which htop
    
  4. 執行 htop 指令。

    ssh oracle@$REMOTE -t htop
    

    SSH 指令的 -t 選項會強制使用 TTY 配置,因為 htop 需要互動式 Shell。

  5. 鍵入 q 以結束 htop 程式。

新增、更新及刪除檔案和目錄

  1. 將下列各項新增至現有的手冊檔案,以建立目錄與檔案。

       
    cat << EOF | tee -a setup.yml > /dev/null     
    
      - name: Create an example directory (if it does not already exist)
        ansible.builtin.file:
          path: "/home/{{ username }}/example"
          state: directory
          mode: '0755'
        become_user: "{{ username }}"
    
      - name: Create an empty file
        ansible.builtin.file:
          path: "/home/{{ username }}/example/test.txt"
          state: touch
          # mode: u=rw,g=r,o=r
        become_user: "{{ username }}"
    EOF
       
    

    此手冊會在定義的播放中新增兩個工作 。首先會在遠端使用者的本位目錄中建立一個名為 example 的新目錄,第二個目錄會在新建立的 example 目錄中建立名為 test.txt 的空白檔案。

    值得注意的其他參數:

    • state:ansible.builtin.file 模組支援下列參數: absentdirectoryfilehardlinktouch 。這些作業會使用 directory 建立目錄路徑 (如果目錄路徑尚未存在),並使用 touch (如果目錄路徑尚未存在的話),來建立空白檔案。
    • mode:設定所建立物件的檔案系統權限。使用指令行時,您可以使用 0644 八進位或 u=rw,g=r,o=r 符號模式。如果您省略 mode: 參數,則會套用系統預設模式。
    • become_user: "":使用 Oracle Linux Automation Engine 權限呈報功能來執行任務。在先前的教學課程中,我們導入了 become: 權限呈報功能,以 root 使用者身分執行工作。這個範例說明如何使用類似功能,以其他使用者身分執行任務。我們將使用者設為 "" 變數,此變數在 vars/defaults.yml 檔案中預先定義為 oracle 使用者。因此,此作業會以 oracle 使用者身分執行,並且繼承該使用者的預設值。
  2. 執行手冊以執行其他作業。

    ansible-playbook -i inventory setup.yml -u opc
    
  3. 確認新目錄和檔案存在於遠端主機上。

    ssh oracle@$REMOTE ls -al example
    

    輸出會確認建立新目錄與空白檔案。請注意,test.txt 檔案的位元組為零, oracle 使用者擁有此檔案。

  4. 新增多重檔案。

    系統設定期間,您可能需要在目錄中建立數個檔案。您可以在單一單元作業的目錄中建立多個檔案,而不使用個別作業。

       
    cat << EOF | tee -a setup.yml > /dev/null     
    
      - name: Add multiple files
        ansible.builtin.file:
          path: "/home/{{ username }}/example/{{ item }}"
          state: touch
        with_items:
        - file01.txt
        - file02.txt
        - file03.txt
        - file04.txt
        become_user: "{{ username }}"
    EOF
       
    

    此作業使用迴圈從清單建立數個空白檔案。

    • path::定義遠端系統上寫入檔案的位置。Oracle Linux Automation Engine 會在程式實際執行時,以 with_items 參數中的每個 item 取代 `` 變數。
    • with_items::此參數指示執行此工作時要迴圈的 items 清單。雖然此範例僅使用四個檔案名稱,但您可以視需要建立清單。
  5. 執行手冊以執行其他作業。

    ansible-playbook -i inventory setup.yml -u opc
    
  6. 確認新目錄和檔案存在於遠端主機上。

    ssh oracle@$REMOTE ls -al example
    

    輸出會顯示原始的 test.txt 檔案以及新的檔案。

  7. 新增一行內容至檔案。

       
    cat << 'EOF' | tee -a setup.yml > /dev/null     
    
      - name: Insert some text into the test.txt file
        ansible.builtin.lineinfile:
          path: "/home/{{ username }}/example/test.txt"
          line: This is a test
          create: True
          owner: "{{ username }}"
          group: "{{ username }}"
          mode: '0644'
    
      - name: Add an extra line after the line just inserted
        ansible.builtin.lineinfile:
          path: "/home/{{ username }}/example/test.txt"
          regexp: '^a test'
          insertafter: 'This is a test'
          line: This is an additional test.
          create: True
          owner: "{{ username }}"
          group: "{{ username }}"
          mode: '0644'
    
      - name: Get the contents of the test.txt file
        ansible.builtin.command: cat ~/example/test.txt
        register: command_output
        become_user: "{{ username }}"
    
      - name: Print the results of the cat command
        ansible.builtin.debug:
          msg: "{{ command_output }}"
    
      - name: Print only the lines added to the text file
        ansible.builtin.debug:
          msg: "{{ command_output.stdout_lines }}"
    EOF
       
    
    • 前兩個作業使用 ansible.builtin.lineinfile 來新增和更新檔案中的單行。

    • 剩餘的任務會顯示確認變更的方法。播放手冊首先使用 ansible.builtin.command 將更新過的檔案內容輸出到標準輸出,然後將它儲存在 register 變數的記憶體中。接著會使用 ansible.builtin.debug 模組,將該內容列印至畫面作為播放手冊輸出的一部分。第二個除錯作業示範將輸出限制在前一個作業之 JSON 輸出的特定部分的方法。

  8. 執行手冊以執行其他作業。

    ansible-playbook -i inventory setup.yml -u opc
    

    此手冊中的最後一項作業是使用 debug 模組來列印檔案的內容。

  9. 新增多行內容至檔案。

       
    cat << EOF | tee -a setup.yml > /dev/null     
    
      - name: Add two lines into test.txt
        ansible.builtin.blockinfile:
          path: "/home/{{ username }}/example/test.txt"
          insertafter: 'This is some updated text that was added later.'
          block: |
            "Welcome to {{ ansible_hostname }}"
            "Last updated on {{ ansible_date_time.iso8601 }}"
    
      - name: Create a new file and insert content into it
        ansible.builtin.copy:
          content: |
            === The text below was added by Oracle Linux Automation Engine ==========
    
            Hello from the ansible.builtin.copy module.
            This task is an example of inserting multiple lines into a text file.
            You can insert more lines if you want.
    
            === The text above was added by Oracle Linux Automation Engine ===========
          dest: "/home/{{ username }}/example/testing.txt"
          mode: '0644'
        become_user: "{{ username }}"
    EOF
       
    
  10. 執行手冊以執行其他作業。

    ansible-playbook -i inventory setup.yml -u opc
    
  11. 確認檔案中有兩個新行。

    ssh oracle@$REMOTE cat example/test.txt
    

    輸出會顯示新增的內容,並根據手冊在程式實際執行時對遠端主機進行的自動事實收集,解譯作業的變數值。

  12. 確認建立新檔案並新增我們的內容。

    ssh oracle@$REMOTE cat example/testing.txt
    
  13. 刪除檔案和目錄。

    除了建立檔案和目錄以及新增文字之外,Oracle Linux Automation Engine 還可以刪除這些項目。讓我們移除此手冊所建立的目錄和所有檔案,讓系統維持與啟動時相同的狀態。這些作業會先使用設為 absentstate: 參數移除檔案清單,然後移除目錄。第二個工作則是在單一步驟中處理移除檔案與目錄。不過,我們也包含第一項額外 Oracle Linux Automation Engine 功能示範的任務。

        
    cat << EOF | tee -a setup.yml > /dev/null     
    
      - name: Delete multiple files
        ansible.builtin.file:
          path: '{{ item }}'
          state: absent
        with_items:
          - "/home/{{ username }}/example/test.txt"
          - "/home/{{ username }}/example/file01.txt"
          - "/home/{{ username }}/example/file02.txt"
          - "/home/{{ username }}/example/file03.txt"
          - "/home/{{ username }}/example/file04.txt"
          - "/home/{{ username }}/example/testing.txt"
    
      - name: Recursively remove directory
        ansible.builtin.file:
          path: "/home/{{ username }}/example"
          state: absent
    EOF
        
    

    您可以使用顯示的第二個工作來遞迴刪除目錄與任何內容。如果您只想刪除特定檔案,就可以使用第一個個別刪除檔案的工作。如果要刪除檔案和檔案以及目錄,第二個工作會更有效率。

  14. 執行手冊以執行其他作業。

    ansible-playbook -i inventory setup.yml -u opc
    
  15. 檢查是否移除指定的目錄。

    ssh oracle@$REMOTE ls
    

    輸出不足會確認此自學課程期間所建立的指定目錄和檔案已不存在。

接下來的步驟

恭喜您達成這個目標。本教學課程介紹 Oracle Linux Automation Engine 的幾種方式,可讓您自動化 Oracle Linux 上的例行工作,包括安裝套裝軟體、建立、更新及刪除檔案和目錄,以及新增使用者帳戶至 Oracle Linux。我們也示範如何在執行 Playbook 時,利用 Oracle Linux Automation Engine 的 ansible.builtin.debug 模組向終端機顯示資訊。有了這些技能,您就可以開始創投,寫下您的手冊。

其他學習資源

探索 docs.oracle.com/learn 上的其他實驗室,或存取 Oracle Learning YouTube 頻道上的更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。

如需產品文件,請造訪 Oracle Help Center