Ansible一種集成IT系統(tǒng)的配置管理,應(yīng)用部署,執(zhí)行特定任務(wù)的開源平臺。Ansible具有如下特點(diǎn):
部署簡單,只需在主控制端部署Ansible環(huán)境,被控端無需做任何操作;
默認(rèn)使用SSH協(xié)議對設(shè)備進(jìn)行管理;
主從集中化管理;
配置簡單,功能強(qiáng)大,擴(kuò)展性強(qiáng)
通過Playbooks來定制強(qiáng)大的配置、狀態(tài)管理;
一、Ansible安裝
1.環(huán)境配置
| 角色 | 主機(jī)名 | IP | 組名 |
| msster | Automation | 192.168.1.23 | |
| cleint | test1 | 192.168.1.24 | webservers |
| client | test2 | 192.168.1.76 | webservers |
2.安裝ansible,在master上執(zhí)行以下操作
EPEL已經(jīng)提供了ansible所需的所有支持軟件包,所以在這里使用epel源進(jìn)行安裝:
3.ansible配置及測試
第一步修改主機(jī)與組配置,文件位置/etc/ansible/hosts,格式為ini,添加兩臺主機(jī)Ip,同時(shí)定義兩個(gè)ip到webservers組,內(nèi)如如下:
通過ping模塊測試主機(jī)的連通性,分別對單主機(jī)及組進(jìn)行ping操作,出現(xiàn)如下圖所示表示測試成功

注意:由于主控端與被控主機(jī)未配置SSH證書信任,需要在執(zhí)行ansible命令時(shí)添加-k參數(shù),要求提供root默認(rèn)密碼,
4.配置Linux主機(jī)SSH無密碼訪問
在主控端主機(jī)192.168.1.23創(chuàng)建密鑰,執(zhí)行ssh-keygen -t rsa ,有詢問直接按回車鍵即可,將在/root/.ssh/下生成一對密鑰,其中id_rsa為私鑰,id_rsa.pub為公鑰
[root@Automation ~]# ssh-keygen -t rsaGenerating 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:8c:b2:59:d6:39:14:92:7f:95:98:3d:98:55:36:aa:bd root@AutomationThe key's randomart image is:+--[ RSA 2048]----+| ... B.o+ || .. .= =o . || .. ... || =...o || . + S.. . || * . . || o E || || |+-----------------+
接下來同步公鑰id_rsa.pub到目標(biāo)主機(jī),推薦使用ssh-copy-id 公鑰拷貝工具
檢驗(yàn)SSH無密碼配置是否成功,運(yùn)行ssh root@192.168.1.24,直接進(jìn)入目標(biāo)root賬戶提示符,說明配置成功.
5.定義主機(jī)與組規(guī)則
ansible通過定義好的主機(jī)與組規(guī)則對匹配的目標(biāo)主機(jī)進(jìn)行遠(yuǎn)程操作,配置規(guī)則文件默認(rèn)是/etc/ansible/hosts。
6.定義主機(jī)與組
所有定義的主機(jī)與組規(guī)則都在/etc/ansible/hosts文件中,為ini文件格式,主機(jī)可以用域名、IP、別名進(jìn)行標(biāo)識,其中webservers,dbservers為組名,緊跟著主機(jī)為其成員
192.168.1.21:29050 [webservers]192.168.1.23 [dbservers]192.168.1.76
其中,192.168.1.21:29050的意思是定義一個(gè)SSH服務(wù)器端口29050的主機(jī),當(dāng)然我們也可以使用別名來描述一臺主機(jī),如
web1 ansible_ssh_port=22 ansible_ssh_host = 192.168.1.34
web1為定義的一個(gè)別名,ansible_ssh_port為主機(jī)SSH服務(wù)端口,ansible_ssh_host為目標(biāo)主機(jī)
新聞熱點(diǎn)
疑難解答
圖片精選