如何在Debian 10上安裝和使用Ansible
每當我們談論配置管理工具時,我們最常聽到的名字是Ansible。這是一個跨平台工具,旨在在Linux,macOS和Windows操作系統上工作時處理系統配置。今天,我將闡明在Debian 10上安裝Ansible的步驟。
在Debian 10上安裝Ansible
要在Debian 10上安裝Ansible,您需要執行三個簡單步驟:
步驟1:更新您的Debian 10系統。
在Debian 10上安裝Ansible之前,您需要使用以下命令更新Ansible。
sudo apt update
更新Debian 10系統後,您應該在終端中看到類似下圖的內容。
步驟2:在Debian 10系統上安裝Ansible。
系統更新完成後,您可以使用以下命令在Debian 10上安裝Ansible。
sudo apt install ansible
運行此命令時,系統將詢問您是否要在終端上繼續安裝。您必須輸入“ Y”以保持安裝過程的順利進行,如下圖所示。
在Debian 10系統上成功安裝Ansible時,終端將生成類似於下圖所示的消息。
步驟3:確認在您的Debian 10系統上安裝了Ansible。
在Debian 10系統上安裝Ansible非常容易,您可以通過以上兩個步驟完成它。但是,您仍然可以檢查它是否已成功安裝在Debian 10系統上。這可以通過使用以下命令檢查版本來完成:
ansible --version
在Debian 10系統上安裝的Ansible版本是2.7.7,並在下圖中突出顯示。
編輯Ansiblehosts文件 / etc / ansible /主機 添加要使用Ansible管理的系統。
sudo nano /etc/ansible/hosts
添加以下內容:
[TestClient] node1 ansible_ssh_host=192.168.0.12
And save the file.
Ansible的使用
首先,由於Ansible使用SSH協議將命令轉發到客戶端系統,因此您需要配置客戶端節點的SSH密鑰。
使用以下命令為基於密鑰的身份驗證生成SSH密鑰。
ssh-keygen
輸出:
Generating public/private rsa key pair. 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 /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:gTHiTCK....................... [email protected] The key's randomart image is: +---[RSA 2048]----+ | . . . | +----[SHA256]-----+
下一步是將新生成的密鑰複製到另一個系統。運行以下命令:
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
上面命令中的IP 192.168.0.2應該替換為Ansible管理的系統的IP地址。
輸出:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: Number of key(s) added: 1
現在是第一次測試的時候了。使用以下SSH命令登錄到另一台計算機。
ssh [email protected]
登錄無需密碼即可工作。
測試Ansible
安裝部分完成。您現在可以開始測試Ansible。
通過運行以下命令來測試連接:
ansible -m ping TestClient
輸出:
node1 | SUCCESS => { "changed": false, "ping": "pong" }
如果定義了多個客戶端,則可以使用以下命令測試所有連接:
ansible -m ping all
然後在遠程系統上運行命令並獲取結果。本示例使用df命令。
ansible -m shell -a 'df -h' TestClient
輸出:
node1 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on udev 957M 0 957M 0% /dev tmpfs 195M 21M 175M 11% /run /dev/sda1 38G 11G 25G 31% / tmpfs 974M 0 974M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 974M 0 974M 0% /sys/fs/cgroup /dev/sda15 121M 130K 120M 1% /boot/efi tmpfs 195M 0 195M 0% /run/user/0
結果顯示了遠程系統上的硬盤使用情況。 您可以使用ansible運行此類Linux Shell命令並創建命令集來設置和維護服務器。
刪除Ansible
要從Debian 10系統中刪除Ansible,必須首先使用以下命令卸載Ansible及其配置文件:
sudo apt-get purge ansible
成功執行此命令後,您還可以通過執行以下命令刪除所有不相關的程序包和依賴項。
sudo apt-get autoremove
結論
本文介紹了如何在Debian 10系統上安裝Ansible,以及如何使用Ansible遠程管理系統。
如何在Debian 10上安裝和使用Ansible