介绍:
node
ansible是基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优势nginx
具备批量系统配置、批量程序部署、批量运行命令等功能。git
是基于模块工做的,自己没有批量部署的能力。真正具备批量部署的是ansible所运行的模块,ansible只是提供一种框架。github
操做:web
如下经过实际演练来更好的理解ansible的工做原理redis
使用三台机器组建ansible使用环境shell
注意:三台主机要网络同步时间,101主机做为ansible控制机,106和107主机做为普通服务器centos
yum -y install ansible
bash
#安装ansible服务器
基于密钥进行ssh认证
配置/etc/ansible/hosts
ssh-keygen -t rsa -P ''
for i in 106 107;do ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.$i ;done
ansible -help -m 指定调用的模块 -a 每一个模块都有本身的专有的参数,指定此模块参数 -f 指定每批处理多少台主机,默认5台 #更多的参数可自行了解 eg: ansible all --list-hosts #列出当前处于同一模式的全部主机 ansible webservers --list-hosts
ping模块: ansible all -m ping #测试ansible到各个服务器的连通性 192.168.1.106 | SUCCESS => { #状态是成功的,即连通性是没问题 "changed": false, #修改状态是失败,说明咱们未作任何修改操做 "ping": "pong" #ansible发出ping验证,对方返回pong告知链接正常 } 192.168.1.107 | SUCCESS => { "changed": false, "ping": "pong" }
ansible-doc -h #查看ansible文档的帮助信息 ansible-doc -l #列出ansible支持的全部模块信息,如上文只能中的ping模 ansible-doc -s command #查看指定模块的参数信息,command是一种经常使用的模块,默认不指定模块时就是使用command
command模块:
[root@node1 ~]#ansible 192.168.1.106 -m command -a 'pwd chdir=/app' #在执行命令pwd以前,先cd进入chdir指定的目录,因此结果显示目录是/app而不是root的家目录 #-m指定模块,-a指定参数(基本都是键值对),只是这里的参数其实是两台命令一个是pwd,一个是chdir,而这两个命令又有前后之分 ansible 192.168.1.106 -m command -a 'mkdir dir chdir=/app creates=dir' #建立目录dir,creates表示若是dir已经存在就再也不执行mkdir操做,直接跳过。此过程实现幂等性 #幂等:执行一次和执行屡次的结果都是同样的 #creates表示指定路径文件不存在才执行指令,而removes则相反,表示指定路径文件存在才执行指令 #creates和removes含义相反,注意区分,并且他们都是针对指定目录下的文件而不针对用户或进程操做 command模块是不能是被shell语法的,全部在用到如bash类的shell时可使用shell模块 ansible all -m shell -a "echo centos| passwd --stdin user1" #shell模块与command模块基本相同,只是此模块支持shell类型的语法 #若是换成command模块,则结果是echo后的内容所有做为字符串输出,即command是不识别管道和passwd等shell语言的
user和group模块:
group模块: ansible webservers -m group -a 'name=group1 system=yes state=present' #使用group组模块,向webservers中建立组gruop1,而且是系统组,present是建立此组的含义,absent是删除此组的意思 192.168.1.106 | SUCCESS => { "changed": true, "gid": 983, "name": "group1", "state": "present", "system": true } #组id、组名称、状态、系统组,操做成功 user模块: ansible webservers -m user -a "name=tomm groups=group1 state=present uid=20000 shell=/bin/csh" #建立tomm用户,指定辅助组、uid和shell类型,建立present, 192.168.1.106 | SUCCESS => { #上述操做成功 "changed": true, "comment": "", "createhome": true, #建立家目录 "group": 20000, # "groups": "group1", "home": "/home/tomm", #未指定用户家目录,则采用默认 "name": "tomm", "shell": "/bin/csh", "state": "present", "system": false, #表示不是系统用户 "uid": 20000 } #此时webservers组内的成员都建立了用户tomm
copy模块:
将文件复制到远程位置 ansible webservers -m copy -a 'src=/root/aaa dest=/app/ owner=daemon group=nobody mode=664' #使用copy模块,将本地的aaa文件复制到远程主机webservers组中,并修改用户/组和权限 192.168.1.106 | SUCCESS => { "changed": true, "checksum": "7272fa0670a2f6d5cf0f5c1e6f31641fad625bf8", "dest": "/app/aaa", "gid": 99, "group": "nobody", "md5sum": "890b22b2dc6ff3f00f2374dce5634526", "mode": "0664", "owner": "daemon", "secontext": "system_u:object_r:default_t:s0", "size": 1493, "src": "/root/.ansible/tmp/ansible-tmp-1510928315.78-72588321449487/source", #生成临时文件,而不是直接将源文件复制过去 "state": "file", "uid": 2 } #此时对aaa文件进行修改后在执行此操做,会发现changed字段依然是true,缘由是aaa修改后的hash值改变了 ansible webservers -m copy -a "content='hello,ansible' dest=/app/bbb owner=daemon group=nobody mode=664" #本机没有源文件而只是指定字符串,到目标webservers组,即远程主机上就会生成bbb文件,并设置成相应的所属用户权限等信息 #content只是修改文件内容,若是针对文件自己属性进行操做,可使用file模块
file模块:
此模块仅设置文件属性,不改变文件内容 ansible webservers -m file -a "path=/app/testdir state=directory owner=nobody mode=770" #在远程主机上建立目录,state后能够指定目录、文件和软硬连接,并设置属性 ansible webservers -m file -a "path=/app/testfile state=touch owner=nobody mode=666" #state用法比较奇特,除了指定文件类型外还能够建立文件,即state为touch ansible webservers -m file -a "path=/app/filelink src=/app/testfile state=link" #link表示符号连接,若是是hard表示硬连接 ansible webservers -m file -a "path=/app/filelink state=absent" #absent表示path指定文件删除文件
get_url模块:
从互联网获取文件到本节点,可经过http、https和ftp实现
ansible webservers -m get_url -a "url=http://download.redis.io/releases/redis-4.0.2.tar.gz dest=/app/" #url指定下载的资源路径,dest指定下载到指定的目录,此外还能够指定下载文件的属主信息以及校验信息如sha256sum
cron模块:
计划任务 ansible webservers -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1' minute='*/5'" #建立热舞计划,name指定计划任务的名称,job指定任务计划的操做,minute表示执行的时间点即每五分钟执行一次同步时间操做 ansible webservers -m cron -a "name='timesync' state=absent" #删除计划任务 ansible webservers -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1' minute='*/5' disabled=true" #disabled表示虽然建立计划任务可是不启用,要启用的话将true改成false
yum模块:
ansible webservers -m yum -a "name=nginx state=latest" #安装nginx,最新版本,安装过程当中的信息会所有输出值屏幕端
service模块:
管理服务 ansible webservers -m service -a "name=nginx enabled=true state=started" #开启nginx服务,并设置成开机自启
git模块:
ansible webservers -m git -a "repo=https://github.com/magro/memcached-session-manager.git dest=/app/git" #repo指定git文件路径,dest指定git文件的存放位置,存放的目录不能有内容 #前提是远程主机须要提早安装git