標籤:
Ansible是一款為類Unix系統開發的自由開源的配置和自動化工具。它用Python寫成,類似於Chef和Puppet,但是有一個不同和優點是我們不需要在節點中安裝任何用戶端。它使用SSH來和節點進行通訊。
本篇中我們將在CentOS 7上安裝並配置Ansible,並且嘗試管理兩個節點。
Ansible 服務端 – ansible.linuxtechi.com ( 192.168.1.15 )
節點 – 192.168.1.9 , 192.168.1.10
第一步: 設定EPEL倉庫
Ansible倉庫預設不在yum倉庫中,因此我們需要使用下面的命令啟用epel倉庫。
[[email protected] ~]# rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
第二步: 使用yum安裝Ansible
[[email protected] ~]# yum install ansible
安裝完成後,檢查ansible版本:
[[email protected] ~]# ansible --version
第三步: 設定用於節點鑒權的SSH密鑰
在Ansible服務端產生密鑰,並且複製公開金鑰到節點中。
[email protected] ~]# ssh-keygen
使用ssh-copy-id命令來複製Ansible公開金鑰到節點中。
第四步:為Ansible定義節點的清單
檔案 /etc/ansible/hosts 維護著Ansible中伺服器的清單。
[[email protected] ~]# vi /etc/ansible/hosts
[test-servers]
192.168.1.9
192.168.1.10
儲存並退出檔案。
第五步:嘗試在Ansible服務端運行命令
使用ping檢查‘test-servers’或者ansible節點的連通性。
[[email protected] ~]# ansible -m ping ‘test-servers‘
執行shell命令
1:檢查Ansible節點的已耗用時間(uptime)
[[email protected] ~]# ansible -m command -a "uptime" ‘test-servers‘
2:檢查節點的核心版本
[[email protected] ~]# ansible -m command -a "uname -r" ‘test-servers‘
3:給節點增加使用者
[[email protected] ~]# ansible -m command -a "useradd mark" ‘test-servers‘
[[email protected] ~]# ansible -m command -a "grep mark /etc/passwd" ‘test-servers‘
4:重新導向輸出到檔案中
[[email protected] ~]# ansible -m command -a "df -Th" ‘test-servers‘ > /tmp/command-output.txt
免費領取兄弟連IT教育原創linux營運工程師視頻/細說linux教程,詳情諮詢官網客服:http://www.itxdl.cn/linux/
或者勾搭Q2430675018
歡迎加入linux交流群 478068715
在 CentOS 7 中安裝並使用自動化工具 Ansible