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通信使用 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 //添加



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 root@ansible 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 root@192.168.252.173 ssh-copy-id root@192.168.252.174 //将公钥推送给被管理端 ssh-agent bash //免交互 ssh-add 至此ansible安装以及主机清单配置完成。(注:如果连接失败请检查ansibleserver和被管理端是否能ping通,如能ping通,可以删除.ssh下的密钥,重新生成并下发)