ansible是个啥?

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

2、架构图如下:

这个架构图网上很多人都是在使用,顺便。。用下

ansible介绍以及安装_第1张图片


3、特性

(1)、no agents:不需要在被管控主机上安装任何客户端;

(2)、no server:无服务器端,使用时直接运行命令即可;

(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;

(4)、yaml,not code:使用yaml语言定制剧本playbook;

(5)、ssh by default:基于SSH工作;

(6)、strong multi-tier solution:可实现多级指挥。


4、优点
(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。


5、执行过程、、


ansible介绍以及安装_第2张图片


注:以上大部分都是在网上其他分享或者我在别的网站拷贝个别部分的。


ansible安装可以源码或者使用第三方源安装。。这里我使用第三方源epel

操作系统环境:

[root@ansible ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
[root@ansible ~]# uname -r
2.6.32-358.el6.x86_64
[root@ansible ~]# uname -m
x86_64


下载epel源以及安装。

[root@ansible ~]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
--2016-04-29 02:13:01--  http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
Resolving dl.fedoraproject.org... 209.132.181.25, 209.132.181.26, 209.132.181.27, ...
Connecting to dl.fedoraproject.org|209.132.181.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14540 (14K) [application/x-rpm]
Saving to: “epel-release-latest-6.noarch.rpm”
100%[======================================================================================>] 14,540      62.5K/s   in 0.2s    
2016-04-29 02:13:01 (62.5 KB/s) - “epel-release-latest-6.noarch.rpm” saved [14540/14540]
[root@ansible ~]# rpm -ivh epel-release-latest-6.noarch.rpm 
warning: epel-release-latest-6.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

注:使用第三方epel源的时候提示错误或者其他的,可能需要修改下epel.repo文件。吧#baseurl改成baseurl,吧mirrorlist改成#mirrorlist,然后yum clean all即可


安装ansible

[root@ansible ~]# yum install ansible
................................
................................
Installing:
 ansible                              noarch                    2.0.1.0-2.el6                     epel                    2.9 M
Installing for dependencies:
 PyYAML                               x86_64                    3.10-3.1.el6                      base                    157 k
 libyaml                              x86_64                    0.1.3-4.el6_6                     base                     52 k
 python-babel                         noarch                    0.9.4-5.1.el6                     base                    1.4 M
 python-crypto                        x86_64                    2.0.1-22.el6                      base                    159 k
 python-crypto2.6                     x86_64                    2.6.1-2.el6                       epel                    513 k
 python-httplib2                      noarch                    0.7.7-1.el6                       epel                     70 k
 python-jinja2-26                     noarch                    2.6-3.el6                         epel                    527 k
 python-keyczar                       noarch                    0.71c-1.el6                       epel                    219 k
 python-markupsafe                    x86_64                    0.9.2-4.el6                       base                     22 k
 python-paramiko                      noarch                    1.7.5-2.1.el6                     base                    728 k
 python-pyasn1                        noarch                    0.0.12a-1.el6                     base                     70 k
 python-setuptools                    noarch                    0.6.10-3.el6                      base                    336 k
 python-simplejson                    x86_64                    2.0.9-3.1.el6                     base                    126 k
 python-six                           noarch                    1.9.0-2.el6                       base                     28 k
 sshpass                              x86_64                    1.05-1.el6                        epel                     19 k
Transaction Summary
================================================================================================================================
Install      16 Package(s)
Total download size: 7.3 M
Installed size: 35 M
Is this ok [y/N]: y
...........................................
Dependency Installed:
  PyYAML.x86_64 0:3.10-3.1.el6               libyaml.x86_64 0:0.1.3-4.el6_6          python-babel.noarch 0:0.9.4-5.1.el6      
  python-crypto.x86_64 0:2.0.1-22.el6        python-crypto2.6.x86_64 0:2.6.1-2.el6   python-httplib2.noarch 0:0.7.7-1.el6     
  python-jinja2-26.noarch 0:2.6-3.el6        python-keyczar.noarch 0:0.71c-1.el6     python-markupsafe.x86_64 0:0.9.2-4.el6   
  python-paramiko.noarch 0:1.7.5-2.1.el6     python-pyasn1.noarch 0:0.0.12a-1.el6    python-setuptools.noarch 0:0.6.10-3.el6  
  python-simplejson.x86_64 0:2.0.9-3.1.el6   python-six.noarch 0:1.9.0-2.el6         sshpass.x86_64 0:1.05-1.el6              
Complete!

注:以上就ansible的安装



ansible的配置文件

[root@ansible ~]# tree /etc/ansible/

/etc/ansible/

├── ansible.cfg  //主配置文化

├── hosts    //主机清单

└── role    //角色


下面就使用下ansible的东东。。默认是ssh方式。。所以我就在ansible主机上生成key文件。然后直接ssh-copy-id了。。你懂。免密钥,配置密码登陆,需要ansible本机安装sshpass,你直接在ansible的hosts文件直接配置类似这样的

192.168.33.132 ansible_ssh_user=xxx ansible_ssh_pass=xx ansible_ssh_port=521


激动的时刻,运行ansible命令,all指的所有主机,也可以简写a,-m指的是模块而ping模块名称。类似saltstack的test.ping


[root@ansible ~]# ansible all -m ping
db | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.33.132 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
web | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

更多ansible相关命令使用ansible --help 来查看使用吧


以上就是ansible简单命令。。先这样吧