--- tags: Ansible, Ubuntu Server 20.04 --- # How to install and configure Ansible on Ubuntu Server 20.04 ##### `Infrastructure as Code` `Configuration Management` `Automation Tool` `Ansible` `Ubuntu Server 20.04` > [name=Mr. Akashic] [time=Sun, Feb 28, 2021] ## Implementation ### Installing Ansible on Ubuntu 使用Ansible官方提供的Ubuntu個人套件儲存庫(PPA)<a href="#s4"><sup>[1]</sup></a>套件來源,安裝Ansible。請在您的電腦(control node)上執行以下指令設定PPA和安裝Ansible: ```bash= sudo apt update sudo apt install software-properties-common sudo apt-add-repository --yes --update ppa:ansible/ansible sudo apt install ansible ``` :::info :warning: **注意:** 在舊的Ubuntu發行版,請將"software-properties-common"改成"python-software-properties",並使用<span class="dark_orange">apt-get</span>指令安裝Ansible。另外,只有在較新的Ubuntu發行版(18.04、18.10等)才有<span class="dark_orange">-u</span>或<span class="dark_orange">--update</span>參數,所以請根據您的Ubuntu版本執行合適的指令。 ::: #### 實作步驟一畫面 ![](https://i.imgur.com/QTtjXpP.png) 也可以執行下列指令建立Ansible的deb檔,往後便可執行Ansible的deb檔進行離線安裝Ansible。展開觀看完整指令。 ```bash= $ make deb ``` :::spoiler 完整指令 ```bash= apt-get install libxml2-utils sshpass devscripts python-yaml python-paramiko python-jinja2 cdbs debhelper dpkg-dev git-core reprepro asciidoc git clone git://github.com/ansible/ansible.git --recursive cd ./ansible git checkout [branch-with-desired-version] # stable-2.X etc. git pull --rebase git submodule update --init --recursive make && make deb dpkg -i deb-build/[some-path]/ansible[some-build-details].deb ``` ::: </br> <div id="s4">1. 個人套件儲存庫(PPA): 是Launchpad網站提供Ubuntu的使用者上傳打包好的套件,讓其他Ubuntu的使用者可以下載或更新套件。</div> ### Installing Ansible on Debian Debian用戶可以使用與Ubuntu PPA上相同的來源安裝Ansible。將下列指令加入/etc/apt/sources.list。 ```bash deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main ``` 🌳Linux每個發行版有自己的套件管理系統和套件庫,trusty就是Ubuntu 14.04的版本名稱,因此它的套件庫在網路上的目錄路徑就是`http://ppa.launchpad.net/ansible/ ansible/ubuntu/dists/trusty/`,而main是Ubuntu官方對套件庫的分類,會放在main目錄的套件就表示該套件是官方支持的免費且開源的套件。 <p> 接著執行下列指令: ```bash= sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367 sudo apt update sudo apt install ansible ``` :::info :warning: **注意:** 上述的安裝方式已經被驗證可以在Debian Jessie和Stretch作業系統中從Trusty<a href="#s5"><sup>[1]</sup></a>的檔案庫中下載並安裝Ansible,如果在早期的作業系統版本可能不支援此安裝方式。此外,在舊版的作業系統中,請使用<span class="dark_orange">apt-get</span>指令安裝Ansible。</br></br> <div id="s5">1. Trusty: 為Ubuntu 14.04版本的名稱(codename)。</div> ::: ### Setting Up the Inventory File Inventory檔案用來管理要做自動化維運的manged nodes,該檔案預設的存放路徑為<span class="dark_orange">/etc/ansible/</span>,檔名為hosts。您也可以使用<span class="dark_orange">-i <path></span>指令指定不同路徑的inventory檔案,或使用雲端的inventory檔案。 Inventory檔案內容支援多種資料格式進行編寫,像是INI或YAML等。在您作為control node的電腦上執行下列指令開啟並編輯inventory檔案。可將<span class="dark_orange">nano</span>換成自己慣用的文字編輯器。 ```bash= sudo nano /etc/ansible/hosts ``` #### 實作步驟二畫面 ![](https://i.imgur.com/MyVpV1h.png) 編輯完成後按 <span class="dark_orange">CTRL+X</span>然後按<span class="dark_orange">Y</span>和<span class="dark_orange">ENTER</span>確認儲存修改。若要檢查inventory檔案有管理哪些manged nodes可執行以下指令。 ```bash= ansible-inventory --list -y ``` #### 實作步驟三畫面 ![](https://i.imgur.com/pShjRKU.png) 此步驟已完成inventory檔案的設定,接著進行連線測試。 ### Testing Connection 設定完inventory檔後,接著檢查control node是不是可以透過SSH與在inventory檔案的被控端機器做連線,在control node執行指令如下: ```bash= ansible all -m ping -u tests ``` <span class="dark_orange">ping</span>參數為Ansible內建的功能模組,用來測試control node與inventory檔案內的被控端機器的連線狀況。 其中<span class="dark_orange">-u</span>參數,可以指定要與managed node的哪個使用者帳戶連線。如果不使用<span class="dark_orange">-u</span>參數,Ansible會使用control node的使用者帳戶做為被控端機器的使用者帳戶進行連線(和SSH連線機制一樣)。 <span class="dark_orange">tests</span>參數為被控端機器的使用者帳號名稱。 如果control node與被控端機器是第一次使用SSH進行連線,會詢問是否加入被控端機器的金鑰,作為之後連線的安全性認證,請輸入<span class="dark_orange">yes</span>然後按<span class="dark_orange">ENTER</span>。 若收到被控端機器回傳<span class="dark_orange">pong</span>,表示已經可以從control node傳送Ansible指令和playbooks讓被控端機器執行動作或任務。 #### 實作步驟四畫面 ![](https://i.imgur.com/tDZ9bMs.png) ## Ansible Ansible是一種非置入式自動化工具<a href="#s1"><sup>[1]</sup></a>,主要透過SSH傳送指令的方式管理遠端的多部機器(managed node, 被控端)。Ansible安裝後,不用為它建立資料庫且不會在背景執行程式(daemons<a href="#s2"><sup>[2]</sup></a>)。只需要在一台電腦(control node)上安裝它,就可以管理整個被控端機器。由於不用在被控端機器上安裝或執行Ansible,因此不會有Ansible版本升級的問題。<p> <div id="s1">1. 非置入式自動化工具: 不需要在機器上安裝的自動化工具。</div> <div id="s2">2. daemon: 是一種在背景執行的程式,通常是負責某個服務 (service),以完成使用者或網路客戶端(client)對系統的請求任務。</div> <!-- </div> --> ## Prerequisites ![](https://i.imgur.com/TTCbowd.png) <!-- <div class="blockquote"> --> 只需要在一台電腦安裝Ansible作為control node,然後使用SSH(Ansible預設)和被控端機器(您要執行自動化任務的終端設備)進行通訊。 **- Control node的需求** 目前,Ansible可以在裝有Python 2 (2.7版)Python 3 (3.5版或更高版本)的任何機器(電腦、平板等)運行。作業系統可以是Red Hat、Debian、CentOS、macOS、任何BSD等作業系統,但尚未支援Windows作業系統,更多相關資訊請連結[Matt Davis的部落格文章](http://blog.rolpdog.com/2020/03/why-no-ansible-controller-for-windows.html)。 記住,選擇被控端機器附近的電腦作為control node去運行任何的管理系統會比在雲端上更好,請考慮從該雲端機房中的一台電腦上執行Ansible。因為在大部分的情況下,會比在開放的網際網路上執行Ansible更好。(資安考量) :::info :warning: **注意:** 由於Ansible 2.0版本開始使用更多檔案代碼<a href="#s3"><sup>[1]</sup></a>(file handle)管理它的子程序,而macOS預設分配檔案代碼的數量較少,所以如果要使用15個以上的子程序(fork),需要執行指令<span class="dark_orange">sudo launchctl limit maxfiles unlimited</span>增加ulimit值. 這個指令也可解決引起“Too many open files”相關的錯誤。</br></br> <div id="s3">1. 檔案代碼: 就是在檔案被開啟或建立時,系統會賦予該檔案一個暫時的號碼,該號碼就叫檔案代碼。系統會透過檔案代碼進行呼叫、存取、操作代表的檔案,而不使用檔名,直到檔案被關閉。 </div> ::: :::warning :warning: **提醒:** 有些模組和外掛程式需要額外的需求才能執行,需要在被控端機器上滿足這些模組的需求,而這些需求應寫在模組的說明文件內。 ::: **- Managed node的需求** 在被控端機器上,通常是透過SSH進行通訊。而SSH的檔案傳輸,Ansible預設是SFTP<a href="#s8"><sup>[1]</sup></a>。如果SFTP不可用,可以在ansible.cfg設定文件中切換為使用SCP<a href="#s9"><sup>[2]</sup></a>進行通訊。此外,還需要安裝Python 2(2.6版或更高版本)或Python 3(3.5版或更高版本)。 <div id="s8">1. SFTP: Secure File Transfer Protocol,一種使用SSH的加密技術的檔案傳輸協定。</div> <div id="s9">2. SCP: Secure Copy Protocol,也是一種使用SSH的加密技術的檔案傳輸協定。與SFTP最大的差別在於SCP不支援檔案傳輸中斷後再恢復續傳。</div> <br> :::info :warning: **注意:** - 如果在被控端機器上啟用SELinux,那麼您還需要在被控端機器上安裝libselinux-python,才可以使用Ansible的copy/file/template相關功能。你/妳可以透過Ansible的[yum模組](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html#yum-module)或[dnf模組](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/dnf_module.html#dnf-module)安裝libselinux-python在被控端機器。 - control node執行playbook文件中的第一個Python模組前,Ansible會嘗試尋找適合的Python直譯器直譯Python語言。也可以在hosts文件中設定inventory的[ansible_python_interpreter](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#ansible-python-interpreter)變數為哪個Python版本的直譯器。其他方法請連結[Interpreter Discovery](https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html#interpreter-discovery)。 - Ansible的[raw模組https://hackmd.io/](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/raw_module.html#raw-module)和[script模組](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html#script-module)不必依賴客戶端安裝的Python即可執行(會特地說明是因為Ansible是基於python開發的)。從技術上來講,可以透過Ansible使用[raw模組](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/raw_module.html#raw-module)安裝相容版本的Python或其他功能上。 例如: 如果你/妳需要在RHEL-based系統安裝Python 2,可使用下列指令: </br> ```bash= ansible myhost --become -m raw -a "yum install -y python2" ``` ::: <!-- </div> --> ## Selecting an Ansible Version to Install 根據您的需求選擇下列任一種方式安裝想要的Ansible版本: - 使用您作業系統的套件管理器安裝最新版的Ansible。(不同的作業系統所使用的套件管理器都不一樣) - 使用Python的套件管理器<span class="dark_orange">pip</span>安裝Ansible。 - 從原始碼安裝<span class="dark_orange">ansible-base</span>去體驗或測試開發中(<span class="dark_orange">devel</span>)版本的新功能。 :::info :warning: **注意:** 只有您想要修改原始碼或試用開發中的功能,才需要安裝開發中的Ansible(<span class="dark_orange">ansible-base</span>),因為開發中的版本變化很快且不穩定。 ::: Ansible每年會釋出2到3次新的版本,在這2到3次釋出的版本通常是修復一些次要的bug,且不會做backports<a href="#s6"><sup>[1]</sup></a>。而主要的bug在修復後,會以維護版本<a href="#s7"><sup>[2]</sup></a>的形式釋出,但不常見。 <div id="s6">1. backports: 是一種對舊版軟體進行維護的動作,主要動作是將新版軟體所做的修改或新增的功能移植到舊版軟體上,通常是用在安全性問題的修復上。</div> <div id="s7">2. maintenance releases: 維護版本,是指不新增功能或內容,只針對bug或安全性問題做修復然後釋出的版本。</div> ## Conclusion 本篇內容針對Ansible做簡單的介紹,跟著實作步驟一到四,您將會在Ubuntu Server 20.04的電腦(control node)完成安裝Ansible自動化工具、設定inventory檔案,並測試control node與被控端機器之間的連線,以利control node分派任務給被控端機器運行。 <style> .dark_orange { color: #FF8C00; background:#F6F6F6; border-radius:4px; padding-right:6px; padding-left:6px; } .sub_title { font-size: 25px; } .blockquote { background:#F6F6F6; } </style> ## Acknowledgements 1. [Ansible 2.9 Documentation](https://docs.ansible.com/ansible/2.9/installation_guide/intro_installation.html#prerequisites) 2. [Ansible Core 2.3 Documentation](https://docs.ansible.com/ansible/2.3/intro_installation.html) 3. [Ansible concepts](https://docs.ansible.com/ansible/2.9/user_guide/basic_concepts.html#inventory) 4. [hanovruslan/file.sh](https://gist.github.com/hanovruslan/86434f73c8672f069b38) 5. [How To Install and Configure Ansible on Ubuntu 20.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-ubuntu-20-04)