因为互联网的快速发展致使产品更新换代速度逐渐加快,运维人员天天都要进行大量的维护操做,仍旧按照传统方式进行维护使得工做效率低下。这时,部署自动化运维就能够尽量安全,高效地完成这些工做。
通常会把自动化运维工具划分为两类:一类是须要使用代理工具的,也就是基于专用的Agent程序来完成管理功能,如:Puppet、Func、Zabbix等;另一类是不须要配置代理工具的,能够直接基于SSH服务来完成管理功能,如:Ansible、Fabric等。 node
Puppet基于Ruby开发,支持Linux、UNIX、Windows平台,能够针对用户、系统服务、配置文件、软件包、软件包等进行管理,有很强的扩展性,但远程执行命令相对较弱。python
SaltStack基于Python开发,容许管理员对多个操做系统建立统一的管理系统,比Puppet更轻量级。mysql
Ansible基于Python开发,集合了众多优秀运维工具的优势,实现了批量运行命令、部署程序、配置系统等功能。默认经过SSH协议进行远程命令执行或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变得更加简单。可同时支持多台主机并行管理,使得管理主机更加便捷。web
工具 | 开发语言 | 结构 | 配置文件 | 运行任务 |
---|---|---|---|---|
Ansible | Python | 无 | YAML | 执行命令行 |
SaltStack | Python | C/S | YAML | 支持命令行 |
Puppet | Ruby | C/S | Ruby语法格式 | 经过模块实现 |
Ansible能够看做是一种基于模块进行工做的框架结构,批量部署能力就是由Ansible所运行的模块实现的。简而言之Ansible是基于“模块”完成各类“任务”的。其基本框架结构如图所示:sql
能够看出Ansible基本架构由六个部分构成:shell
Ansible自动化运维环境由控制主机与被管理主机组成,因为Ansible是基于SSH协议进行通讯的,因此控制主机安装Ansible软件后不须要重启或运行任何程序,被管理主机也不须要安装和运行任何代理程序。安全
角色 | 主机名 | IP地址 |
---|---|---|
控制主机 | node1 | 192.168.88.11 |
被管理主机 websrvs | node2 | 192.168.88.10 |
被管理主机 dbsrvs | node3 | 192.168.88.12 |
[root@localhost ~]# yum install epel-release -y //安装epel源 [root@localhost ~]# yum install ansible -y //安装Ansible [root@localhost ~]# ansible --version //查看版本信息 ansible 2.6.2 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, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] [root@localhost ansible]# cd /etc/ansible/ [root@localhost ansible]# ll 总用量 24 -rw-r--r--. 1 root root 19549 7月 29 04:07 ansible.cfg //配置文件 -rw-r--r--. 1 root root 1016 7月 29 04:07 hosts //管控主机文件 drwxr-xr-x. 2 root root 6 7月 29 04:07 roles
hostfile = /etc/ansible/hosts //hosts文件的位置 library = /usr/share/ansible //ansible默认搜寻模块的位置 pattern = * //若是没有提供hosts节点,这是playbook要通讯的默认主机组.默认值是对全部主机通讯 remote_tmp = $HOME/.ansible/tmp //Ansible经过远程传输模块到远程主机,而后远程执行,执行后在清理现场.在有些场景下,你也许想使用默认路径但愿像更换补丁同样使用 forks = 5 //在与主机通讯时的默认并行进程数 ,默认是5d poll_interval = 15 //当具体的poll interval 没有定义时,多少时间回查一下这些任务的状态, 默认值是5秒 sudo_user = root //sudo使用的默认用户 ,默认是root #ask_sudo_pass = True //用来控制Ansible playbook 在执行sudo以前是否询问sudo密码.默认为no #ask_pass = True //控制Ansible playbook 是否会自动默认弹出密码 transport = smart //通讯机制.默认 值为’smart’。若是本地系统支持 ControlPersist技术的话,将会使用(基于OpenSSH)‘ssh’,若是不支持将使用‘paramiko’.其余传输选项‘local’,‘chroot’,’jail’等等 #remote_port = 22 //远程SSH端口。 默认是22 module_lang = C //模块和系统之间通讯的计算机语言,默认是C语言 #host_key_checking = False //检查主机密钥 timeout = 10 //SSH超时时间 #log_path = /var/log/ansible.log //日志文件存放路径 #module_name = command //ansible命令执行默认的模块 #private_key_file = /path/to/file //私钥文件存储位置
ansible_ssh_host //指定主机别名对应的真实 IP ansible_ssh_port //指定链接到这个主机的 ssh 端口,默认 22 ansible_ssh_user //指定链接到该主机上的用户 ansible_sudo_pass //sudo 密码 ansible_sudo_exe //sudo 命令路径 ansible_connection //链接类型,能够是 local、ssh 或paramiko,ansible1.2 以前默认为 paramiko ansible_ssh_private_key_file //私钥文件路径 ansible_shell_type // 目标系统的 shell 类型,默认为sh
Ansible经过读取默认主机清单/etc/ansible/hosts文件,修改主机与组配置后,可同时链接到多个被管理主机上执行任务。好比定义一个websrvs组,包含两台主机的IP地址,再定义一个dbsrvs组,包含一个主机的IP地址,内容以下:架构
[root@localhost ansible]# vi hosts ... [webserver] 192.168.88.10 [mysql] 192.168.88.12
[root@localhost ansible]# ssh-keygen -t rsa //基于SSH秘钥的链接 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): //直接Enter使用默认路径 Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): //直接Enter不加密 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:NGsx7HoUljEOpgUU6Fp1vay+aDkgHO2AHhtedcrXoD8 root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | o+o+.o | | . .=.*.+ | |....+.+.&. | |.=oo + =oB | |+oO o.S | |o=.. .E | | . . o. o | | +... | | ..... | +----[SHA256]-----+ [root@localhost ansible]# ssh-copy-id root@192.168.88.10 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.88.10 (192.168.88.10)' can't be established. ECDSA key fingerprint is SHA256:LVQy8BE9xArhgdx5buiZoCIhYKAzoTkl7SPX6geEFdk. ECDSA key fingerprint is MD5:af:47:6d:64:8f:0f:e6:25:7f:7b:d1:10:a5:31:83:29. Are you sure you want to continue connecting (yes/no)? y Please type 'yes' or 'no': yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.88.10's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.88.10'" and check to make sure that only the key(s) you wanted were added. [root@localhost ansible]# ssh-copy-id root@192.168.88.12 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.88.12 (192.168.88.12)' can't be established. ECDSA key fingerprint is SHA256:/E8PrN6v7MJRRcWbmU0mXwELY+yABUuNTyDiTl7O2lU. ECDSA key fingerprint is MD5:1e:f1:8e:93:d6:65:1d:fd:1d:ff:71:15:a4:6c:2f:4e. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.88.12's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.88.12'" and check to make sure that only the key(s) you wanted were added.
至此一个简单的Ansible环境就部署成功了!框架