1、基础介绍html
==========================================================================================java
1、简介node
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优势,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工做的,自己没有批量部署的能力。真正具备批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:python
(1)、链接插件connection plugins:负责和被监控端实现通讯;nginx
(2)、host inventory:指定操做的主机,是一个配置文件里面定义监控的主机;git
(3)、各类模块核心模块、command模块、自定义模块;web
(4)、借助于插件完成记录日志邮件等功能;算法
(5)、playbook:剧本执行多个任务时,非必需可让节点一次性运行多个任务。shell
2、整体架构数据库
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、任务执行流程
说明:
(1)、以上内容大可能是基于他人分享的基础上总结而来,学习借鉴之用;
2、centos7简单安装和配置ansible
==========================================================================================
(1)、安装EPEL库
# yum install epel-release # yum repolist
(2)、安装ansible
# yum install ansible
(3)、生成公钥和私钥并下发
# ssh-keygen 出现提示选者默认便可 # ssh-copy-id root@192.168.118.130 # ssh-copy-id root@192.168.118.129
(4)、配置操做主机清单
# vi /etc/ansible/hosts [storm_cluster] #测试主机分组名称 192.168.118.129 192.168.118.130
(5)、测试
# ansible storm_cluster -m ping
3、ansible经常使用模块介绍
==========================================================================================
根据zs官方的分类,将模块按功能分类为:云模块、命令模块、数据库模块、文件模块、资产模块、消息模块、监控模块、网络模块、通知模块、包管理模块、源码控制模块、系统模块、单元模块、web设施模块、windows模块 这里从官方分类的模块里选择最经常使用的一些模块进行介绍。
测试主机是不是通的,用法很简单,不涉及参数:
ansible test -m ping
setup模块,主要用于获取主机信息,在playbooks里常常会用到的一个参数gather_facts就与该模块相关。setup模块下常用的一个参数是filter参数,具体使用示例以下:
ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb' //查看主机内存信息
ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]' //查看地接口为eth0-2的网卡信息
ansible all -m setup --tree /tmp/facts //将全部主机的信息输入到/tmp/facts目录下,每台主机的信息输入到主机名文件中(/etc/ansible/hosts里的主机名)
file模块主要用于远程主机上的文件操做,file模块包含以下选项:
force:须要在两种状况下强制建立软连接,一种是源文件不存在但以后会创建的状况下;另外一种是目标软连接已存在,须要先取消以前的软链,而后建立新的软链,有两个选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
owner:定义文件/目录的属主
path:必选项,定义文件/目录的路径
recurse:递归的设置文件的属性,只对目录有效
src:要被连接的源文件的路径,只应用于state=link的状况
dest:被连接到的路径,只应用于state=link的状况
state:
directory:若是目录不存在,建立目录
file:即便文件不存在,也不会被建立
link:建立软连接
hard:建立硬连接
touch:若是文件不存在,则会建立一个新的文件,若是文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消连接文件
使用示例:
ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"
ansible test -m file -a "path=/tmp/fstab state=absent"
ansible test -m file -a "path=/tmp/test state=touch"
复制文件到远程主机,copy模块包含以下选项:
backup:在覆盖以前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
content:用于替代"src",能够直接设定指定文件的值
dest:必选项。要将源文件复制到的远程主机的绝对路径,若是源文件是一个目录,那么该路径也必须是个目录
directory_mode:递归的设定目录的权限,默认为系统默认权限
force:若是目标主机包含该文件,但内容不一样,若是设置为yes,则强制覆盖,若是为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others:全部的file模块里的选项均可以在这里使用
src:要复制到远程主机的文件在本地的地址,能够是绝对路径,也能够是相对路径。若是路径是一个目录,它将递归复制。在这种状况下,若是路径使用"/"来结尾,则只复制目录里的内容,若是没有使用"/"来结尾,则包含目录在内的整个内容所有复制,相似于rsync。
validate :The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the visudo example below.
示例以下:
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"
ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"
用于管理服务
该模块包含以下选项:
arguments:给命令行提供一些选项
enabled:是否开机启动 yes|no
name:必选项,服务名称
pattern:定义一个模式,若是经过status指令来查看服务的状态时,没有响应,就会经过ps指令在进程中根据该模式进行查找,若是匹配到,则认为该服务依然在运行
runlevel:运行级别
sleep:若是执行了restarted,在则stop和start之间沉睡几秒钟
state:对当前服务执行启动,中止、重启、从新加载等操做(started,stopped,restarted,reloaded)
使用示例:
ansible test -m service -a "name=httpd state=started enabled=yes"
asnible test -m service -a "name=foo pattern=/usr/bin/foo state=started"
ansible test -m service -a "name=network state=restarted args=eth0"
用于管理计划任务包含以下选项:
backup:对远程主机上的原任务计划内容修改以前作备份
cron_file:若是指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
day:日(1-31,*,*/2,……)
hour:小时(0-23,*,*/2,……)
minute:分钟(0-59,*,*/2,……)
month:月(1-12,*,*/2,……)
weekday:周(0-7,*,……)
job:要执行的任务,依赖于state=present
name:该任务的描述
special_time:指定何时执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
state:确认该任务计划是建立仍是删除
user:以哪一个用户的身份执行
示例:
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root
ansible test -m cron -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"'
ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
使用yum包管理器来管理软件包,其选项有:
config_file:yum的配置文件
disable_gpg_check:关闭gpg_check
disablerepo:不启用某个源
enablerepo:启用某个源
name:要进行操做的软件包的名字,也能够传递一个url或者一个本地的rpm包的路径
state:状态(present,absent,latest)
示例以下:
ansible test -m yum -a 'name=httpd state=latest'
ansible test -m yum -a 'name="@Development tools" state=present'
ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令。
home:指定用户的家目录,须要与createhome配合使用
groups:指定用户的属组
uid:指定用的uid
password:指定用户的密码
name:指定用户名
createhome:是否建立家目录 yes|no
system:是否为系统用户
remove:当state=absent时,remove=yes则表示连同家目录一块儿删除,等价于userdel -r
state:是建立仍是删除
shell:指定用户的shell环境
使用示例:
user: name=johnd comment="John Doe" uid=1040 group=admin
user: name=james shell=/bin/bash groups=admins,developers append=yes user: name=johnd state=absent remove=yes
user: name=james18 shell=/bin/zsh groups=developers expires=1422403387
user: name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa #生成密钥时,只会生成公钥文件和私钥文件,和直接使用ssh-keygen指令效果相同,不会生成authorized_keys文件。
注:指定password参数时,不能使用明文密码,由于后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中,因此须要先将密码字符串进行加密处理。而后将获得的字符串放到password中便可。
echo "123456" | openssl passwd -1 -salt $(< /dev/urandom tr -dc '[:alnum:]' | head -c 32) -stdin
$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0
#使用上面的密码建立用户
ansible all -m user -a 'name=foo password="$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0"'
不一样的发行版默认使用的加密方式可能会有区别,具体能够查看/etc/login.defs文件确认,centos 6.5版本使用的是SHA512加密算法。
ansible all -m group -a 'name=somegroup state=present'
使用rsync同步文件,其参数以下:
archive: 归档,至关于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启
checksum: 跳过检测sum值,默认关闭
compress:是否开启压缩
copy_links:复制连接文件,默认为no ,注意后面还有一个links参数
delete: 删除不存在的文件,默认no
dest:目录路径
dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议
dirs:传速目录不进行递归,默认为no,即进行目录递归
rsync_opts:rsync参数部分
set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不一样的状况
mode: push或pull 模块,push模的话,通常用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件
使用示例:
src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
src=some/relative/path dest=/some/absolute/path archive=no links=yes
src=some/relative/path dest=/some/absolute/path checksum=yes times=no
src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
dev:目标块设备
force:在一个已有文件系统 的设备上强制建立
fstype:文件系统的类型
opts:传递给mkfs命令的选项
ansible test -m filesystem -a 'fstype=ext2 dev=/dev/sdb1 force=yes'
ansible test -m filesystem -a 'fstype=ext4 dev=/dev/sdb1 opts="-cc"'
配置挂载点
选项:
dump
fstype:必选项,挂载文件的类型
name:必选项,挂载点
opts:传递给mount命令的参数
src:必选项,要挂载的文件
state:必选项
present:只处理fstab中的配置
absent:删除挂载点
mounted:自动建立挂载点并挂载之
umounted:卸载
示例:
name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
name=/srv/disk src='LABEL=SOME_LABEL' state=present
name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
ansible test -a 'losetup /dev/loop0 /disk.img'
ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0'
ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
该模块主要用于从http、ftp、https服务器上下载文件(相似于wget),主要有以下选项:
sha256sum:下载完成后进行sha256 check;
timeout:下载超时时间,默认10s
url:下载的URL
url_password、url_username:主要用于须要用户名密码进行验证的状况
use_proxy:是事使用代理,代理需事先在环境变动中定义
示例:
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
用于解压文件,模块包含以下选项:
copy:在解压文件以前,是否先将文件复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存在。
creates:指定一个文件名,当该文件存在时,则解压指令不执行
dest:远程主机上的一个路径,即文件解压的路径
grop:解压后的目录或文件的属组
list_files:若是为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项
mode:解决后文件的权限
src:若是copy为yes,则须要指定压缩文件的源路径
owner:解压后文件或目录的属主
示例以下:
- unarchive: src=foo.tgz dest=/var/lib/foo - unarchive: src=/tmp/foo.zip dest=/usr/local/bin copy=no - unarchive: src=https://example.com/example.zip dest=/usr/local/bin copy=no
3、ansible playbook
==========================================================================================
一、建立文件实例
(1)、编辑配置文件
[root@master ~]# cd /etc/ansible/
[root@master ansible]# vim test.yaml //固定后缀为yaml,必定要注意空格
--- - hosts: testhost user: root tasks: - name: playbook_test shell: touch /tmp/playbook.txt
hosts参数指定了对哪些主机进行参做;注意:
user参数指定了使用什么用户登陆远程主机操做;
tasks指定了一个任务,其下面的name参数一样是对任务的描述,在执行过程当中会打印出来。
(2)、执行配置文件
[root@master ansible]# ansible-playbook test.yml
一、playbook安装etcd详细示例
(1)、先配置须要在哪些机器上安装etcd,编辑/etc/ansible/hosts以下.
[dc@sdw1 ~]$ cat /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups [etcd_host] 10.1.4.59
(2)、编写playbook
创建测试目录
[dc@sdw1 test]$ ll 总用量 4 -rw-rw-r-- 1 dc dc 237 6月 13 10:50 _etcd.yaml drwxrwxr-x 2 dc dc 25 6月 13 11:02 group_vars drwxrwxr-x 3 dc dc 17 6月 13 10:52 roles
其中_etcd.yaml就是咱们要的剧本,roles目录下面是配合脚本运行的文件,查看目录结构
[dc@sdw1 test]$ du roles/ 9924 roles/etcd/files 4 roles/etcd/tasks 4 roles/etcd/templates 9932 roles/etcd 9932 roles/
其中templates文件夹中放一些配置文件,files中放一些须要的文件,好比安装包等
[dc@sdw1 test]$ cat _etcd.yaml --- # update hosts files of all nodes - hosts: etcd_host #指定要操做的主机 remote_user: root #指定要登陆的帐户 become_method: sudo name: "install and copy etcd file and start service" vars_files: - group_vars/hostmap.yaml #要导入的变量文件,若是须要变量文件,此处添加 roles: - role: etcd
查看运行的脚本步骤:
[dc@sdw1 test]$ cat roles/etcd/tasks/main.yaml - name: create work dir file: state=directory path={{work_dir}} - name: copy etcd.tar.gz copy: src={{etcd_tar}}.tar.gz dest={{work_dir}}{{etcd_tar}}.tar.gz - name: unzip etcd unarchive: src={{work_dir}}{{etcd_tar}}.tar.gz dest={{work_dir}} copy=no - name: copy shell etcd command: cp {{work_dir}}{{etcd_tar}}/etcd /usr/bin/ - name: copy shell etcdctl command: cp {{work_dir}}{{etcd_tar}}/etcdctl /usr/bin/ - name: create service copy: src=etcd.service dest=/usr/lib/systemd/system/etcd.service - name: change etcd conf template: src=etcd.conf dest=/etc/etcd/etcd.conf - name: start etcd service service: name=etcd state=restarted enabled=yes
这些命令跟上面介绍的模块命令基本一致,全部命令都有-name来讲明用处,补充说明:
1.{{work_dir}}表示引用group_vars/hostmap.yaml中有配置的参数
2.命令中template: src=etcd.conf表示从templates中拷贝配置文件到指定目录,templates文件夹中的文件也能够动态引用全局参数.
执行剧本:
[root@sdw1 test]# ansible-playbook _etcd.yaml PLAY [install and copy etcd file and start service] ******************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************************************ ok: [10.1.4.59] TASK [etcd : create work dir] ***************************************************************************************************************************************************************** ok: [10.1.4.59] TASK [etcd : copy etcd.tar.gz] **************************************************************************************************************************************************************** ok: [10.1.4.59] TASK [etcd : unzip etcd] ********************************************************************************************************************************************************************** ok: [10.1.4.59] TASK [etcd : copy shell etcd] ***************************************************************************************************************************************************************** changed: [10.1.4.59] TASK [etcd : copy shell etcdctl] ************************************************************************************************************************************************************** changed: [10.1.4.59] TASK [etcd : create service] ****************************************************************************************************************************************************************** ok: [10.1.4.59] TASK [etcd : change etcd conf] **************************************************************************************************************************************************************** ok: [10.1.4.59] TASK [etcd : start etcd service] ************************************************************************************************************************************************************** changed: [10.1.4.59] PLAY RECAP ************************************************************************************************************************************************************************************ 10.1.4.59 : ok=9 changed=3 unreachable=0 failed=0
由于这边已经安装过一遍,因此changed=3,到此就安装完成了.
4、ansible playbook 技巧
==========================================================================================
在刚开始使用 ansible-playbook 作应用程序部署的时候,由于在部署的过程当中有使用到 command 或 shell 模块执行一些自定义的脚本,并且这些脚本都会有输出,用来表示是否执行正常或失败。若是像以前本身写脚本作应用程序部署的,这很好实现。但如今是用 Ansible 作,那么要怎么样作能够获取到 ansible playbook 中 command 模块的输出呢? Ansible 也提供的解决办法,这时咱们就能够经过使用 register 关键字来实现,register 关键字能够存储指定命令的输出结果到一个自定义的变量中,咱们经过访问这个自定义变量就能够获取到命令的输出结果。Register 的使用很方便,只须要在 task 声明 register 关键字,并自定义一个变量名就能够。以下:
- name: echo date
command: date register: date_output - name: echo date_output command: echo "30" when: date_output.stdout.split(' ')[2] == "30"
这里第 1 个 task 是执行了一个 date 命令,register 关键字将 date 命令的输出存储到 date_output 变量名。第 2 个 task 对输出进行分析,并使用 when 对关键字对分析后的进行判断,若是匹配,则执行这个 task,不匹配就不执行。这里要重点说下的,由于 register 获取到的输出内容都是字符串,而 ansible 又是 python 写的,你可使用 python 字符串的方法对其作处理,好比本文中使用的 split,还可使用 find 方法。我的以为,真是很是灵活方便。
参考资料:
http://www.mamicode.com/info-detail-1240734.html
http://breezey.blog.51cto.com/2400275/1555530/
http://blog.csdn.net/fruitful_life/article/details/53195478
http://www.ansible.com.cn/index.html