ansible自动化工具部署实例推荐

上次写了个puppet安装的文档,今天给大家介绍另外一款自动化运维工具:ansible ansible自动化工具只需要在一台机器上安装,其他机器不需要安装任何东西,这就是ansible比puppet, saltstck方便的地方。ansible的特点有哪些呢?下面为你逐个介绍:特点:

(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;

(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;

(3)、使用python编写,维护更简单;

(4)、基于SSH工作;

(5)、支持文件同步并且对修改之前的文件进行备份,支持回滚;

好了,下面我们开始安装ansible:首先得准备好安装环境

我这里准备了两台机器:server1:192.168.1.231server2:192.168.1.232

一、安装准备

#在server1上部署#关闭防火墙iptablesserviceiptablesstopchkconfigiptablesoff#并且修改SELINUX=disabledsed-i's#SELINUX=enforcing#SELINUX=disabled#g'/etc/selinux/config#使用yum安装python等软件yuminstallPyYAML.x86_64python-paramiko.noarchpython-jinja2.x86_64python-devel–y

二、下载安装包

#下载ansible和setuptools安装包wgethttps://pypi.python.org/packages/source/a/ansible/ansible-1.7.2.tar.gzwgethttps://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz

三、解压安装软件

#解压安装setuptoolstarzfxvsetuptools-7.0.tar.gzcdsetuptools-7.0pythonsetup.pyinstallcd..#解压安装ansibletarfzvxansible-1.7.2.tar.gzcdansible-1.7.2pythonsetup.pybuildpythonsetup.pyinstallmkdir/etc/ansiblecpexamples/ansible.cfg/etc/ansible/cpexamples/hosts/etc/ansible/cd..

四、修改配置文件

#配置ansible,我这里默认是不需要修改的vi/etc/ansible/ansible.cfghostfile=/etc/ansible/hostslibrary=/usr/share/ansibleremote_tmp=$HOME/.ansible/tmppattern=*forks=5poll_interval=15sudo_user=ansible#ask_sudo_pass=True#ask_pass=Truetransport=smartremote_port=22module_lang=C#修改/etc/ansible/hosts文件#local_Server[localhost]127.0.0.1#client[client]192.168.1.232#server2的IP

五、SSH通信设置

#ssh互信[root@ansibleserver~]#ssh-keygen–b1024-trsa#不断的回车即可Thekey'srandomartimageis:+--[RSA2048]----+|oo.||+=o.||.=+*o||o*OE.||.S.=||+..||.+||.|||+-----------------+
#然后进入.ssh/目录下[root@ansibleserver~]#cd.ssh/cat*.pub authorized_keyschmod–R700.#再通过scp命令将authorized_keys拷贝到客户端192.168.1.232scpauthorized_keysroot@192.168.1.232:/root/.ssh/authorized_keys#执行scp出现的错误以及解决方法:scp:/root/.ssh/authorized_keys:Nosuchfileordirectory#解决方法:在客户端也直接生成一个ssh-keygen–b124–trsa文件即可,这样的做法主要是生成.ssh/的目录,服务端才能够将公钥拷贝过去
#测试互信是否成功[root@ansibleserver~]#ssh192.168.1.232Lastlogin:TueMar1722:56:262015from192.168.1.231#无需密码,直接登录成功!说明互信已经成功了![root@localhost~]#ifconfigeth1Linkencap:EthernetHWaddr08:00:27:41:28:38inetaddr:192.168.1.232Bcast:192.168.1.255Mask:255.255.255.0

六、模块测试

模块命令

-i设备列表路径,可以指定一些动态路径-f并发任务数-private-key私钥路径-m模块名称-M模块夹的路径-a参数-k登陆密码-Ksudo密码-t输出结果保存路径-B后台运行超时时间-P调查后台程序时间-u执行用户-Usudo用户-l限制设备范围-s是此用户sudo无需输入密码

ping模块

#使用ansible的ping模块测试client是否能够通信!#注意:all代表所有client的意思[root@ansibleserver~]#ansibleall-mping192.168.1.232|success {"changed":false,"ping":"pong"127.0.0.1|success {"changed":false,"ping":"pong"#查看时间[root@ansibleserver~]#ansibleall-mcommand-a"date"192.168.1.232|success|rc=0 TueMar1723:06:43EDT2015127.0.0.1|success|rc=0 TueMar1723:06:44EDT2015

安装软件测试

[root@ansibleserver~]#ansibleall-mcommand-a"yuminstallunzip-y"192.168.1.232|success|rc=0 Loadedplugins:fastestmirrorSettingupInstallProcessDeterminingfastestmirrorsInstalled:unzip.x86_640:6.0-1.el6Complete!

copy模块

#拷贝文件到远程主机相关选项如下:backup:在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|nocontent:用于替代“src”,可以直接设定指定文件的值dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录directory_mode:递归设定目录的权限,默认为系统默认权限force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yesothers:所有的file模块里的选项都可以在这里使用src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
实例:拷贝本地的/root/script目录所有内容到192.168.1.232的/tmp目录下注意:因为script后面没有加/,所以拷贝的是整个目录ansible192.168.1.232-mcopy-a"src=/root/scriptdest=/tmp/owner=rootgroup=rootmode=0644"#拷贝成功的返回信息192.168.1.232|success {"changed":true,"dest":"/tmp/","src":"/root/script"#切换到192.168.1.232机器中查看[root@localhost~]#ls/tmp/script/a.txtb.txt#拷贝script目录的文件实例:注意:这里的script后面是加了/,所以只拷贝script目录下的文件[root@ansibleserverscript]#ansible192.168.1.232-mcopy-a"src=/root/script/dest=/tmp/script/owner=rootgroup=rootmode=0644"192.168.1.232|success {"changed":true,"dest":"/tmp/script/","src":"/root/script"}
在192.168.1.232的script目录下查看内容[root@localhostscript]#lltotal0-rw-r--r--1rootroot0Mar1723:38a.txt-rw-r--r--1rootroot0Mar1723:38b.txt-rw-r--r--1rootroot0Mar1723:38c.txt-rw-r--r--1rootroot0Mar1723:38d.txt#backup参数:有yes|no两个选项ansible192.168.1.232-mcopy-a"src=/root/script/dest=/tmp/script/owner=rootgroup=rootmode=0644backup=yes"提示:例如,src和dest同时有个a.txt文件,如果在src修改了a.txt再执行copy的时候,dest就会生成一个备份[root@localhostscript]#lltotal4-rw-r--r--1rootroot12Mar1803:09a.txt-rw-r--r--1rootroot0Mar1803:08a.txt.2015-03-18@03:09~#因为a.txt被修改过了,所以生成了一个备份[root@localhostscript]#cata.txt#这里被修改过,然后copy过来的helloworld[root@localhostscript]#cata.txt.2015-03-18\@03\:09~#备份的a.txt默认没有内容

file模块

#使用file模块,更改文件的用户和权限[root@ansibleserver~]#ansible192.168.1.232-mfile-a"dest=/tmp/a.txtmode=600"#查看更改情况[root@localhosttmp]#lltotal8-rw-------1rootroot0Mar1723:38a.txt#创建目录,类似mkdir–p[root@ansibleserver~]#ansible192.168.1.232-mfile-a"dest=/tmp/to/cmode=755owner=rootgroup=rootstate=directory"#查看创建情况[root@localhostc]#pwd/tmp/to/c#删除文件或者目录[root@ansibleserver~]#ansible192.168.1.232-mfile-a"dest=/tmp/a.txtstate=absent"192.168.1.232|success {"changed":true,"path":"/tmp/a.txt","state":"absent"#查看删除情况[root@localhosttmp]#lltotal8-rw-r--r--1rootroot0Mar1723:38b.txtdrwxr-xr-x2rootroot4096Mar1723:38scriptdrwxr-xr-x3rootroot4096Mar1723:53to-rw-------.1rootroot0Dec2819:45yum.log

cron模块

#注意:cron是为远程主机定义任务计划的#批量定义远程主机上的定时任务#首先我们在本地的/etc/ansible/目录下定义一个cron.yml文件-hosts:192.168.1.232#远程主机IPremote_user:root#指定执行的用户tasks:#任务-name:cron#任务名称cron:name='cpfile'minute=1job='/usr/bin/tmp/script/test.sh'提示:name为注释名称,minute为执行任务的时间间隔,job为执行的脚本#定义好之后,我们执行下ansible-playbook命令[root@ansibleserveransible]#ansible-playbookcron.ymlPLAYRECAP********************************************************************192.168.1.232:ok=2changed=1unreachable=0failed=0#出现ok=2change=1,代表已经在远程机子上做好定时任务了#在远程主机上查看:[root@ansible-clientscript]#crontab-l#Ansible:cpfile1****/usr/bin/tmp/script/test.sh
实例2:目的:在指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间命令:ansibleall-mcron-a'name="customjob"minute=*/3hour=*day=*month=*weekday=*job="/usr/sbin/ntpdate172.16.254.139"'

synchronize模块

#先声明下,使用rsync模块,远程主机系统必须安装rsync包,否则无法使用这个模块#先给远程机装个rsync吧[root@ansibleserver~]#ansible192.168.1.232-myum-a'name=rsyncstate=latest'#再次验证下rsync是否安装成功[root@ansibleserver~]#ansible192.168.1.232-a"whichrsync"192.168.1.232|success|rc=0 /usr/bin/rsync#看来没问题了!
#看下使用的参数[root@ansibleserver~]#ansible-doc-ssynchronize-name:Usesrsynctomakesynchronizingfilepathsinyourplaybooksquickandeasy.action:synchronizearchive#是否采用归档模式同步,即以源文件相同属性同步到目标地址checksum#是否效验compress#是否压缩copy_links#同步的时候是否复制连接delete#删除源中没有而目标存在的文件dest=#目标地址dest_port#目标接受的端口dirs#以非递归的方式传输目录existing_only#Skipcreatingnewfilesonreceiver.group#Preservegrouplinks#Copysymlinksassymlinks.mode#模式,rsync同步的方式PUSH\PULLrecursive#是否递归yes/norsync_opts#使用rsync的参数rsync_path#服务的路径(源码编译时需指定)rsync_timeout#Specifya--timeoutforthersynccommandinseconds.set_remote_user#putuser@fortheremotepaths.Ifyouhaveacustomsshconfigtodefinetheremoteuserforsrc=#源,同步的数据源times
实例:将ansible端/tmp/目录下的script同步到232机子的/tmp/目录下面[root@ansibleserver~]#ansible192.168.1.232-msynchronize-a'src=/tmp/scriptdest=/tmp/'192.168.1.232|success {"changed":true,"cmd":"rsync--delay-updates-FF--compress--archive--rsh'ssh-oStrictHostKeyChecking=no'--out-format=' CHANGED %i%n%L'\"/tmp/script\"\"root@192.168.1.232:/tmp/\"","msg":"cd+++++++++script/\n f+++++++++script/a.txt\n","rc":0,"stdout_lines":["cd+++++++++script/"," f+++++++++script/a.txt"]#注意:要想ansible端于远程端的文件保持一致,最好用delete=yes参数因为,有时候在ansible的目录下删除了某个文件,若不加delete=yes参数的话,远程端的目录下仍然保留有旧的文件!

服务管理

#启动client的httpd服务[root@ansibleserver~]#ansible192.168.1.232-mservice-a"name=httpdstate=started"192.168.1.232|success {"changed":true,"name":"httpd","state":"started"#注意:state的状态有:startedrestartedstoped#client端查看情况[root@localhost~]#netstat-lntup|grephttpdtcp00:::80:::*LISTEN1565/httpd

收集系统信息

#收集主机的所有系统信息[root@ansibleserver~]#ansible192.168.1.232-msetup#收集系统信息并以主机名为文件名分别保存在/tmp/facts目录[root@ansibleserverfacts]#ansible192.168.1.232-msetup--tree/tmp/facts[root@ansibleserverfacts]#lltotal12-rw-r--r--1rootroot8656Mar1800:25192.168.1.232#收集系统内存相关信息[root@ansibleserver~]#ansible192.168.1.232-msetup-a'filter=ansible_*_mb'192.168.1.232|success {"ansible_facts":{"ansible_memfree_mb":299,"ansible_memtotal_mb":490,"ansible_swapfree_mb":2047,"ansible_swaptotal_mb":2047},"changed":false#收集网卡信息[root@ansibleserver~]#ansible192.168.1.232-msetup-a'filter=ansible_eth[0-2]'

playbook管理复杂任务

对于需反复执行的、较为复杂的任务,我们可以通过定义 Playbook 来搞定。Playbook 是 Ansible 真正强大的地方,它允许使用变量、条件、循环、以及模板,也能通过角色及包含指令来重用既有内容。下面我们来看看一些具体的实例。

安装php软件实例

#首先在/etc/ansible目录下建立一个php.yaml的文件[root@ansibleserveransible]#vimphp.yaml-hosts:192.168.1.232#主机名,如果是全部主机,可以用allremote_user:root#指定执行操作的用户tasks:#任务-name:phpinstalling#起个任务的名字yum:name=phpstate=present#利用yum模块,安装软件的包名为php参数:present为安装absent为卸载提示:注意对齐的格式,不然会出错#用ansible-playbook参数调用php.yaml[root@ansibleserveransible]#ansible-playbookphp.yamlPLAY[192.168.1.232]**********************************************************GATHERINGFACTS***************************************************************ok:[192.168.1.232]TASK:[phpinstalling]********************************************************changed:[192.168.1.232]PLAYRECAP********************************************************************192.168.1.232:ok=2changed=1unreachable=0failed=0#看到结果,ok=2changed=1说明客户机(232)上的php安装成功了!

创建cron定时计划

#建立一个cron.yaml文件,然后每月10号来运行/root/dd.sql脚本#cron定时任务参数#Ensureajobthatrunsat2and5exists.#Createsanentrylike"*5,2**ls-alh /dev/null"-cron:name="checkdirs"hour="5,2"job="ls-alh /dev/null"#Ensureanoldjobisnolongerpresent.Removesanyjobthatisprefixed#by"#Ansible:anoldjob"fromthecrontab-cron:name="anoldjob"state=absent#Createsanentrylike"@reboot/some/job.sh"-cron:name="ajobforreboot"special_time=rebootjob="/some/job.sh"#Createsacronfileunder/etc/cron.d-cron:name="yumautoupdate"weekday="2"minute=0hour=12user="root"job="YUMINTERACTIVE=0/usr/sbin/yum-autoupdate"cron_file=ansible_yum-autoupdate#Removesacronfilefromunder/etc/cron.d-cron:cron_file=ansible_yum-autoupdatestate=absent

#关于ansible的介绍就到此,有不足之处,希望大家多多指教!

失败是成功的亲娘,没有失败哪来的成功呢?诺贝尔如果不经历千万次的失败,

ansible自动化工具部署实例推荐

相关文章:

你感兴趣的文章:

标签云: