我们先看原始的运维时代,也就是手动运维的时候,我们如何去安装一个软件
在以前,运维需要规范,需要统一配置管理,我们只能使用源码安装方式,便于我们去管理,源码安装,如果是单台还好,一会也就装完了,如果此时,生产环境压力骤增,需要扩展100台web节点(源码安装100台nginx)我们该如何操作?
下面是我们之前安装nginx的一个脚本:
| |
| yum install pcre-devel openssl-devel -y |
| |
| cd /usr/local/tools |
| |
| useradd nginx -M -s /sbin/nologin |
| |
| tar xf nginx-1.6.3.tar.gz |
| |
| cd nginx-1.6.3 |
| |
| ./configure --prefix=/usr/local/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module |
| |
| make |
| |
| make install |
| |
| ln -s /usr/local/nginx-1.6.3/ /usr/local/nginx |
很明显,如果我们装一台也还好,那如果装100台服务器,那我们是不是要重复执行100次
以上步骤还只是安装,那还要部署代码呢?修改配置文件?优化?启动?那还工作个毛线啊,一天啥也不用干了,就装nginx吧
正因为有了上面非常麻烦的操作,因此我们在想,如果能有一个批量化操作上述脚本或者批量化执行命令的软件就非常好了
因此,就出现了以Ansible为代表的一系列自动化运维工具,从此我们的运维工作就会变得非常的简单和方便
Ansible是一个自动化统一配置管理工具,自动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率
1.远程执行
批量执行远程命令,可以对多台主机进行远程操作
2.配置管理
批量配置软件服务,可以进行自动化方式配置,服务的统一配置管理,和启停
3.事件驱动
通过Ansible的模块,对服务进行不同的事件驱动
比如:
1)修改配置后重启
2)只修改配置文件,不重启
3)修改配置文件后,重新加载
4)远程启停服务管理
4.管理公有云
通过API接口的方式管理公有云,不过这方面做的不如saltstack.
saltstack本身可以通过saltcloud管理各大云厂商的云平台。
5.二次开发
因为语法是Python,所以便于运维进行二次开发。
6.任务编排
可以通过playbook的方式来统一管理服务,并且可以使用一条命令,实现一套架构的部署
7.跨平台,跨系统
几乎不受到平台和系统的限制,比如安装apache和启动服务
1、连接插件connection plugins用于连接主机 用来连接被管理端
2、核心模块core modules连接主机实现操作, 它依赖于具体的模块来做具体的事情
3、自定义模块custom modules根据自己的需求编写具体的模块
4、插件plugins完成模块功能的补充
5、剧本playbookansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
6、主机清单inventor定义ansible需要操作主机的范围
最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块
1.Ansible读取playbook剧本,剧本中会记录对哪些主机执行哪些任务
2.首先Ansible通过主机清单找到要执行的主机,然后调用具体的模块
3.其次Ansible会通过连接插件连接对应的主机并推送对应的任务列表
4.最后被管理的主机会将Ansible发送过来的任务解析为本地Shell命令执行
环境准备
主机名 |
IP |
角色 |
m01 |
10.0.0.61 |
Ansible控制端 |
web01 |
10.0.0.7 |
Ansible被控端 |
web02 |
10.0.0.8 |
Ansible被控端 |
安装Ansible
| |
| [root@m01 ~] |
| |
| |
| [root@m01 ~] |
| |
| |
| --version |
| -v |
| -i |
| -m |
| -a |
| -k |
| -C |
| -T |
| |
| |
| [root@m01 ~] |
| ansible 2.7.1 |
| config file = /etc/ansible/ansible.cfg |
| configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] |
| ansible python module location = /usr/lib/python2.7/site-packages/ansible |
| executable location = /usr/bin/ansible |
| python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] |
Ansible配置文件介绍
| [root@m01 ~] |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| host_key_checking = False |
| log_path = /var/log/ansible.log |
| |
| |
| [privilege_escalation] |
| |
| |
| |
| |
/etc/ansible/hosts是ansible默认主机资产清单文件,用于定义被管理主机的认证信息, 例如ssh登录用户名、密码以及key相关信息。Inventory文件中填写需要被管理的主机与主机组信息。还可以自定义Inventory主机清单的位置,使用-i指定文件位置即可
场景一
| [root@m01 ~] |
| |
| |
| [webs] |
| 10.0.0.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' |
| 10.0.0.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' |
| |
| |
| [webs] |
| web0[1:2] ansible_ssh_pass='123456' |
| |
| |
| [webs] |
| web0[1:2] |
| [webs:vars] |
| ansible_ssh_pass='123456' |
| |
| [root@m01 ~] |
| 10.0.0.8 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| 10.0.0.7 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
场景二,基于公钥和私钥的连接
| |
| [root@m01 ~] |
| |
| |
| [root@m01 ~] |
| [root@m01 ~] |
| [root@m01 ~] |
| |
| [webs] |
| 10.0.0.7:22 |
| 10.0.0.8 |
| |
| [root@m01 ~] |
| 10.0.0.8 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| 10.0.0.7 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| |
| |
| [root@m01 ~] |
| [webs] |
| web01 ansible_ssh_host=10.0.0.7 ansible_ssh_port=22 |
| web02 ansible_ssh_host=10.0.0.8 |
| |
| [root@m01 ~] |
| web02 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| web01 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |