在 CentOS 7 中安裝並使用自動化工具 Ansible

來源:互聯網
上載者:User

標籤:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.