linux运维、架构之路-ansible批量管理
一、ansible软件
1、介绍
①ansible是一个基于Python开发的自动化运维工具
②其功能实现基于SSH远程连接服务
③ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能
④http://docs.ansible.com/ 官方资料
2、ansible特点
①不需要安装客户端,基于系统自带的sshd服务,sshd就相当于ansible的客户端
②不需服务端
③需要依靠大量的模块实现批量管理功能
④配置文件/etc/ansible/ansible.cfg
3、ansible命令语法格式
4、常用参数
命令参数 |
参数说明 |
-m |
-m后边是模块的名字,默认模块为command |
-a |
-a,后面是要执行的命令;也可以写一个ip,针对一台服务器来执行命令 |
-C,--check |
不做任何改变,只是尝试预言一些可能出现的改变,执行前检查 |
--syntax-check |
执行语法检查在剧本,但不执行剧本 |
5、常用模块
常用模块 |
模块说明 |
command ***** |
执行命令模块,ansible命令执行默认模块 |
shell ***** |
执行shell脚本模块 |
script ***** |
把脚本发到客户端,然后执行,执行脚本命令在远端服务器上 |
copy ***** |
把本地文件发送到远端 |
file |
设定文件属性模块 |
service |
系统服务管理模块 |
cron |
定时任务管理模块 |
yum |
yum软件包安装管理模块 |
synchronize |
使用rsync同步文件模块 |
6、ansible查看帮助信息
ansible-doc –l #列出所有的模块 ansible-doc -s copy #查看指定模块用法
二、ansible实战部署
1、利用SSH批量分发公钥
[[email protected] scripts]# cat piliang.sh #!/bin/bash rm -f /root/.ssh/* ssh-****** -t dsa -f /root/.ssh/id_dsa -P "" &>/dev/null for ip in 8 31 41 do sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no [email protected]$ip" >/dev/null 2>&1 echo "172.16.1.$ip ok" done
2、ansible软件安装
yum install ansible -y #管理机m01安装 yum install libselinux-python -y #被管理机安装
3、模块使用测试
①command
[[email protected] scripts]# ansible oldboy -m command -a "hostname" #oldboy为主机组名称,也可以指定某一台服务器 [[email protected] scripts]# ansible 172.16.1.41 -a "hostname"
②copy
[[email protected] ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/hosts" ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/a/b/c/d/e/f/hosts" #目录不存在自动创建目录
③copy扩展
ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/host owner=oldboy group=oldboy mode=600" #更改文件权限属性
④backup
[[email protected] ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/hosts backup=yes"
⑤shell
ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripys/" #shell模块分发脚本时到被管理机然后再执行 ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=655" #直接加执行权限
⑥script
ansible oldboy -m script -a "/server/scripts/yum.sh" #script模块直接远程执行脚本,无需提前复制到被管理机
⑦yum
ansible oldboy -m yum -a "name=htop state=installed"
⑧cron
添加定时任务
ansible 172.16.1.31 -m cron -a 'name="install ser" minute=0 hour=0 job="/server/scripts/yum.sh &>/dev/null"'
删除定时任务
ansible 172.16.1.31 -m cron -a 'name="install ser" minute=0 hour=0 job="/bin/sh /server/scripts/yum.sh &>/dev/null" state=absent'
⑨mount模块
选项: fstype:挂载文件的类型(常见的有ext3,ext4,xfs) name:挂载点 opts:mount命令挂载的参数 passno src:要挂载的文件 state:mounted present:只处理fstab中的配置 absent:删除挂载点 mounted:自动创建挂载点并挂载之 umounted:卸载 例子: - name: Mount DVD read-only mount: path: /mnt/dvd src: /dev/sr0 fstype: iso9660 opts: ro state: present
⑩service模块
ansible oldboy -m service -a "name=crond state=stop enabled=no" #此处name是服务名,表示将crond停止,取消开机自启动
file模块
ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/hosts state=link #创建软链接
测试所有主机的连通性
[[email protected] ~]# ansible all -m ping
被管理机不需要分发公钥文件,实现ansible管理
[[email protected] ~]# cat /etc/ansible/hosts [test] #主机组名称 172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456 172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=123456 172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=123456
三、ansible剧本
1、剧本编写格式
# play-book #注释 - hosts:空格all #指定演员信息,这里all指处理所有服务器,也可以指定某一台,接ip地址 空格空格tasks: 空格空格空格空格- command:空格echo hello oldboy linux
2、多主机单任务编写方式
[[email protected] ansible-playbook]# cat cron.yml # command play-book - hosts: oldboy tasks: - cron: name=oldboy01 minute=0 hour=1 job='/bin/sh /server/scripts/yum.sh /dev/null'
3、 多主机多任务编写方式
[[email protected] ansible-playbook]# cat cron.yml # command play-book - hosts: oldboy tasks: - name: cron task cron: name=oldboy01 minute=0 hour=1 job='/bin/sh /server/scripts/yum.sh /dev/null' - name: shell task shell: echo "$HOSTNAME" >>/tmp/hostname.txt
4、不同主机做不同的任务
# command play-book - hosts: rsync tasks: - name: edit rsyncd.conf copy: src=/etc/ansible/conf/rsync_conf/rsyncd.conf dest=/etc/rsyncd.conf - name: create user shell: useradd -s /sbin/nologin -M rsync - hosts: nfs tasks: - name: yum install nfs-utils rpcbind shell: yum install -y nfs-utils rpcbind - name: edit exports copy: src=/etc/ansible/conf/nfs_conf/exports dest=/etc/exports
四、利用ansible安装rsync服务器
[[email protected] ansible-playbook]# cat rsync.yml #rsync server play-book - hosts: 172.16.1.41 tasks: - name: 1.install rsync yum: name=rsync state=installed - name: 2.edit rsyncd.conf copy: src=/etc/ansible/conf/rsync/rsyncd.conf dest=/etc/ - name: 3.create rsync user shell: userdel -r rsync && useradd rsync -s /sbin/nologin -M - name: 4.create auth file copy: src=/etc/ansible/conf/rsync/rsync.password dest=/etc/ mode=600 - name: 5.create backup dir file: dest=/backup state=directory owner=rsync group=rsync - name: 6.start rsync server shell: rsync --daemon
五、利用ansible安装nfs服务器
[[email protected] ansible-playbook]# cat nfs.yml #nfs server play-book - hosts: 172.16.1.31 tasks: - name: 1.install rpcbind yum: name=rpcbind state=installed - name: 2.install nfs-utils yum: name=nfs-utils state=installed - name: 3.edit nfs config exports copy: src=/etc/ansible/conf/nfs/exports dest=/etc/ - name: 4.create share dir shell: mkdir /data -p && chown -R nfsnobody.nfsnobody /data - name: 5.start rpcbind shell: /etc/init.d/rpcbind start - name: 6.start nfs shell: /etc/init.d/nfs start
六、利用ansible配置sersync数据同步
[[email protected] ansible-playbook]# cat sersync.yml #sersync server play-book - hosts: 172.16.1.31 tasks: - name: 1.send sersync soft copy: src=/etc/ansible/conf/sersync/sersync_installdir_64bit.zip dest=/server/tools/ - name: 2.exec sersync scripts script: /server/scripts/sersync.sh - name: 3.send sersync conf file copy: src=/etc/ansible/conf/sersync/confxml.xml dest=/usr/local/sersync/conf/ - name: 4.start sersync server shell: /usr/local/sersync/bin/sersync -dro /usr/local/sersync/conf/confxml.xml