如何使用 Ansible 配置 IT 自動化管理
本文提供了對 Ansible 技術的基本瞭解以及安裝步驟。Ansible 是一款開源的 IT 自動化軟體,用於在客戶端或節點上配置、管理和安裝軟體,無需任何停機時間,也不需要在節點上安裝代理。它使用 SSH 與客戶端通訊。
目前,大多數 IT 自動化工具都在遠端主機上作為代理執行,但 Ansible 只需要 SSH 連線、使用者和 Python(2.4 或更高版本)。
環境設定詳情
Server Operating System: Centos 6.7 IP Address: 192.168.87.140 Host-name: ansible.hanuman.com User: root Remote Nodes Node 1: 192.168.87.156 Node 2: 192.168.87.157
安裝 Ansible 伺服器
對於基於 RPB 的克隆,沒有官方的 Ansible 儲存庫,但我們可以透過使用當前支援的 Fedora 發行版啟用 epel 儲存庫來安裝 Ansible,方法是在 RHEL/CentOS 6.X、7.X 上使用。
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm Output: Retrieving http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.no arch.rpm warning: /var/tmp/rpm-tmp.nHoRHj: Header V3 RSA/SHA256 Signature, key ID 0608b89 5: NOKEY Preparing... ########################################### [100%] package epel-release-6-8.noarch is installed
配置 epel 儲存庫後,您現在可以使用以下命令使用 yum 安裝 Ansible。
# sudo yum install ansible -y Output: Loaded plugins: fastestmirror, security Setting up Install Process Determining fastest mirrors epel/metalink | 4.3 kB 00:00 * base: centosmirror.go4hosting.in * epel: epel.mirror.net.in * extras: centosmirror.go4hosting.in * updates: centosmirror.go4hosting.in Resolving Dependencies . . . Installed: ansible.noarch 0:1.9.4-1.el6 Dependency Installed: PyYAML.x86_64 0:3.10-3.1.el6 libyaml.x86_64 0:0.1.3-4.el6_6 python-babel.noarch 0:0.9.4-5.1.el6 python-crypto2.6.x86_64 0:2.6.1-2.el6 python-httplib2.noarch 0:0.7.7-1.el6 python-jinja2.x86_64 0:2.2.1-2.el6_5 python-keyczar.noarch 0:0.71c-1.el6 python-pyasn1.noarch 0:0.0.12a-1.el6 python-simplejson.x86_64 0:2.0.9-3.1.el6 sshpass.x86_64 0:1.05-1.el6 Complete!
驗證安裝
配置 epel 儲存庫後,您現在可以使用以下命令使用 yum 安裝 Ansible。
# ansible --version ansible 1.9.4 configured module search path = None
準備 SSH 金鑰到遠端主機
要從 Ansible 伺服器執行任何部署或升級,每個主機都應該有一個使用者帳戶進行通訊。此外,我們需要將 ssh 金鑰從 Anisble 伺服器複製到遠端主機以實現無密碼連線。
首先,讓我們使用以下命令建立一個 SSH 金鑰,並將金鑰複製到遠端主機。
# ssh-keygen -t rsa -b 4096 -C "ansible.hanuman.com"
生成公鑰/私鑰 rsa 金鑰對
Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in ansible_key. Your public key has been saved in ansible_key.pub. The key fingerprint is: 28:ae:0c:8d:91:0a:fa:ac:2f:e2:8c:e5:fd:28:4b:c6 ansible.hanuman.com The key's randomart image is: +--[ RSA 4096]----+ | | | | | | | . . | |+ . . S | |+= . . | |= E . | |=X.o . | |=*Ooo.. | +-----------------+
成功建立 SSH 金鑰後,現在將建立的金鑰複製到所有兩個遠端伺服器,我們需要一個使用者在這裡進行 Ansible 演示,我使用 root 使用者,我們可以從中執行 Ansible 任務。
# ssh-copy-id root@192.168.87.156 Output: root@192.168.87.156's password: Now try logging into the machine, with "ssh 'root@192.168.87.156'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. # ssh-copy-id root@192.168.87.157 Output: root@192.168.87.157's password: Now try logging into the machine, with "ssh 'root@192.168.87.157'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
複製 SSH 金鑰到第二個遠端主機
將所有 SSH 金鑰複製到遠端主機後,現在在所有遠端主機上執行 ssh 金鑰身份驗證,以檢查身份驗證是否有效,執行以下命令進行測試。
# ssh root@192.168.87.156 [ansible@localhost ~]# Connection to 192.168.87.156 closed. # ssh root@192.168.87.157 [ansible@localhost ~]#
為遠端主機建立清單檔案
清單檔案,此檔案包含有關需要從本地連線到遠端的主機的資訊。預設配置檔案位於 /etc/ansible/hosts 下。
現在,我們將這兩個節點新增到配置檔案中。使用您喜歡的編輯器開啟並編輯檔案,這裡我們使用 vim。
# sudo vim /etc/ansible/hosts Add the following two hosts IP address.. [webservers] 192.168.87.156 192.168.87.157
注意 - [webservers] 中的括號表示組名,用於對節點進行分類和分組,以及控制何時以及為什麼進行控制。
測試 Ansible 是否正常工作
現在是時候檢查我們所有的伺服器了,只需從我們的 Ansible 伺服器執行 ping 操作即可。要執行此操作,我們需要使用命令“ansible”以及“-m”(模組)和“-all”(伺服器組)選項。
# ansible -m ping webservers Output: [root@localhost ~]# ansible -m ping webservers 192.168.87.157 | success >> { "changed": false, "ping": "pong" } 192.168.87.156 | success >> { "changed": false, "ping": "pong" }
或
# ansible -m ping -all Output: [root@localhost ~]# ansible -m ping webservers 192.168.87.157 | success >> { "changed": false, "ping": "pong" } 192.168.87.156 | success >> { "changed": false, "ping": "pong" }
現在,我們在這裡使用另一個名為“command”的模組,該模組用於一次性在所有選定的遠端主機上執行一系列 shell 命令(如 df、free、uptime 等)。為了演示,您可以執行以下命令。
檢查所有遠端主機上的分割槽
# ansible -m command -a "df -h" webservers Output: 192.168.87.156 | success | rc=0 >> Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 2.0G 15G 12% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 42M 411M 10% /boot 192.168.87.157 | success | rc=0 >> Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 2.0G 15G 12% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 42M 411M 10% /boot
檢查所有 Web 伺服器的記憶體使用情況
# ansible -m command -a "free -mt" webservers Output: 192.168.87.156 | success | rc=0 >> total used free shared buffers cached Mem: 981 528 453 0 39 322 -/+ buffers/cache: 166 815 Swap: 2047 0 2047 Total: 3029 528 2501 192.168.87.157 | success | rc=0 >> total used free shared buffers cached Mem: 981 526 455 0 39 322 -/+ buffers/cache: 164 817 Swap: 2047 0 2047 Total: 3029 526 2503
將輸出重定向到檔案
# ansible -m shell -a "service httpd status" webservers > service_status.txt Output: # cat service_status.txt 192.168.87.156 | FAILED | rc=3 >> httpd is stopped 192.168.87.157 | FAILED | rc=3 >> httpd is stopped
關閉遠端伺服器
#ansible -m shell -a "init 0" webservers OutPut: 192.168.87.157 | success | rc=0 >> 192.168.87.156 | success | rc=0 >>
Ansible 是一款強大的 IT 自動化工具,大多數 Linux 管理員都使用它來部署應用程式和一次性管理伺服器。在 Puppet、Chef 等其他任何自動化工具中,Ansible 非常有趣且易於配置,非常適合簡單的環境。