您的位置:首页 > 运维架构 > Linux

基于Ansible实现批量管理 推荐

2014-04-30 22:21 323 查看
基于Ansible实现批量管理
Ansible提供一种最简单的方式用于发布、管理和编排计算机系统的工具,可在数分钟内搞定,Ansible本身是一种python程序,只需要配置一个节点,只要ssh连接ok 被控制节点不需要安装任何工具,就可以实现远程连接执行任何操作支持多节点发布、远程任务执行。
如果想熟练使用Ansible 还需了解Ansible的模块机制,下面我们来介绍ansible的日常使用,从而达到我们批量管理的目的

安装ansible
只需要将管理节点安装ansible ,被管理节点不需要安装
但如果使用yum安装的话,必须配置epel源
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm -import/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6安装依赖关系包
yum installpython-dev python-yamlpython-paramiko python-jinja2 git 确保无误,使用yum安装absible
yum install ansibleansbible配置文件
安装后的配置文件位于/etc/ansible
[root@node1ansible]# pwd/etc/ansible[root@node1ansible]# ll
total 20
-rw-r--r--. 1 root root 5507 May 1 17:53 ansible.cfg #主配置文件
-rw-r--r--. 1 root root 965 Mar 15 13:07 hosts #目标主机目标被管理主机要定义在host中,否则只能一个一个去手写了
查看配置文件ansible.cfg常用参数:
hostfile = /etc/ansible/hosts #默认目标主机文件
library = /usr/share/ansible #库文件,装载模块时候其模块装载的路径,因为ansible是模块化的工具module_name =command #模块的名称,意思是没有使用指定模块的时候其默认使用的模块,默认是命令,意为只执行命令forks = 5 #启动的子进程remote_port = 22 #被管理主机的端口,默认为22,如果ssh端口有变动则需要修改此参数sudo_user = root #如果以普通用户登录进行sudo那么sudo的默认账户是root
host配置文件
一般按照安全性要求,应该禁止 root 登录,禁用密码登录,一律使用证书登录。 ansible 支持通过 sudo 来执行
所有管理主机,每个被管理主机都可以使用主机名或者ip地址都可以
使用中括号可以组,紧跟着中括号下面的都是同一组内被管理的主机
[root@node1ansible]# grep -v "^#" hosts | grep -v "^$"
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
www[001:006].example.com #定义组的时候还可以使用通配符
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
db-[99:101]-node.example.com使用absible批量管理节点
假如这个配置文件不符合我们的意向,可以cp一份作为参考模板,方便以后借鉴
[root@node1ansible]# cp hosts hosts.bak这里我们定义2台被管理节点
#使用
[]
指定分组
[root@node1ansible]# grep -v '#' hosts
[hac]
10.0.10.62
10.0.10.6110.0.10.60要基于ssh工作,所以本地要生成一对ssh秘钥,然后让其基于秘钥认证方式来管理节点
创建秘钥
[root@node1ansible]# ssh-keygen -t rsa -P ''传送至被管理控节点上去
ssh-copy-id-i .ssh/id_rsa.pub root@10.0.10.60
ssh-copy-id -i .ssh/id_rsa.pub root@10.0.10.62
ssh-copy-id -i .ssh/id_rsa.pub root@10.0.10.61 #因为本地也是被监控节点之一尝试ansible是否可用
先来man一下帮助手册
ANSIBLE(1) System administration commands ANSIBLE(1)

NAME
ansible - run acommand somewhere else

SYNOPSIS #使用格式 ansible <host-pattern> [-f forks] [-mmodule_name] [-a args]主机模式,可以使用通配
-f forks 表示我们可以明确指明启动多少个子进程去连接这些主机默认不定义为5个forks,如果节点比较多,可以将forks参数调大-m 指定使用的模块,每个模块都需要接受特定参数-a 来指定使用参数比如我们想让所有主机显示当前系统时间
[root@node1 ~]#ansible all -m command -a 'date'
10.0.10.61 | success | rc=0 >>
Thu May 1 18:49:18 CST 2014

10.0.10.60 | success | rc=0 >>
Mon Apr 28 22:17:58 CST 2014

10.0.10.62 | success | rc=0 >>
Thu May 1 02:49:58 CST 2014-m command 为指定模块,默认为command,如果默认执行命令的话,可以不加这个模块,如下所示
[root@node1 ~]#ansible all -a 'date'
10.0.10.60 | success | rc=0 >>
Mon Apr 28 22:18:06 CST 2014

10.0.10.62 | success | rc=0 >>
Thu May 1 02:50:05 CST 2014

10.0.10.61 | success | rc=0 >>
Thu May 1 18:49:27 CST 2014可以看到,时间不同步,但是命令是同时进行的,因为会发起N个子进程分别与每个被监控节点进行交互的
如果节点比较多,可以将forks参数调大

现在期望多个节点自动的去ntp服务器同步时间
[root@node1 ~]#ansible all -a 'crontab -l'
10.0.10.60 | FAILED | rc=1 >>
no crontab for root

10.0.10.62 | success | rc=0 >>
00 * * * * ntpdate time.windows.com

10.0.10.61 | FAILED | rc=1 >>
no crontab for root可以看到只有一个节点存在,那么我们只要同步其余两台机器即可
定义单独组
[root@node1ansible]# cat hosts[hac]
10.0.10.62
10.0.10.61
10.0.10.60

[crontab]
10.0.10.61
10.0.10.60首先先来手动同步
[root@node1ansible]# ansible crontab -a 'ntpdate time.windows.com'
10.0.10.60 | success | rc=0 >>
30 Apr 18:57:02 ntpdate[2079]: step time server 65.55.56.206 offset160320.206233 sec

10.0.10.61 | success | rc=0 >>
30 Apr 18:57:02 ntpdate[3619]: step time server 65.55.56.206 offset-86361.207014 sec将时间同步加入计划任务,我们可以使用命令追加的方式来写入计划任务
[root@node1ansible]# ansible crontab -a 'echo '00 * * * * * ' >>/var/spool/cron/root'这样一来可以实现,但是实在是太麻烦了,为此,ansible有专门计划任务管理的模块,可以使用内置模块来实现以最简单的命令方式来轻松批量管理
定义crontab的参数有很多,可以在官方去查找帮助文档,这里不再一一介绍

明确指定crontab组,-m指定cron模块 -a指定参数
[root@node1 ansible]# ansible crontab -m cron-a 'name="sync time from time.windows.com" minute='*/3'job="/usr/sbin/ntpdate time.windows.com" '
10.0.10.60 | success >> {
"changed": true,
"jobs": [
"sync time fromtime.windows.com"
]
}

10.0.10.61 | success >> {
"changed": true,
"jobs": [
"sync time fromtime.windows.com"
]
}

[root@node1 ansible]# ansible all -a 'crontab -l'
10.0.10.60 | success | rc=0 >>
#Ansible: sync time from time.windows.com
*/3 * * * * /usr/sbin/ntpdate time.windows.com

10.0.10.62 | success | rc=0 >>
00 * * * * ntpdate time.windows.com

10.0.10.61 | success | rc=0 >>
#Ansible: sync time from time.windows.com
*/3 * * * * /usr/sbin/ntpdate time.windows.com参数说明:
name=sync time from time.windows" 这是任务的名称注释会写到crontab里面去,也可以不指定,效果如下所示
[root@node1ansible]# crontab -l#Ansible:sync time from time.windows.com #因此上面多了一个注释信息*/3 * * * */usr/sbin/ntpdate time.windows.comminute='*/3 我们这里只需要关注分钟,只需要提供分钟的定义,省去的统统默认为*
job="/usr/sbin/ntpdate time.windows.com" 此为我们定义的计划任务执行的命令
使用ansible批量安装程序
我们工作场景中,如果想安装程序的话,只要我们的yum源无问题,那么则可以完全使用yum来安装
那我们来man一下其模块
man的使用方法比较独特如果想man查看ansible模块的话必须加符号.
[root@node1ansible]# man ansible.yum安装vsftpd服务
[root@node1ansible]# ansible crontab -m yum -a'name=vsftpd state=present'state=present 状态判断其是安装还是卸载的,这里也可以使用install
[root@node1ansible]# ansible crontab -m yum -a'name=vsftpd state=present'
10.0.10.61 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror,refresh-packagekit, security\nLoading mirror speeds from cached hostfile\n *base: mirror.neu.edu.cn\n * epel: ftp6.sjtu.edu.cn\n * extras: mirror.neu.edu.cn\n* updates: mirror.neu.edu.cn\nSetting up Install Process\nResolvingDependencies\n--> Running transaction check\n---> Package vsftpd.x86_640:2.2.2-11.el6_4.1 will be installed\n--> Finished DependencyResolution\n\nDependencies Resolved\n\n================================================================================\nPackage Arch Version Repository Size\n================================================================================\nInstalling:\nvsftpd x86_64 2.2.2-11.el6_4.1 base 151 k\n\nTransactionSummary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 151 k\nInstalled size: 331 k\nDownloadingPackages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction TestSucceeded\nRunning Transaction\n\r Installing :vsftpd-2.2.2-11.el6_4.1.x86_64 1/1 \n\r Verifying : vsftpd-2.2.2-11.el6_4.1.x86_64 1/1 \n\nInstalled:\n vsftpd.x86_64 0:2.2.2-11.el6_4.1 \n\nComplete!\n"
]
}
10.0.10.60 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror,security\nLoading mirror speeds from cached hostfile\n * base:mirrors.btte.net\n * extras: mirrors.btte.net\n * updates:mirrors.btte.net\nSetting up Install Process\nResolving Dependencies\n-->Running transaction check\n---> Package vsftpd.x86_64 0:2.2.2-11.el6_4.1will be installed\n--> Finished Dependency Resolution\n\nDependenciesResolved\n\n================================================================================\nPackage Arch Version Repository Size\n================================================================================\nInstalling:\nvsftpd x86_64 2.2.2-11.el6_4.1 base 151 k\n\nTransactionSummary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 151 k\nInstalled size: 331 k\nDownloadingPackages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction TestSucceeded\nRunning Transaction\n\r Installing :vsftpd-2.2.2-11.el6_4.1.x86_64 1/1 \n\r Verifying :vsftpd-2.2.2-11.el6_4.1.x86_64 1/1 \n\nInstalled:\n vsftpd.x86_640:2.2.2-11.el6_4.1 \n\nComplete!\n"
]
}对于ansible来讲的话,很难生成yum配置文件,但可以使用wget将其批量下载
#可以看到只有定义个没用装,因为本来就没有安装。。
[root@node1ansible]# ansible all -a 'rpm -q vsftpd'
10.0.10.62 | FAILED | rc=1 >>
package vsftpd is not installed

10.0.10.60 | success | rc=0 >>
vsftpd-2.2.2-11.el6_4.1.x86_64

10.0.10.61 | success | rc=0 >>
vsftpd-2.2.2-11.el6_4.1.x86_64使其服务批量启动
[root@node1ansible]# ansible crontab -a '/etc/init.d/vsftpd start'
10.0.10.60 | success | rc=0 >>
Starting vsftpd for vsftpd: [ OK ]

10.0.10.61 | success | rc=0 >>
Starting vsftpd for vsftpd: [ OK ]再查看端口是否被监听
[root@node1ansible]# ansible crontab -a 'netstat -lnt ' | grep 21
tcp 0 00.0.0.0:21 0.0.0.0:* LISTEN
tcp 0 00.0.0.0:21 0.0.0.0:* LISTEN

以上,为absible的简单配置与使用,谢谢大家。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息