1. 程式人生 > >ansible在centos7上的安裝和配置

ansible在centos7上的安裝和配置

1.Ansible簡介:ansible基於python開發,集合了眾多優秀運維工具的優點,實現了批量執行命令、部署程式、配置系統等功能。預設通過SSH協議進行遠端命令執行或下發配置,無需部署任何客戶端代理軟體,從而使得自動化環境部署變得更加簡單。可同時支援多臺主機並進行管理,使得管理主機更加便捷。主版本大概每2個月釋出一次。
2.核心元件說明:
Ansible:Ansible的核心程式
Host Lnventory:記錄了每一個由Ansible管理的主機資訊,資訊包括ssh埠,root帳號密碼,ip地址等等。可以通過file來載入,可以通過CMDB載入
Playbooks:YAML格式檔案,多個任務定義在一個檔案中,使用時可以統一呼叫,“劇本”用來定義那些主機需要呼叫那些模組來完成的功能.
Core Modules:Ansible執行任何管理任務都不是由Ansible自己完成,而是由核心模組完成;Ansible管理主機之前,先呼叫core Modules中的模組,然後指明管理Host Lnventory中的主機,就可以完成管理主機。
Custom Modules:自定義模組,完成Ansible核心模組無法完成的功能,此模組支援任何語言編寫。
Connection Plugins:連線外掛,Ansible和Host通訊使用
ansible在centos7上的安裝和配置


3.安裝和配置
實驗環境:ansibleserver(IP:192.168.252.130 centos7)
Mysqlserver(IP:192.168.252.173 centos7)
Webserver(IP:192.168.252.174 centos7)
ansibleserver端
1)安裝ansible並配置主機清單
yum install -y epel-release //安裝環境軟體包
yum install -y ansible //安裝ansible
vim /etc/ansible/hosts //配置主機清單,新增被管理的主機IP



#This is the default ansible 'hosts' file.

#It should live in /etc/ansible/hosts

#- Comments begin with the '#' character
#- Blank lines are ignored
#- Groups of hosts are delimited by [header] elements
#- You can enter hostnames or ip addresses
#- A hostname/ip can be a member of multiple groups

#Ex 1: Ungrouped hosts, specify before any group headers.

##green.example.com
##blue.example.com
##192.168.100.1
##192.168.100.10

#Ex 2: A collection of hosts belonging to the 'webservers' group

#[webservers]
##alpha.example.org
##beta.example.org
##192.168.1.100
##192.168.1.110
[webserver] //新增
192.168.252.173 //新增
[mysql] //新增
192.168.252.174 //新增



ansible在centos7上的安裝和配置
2)生成金鑰對並將公鑰推送給被管理端
ssh-keygen -t rsa //以rsa的型別生成金鑰對
enerating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //回車
Created directory '/root/.ssh'.
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:udpIx9U0dKSLCND1RFwGy3PK/ZdczW1fep2AoaO3GGQ [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| .. ..o+o+.o |
| .. +.+ o |
| . =.= |
| . +.Ooo .o|
| ESo=.+. B|
| o..o. .ooB|
| .o+. o+=|
| . =+ . o |
| o... |
+----[SHA256]-----+
cd ~
cd .ssh
ssh-copy-id [email protected]
ssh-copy-id [email protected] //將公鑰推送給被管理端
ssh-agent bash //免互動
ssh-add
ansible在centos7上的安裝和配置
至此ansible安裝以及主機清單配置完成。(注:如果連線失敗請檢查ansibleserver和被管理端是否能ping通,如能ping通,可以刪除.ssh下的金鑰,重新生成並下發)