你只需要可以使用ssh访问你的服务器或设备就行。
1.安装软件
2.配置服务
ansible可以帮助我们完成一些批量任务,或者完成一些需要经常重复的工作。
比如:同时在100台服务器上安装nfs服务,并在安装后启动服务。
比如:将某个文件一次性拷贝到100台服务器上。
比如:每当有新服务器加入工作环境时,你都要为新服务器部署某个服务,也就是说你需要经常重复的完成相同的工作。
这些场景中我们都可以使用到ansible。
1.ansible不需要单独安装客户端,ssh相当于ansible客户端。
2.ansible不需要启动任何服务,仅需安装对应工具即可。
3.ansible依赖大量的python模块来实现批量管理。
4.ansible配置文件/etc/ansible/ansible.cfg
实现从管理机m01到其他机器的密钥认证
创建及利用非交换式工具实现批量分发公钥与批量管理服务器
[root@m01 ~]# ssh-keygen -t rsa [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# yum install ansible -y
[root@m01 ~]# vim /etc/ansible/hosts [zeq] 172.16.1.31 172.16.1.41
[root@m01 ~]# ansible zeq -m ping 172.16.1.7 | success => { "changed": false, "ping": "pong" } 172.16.1.31 | success => { "changed": false, "ping": "pong" } 172.16.1.41 | success => { "changed": false, "ping": "pong" }
[root@m01 ~]# ansible zeq -m command -a "df -h"
172.16.1.41 ansible_ssh_user='root' ansible_ssh_pass='1' ansible_ssh_port='22'
[root@m01 ~]# vim /etc/ansible/hosts [web] 172.16.1.7 [nfs] 172.16.1.31 [backup] 172.16.1.41 [zeq:children] web nfs backup
[root@m01 ~]# ansible web --list-hosts #web hosts (1): 172.16.1.7 [root@m01 ~]# ansible nfs --list-hosts #nfs hosts (1): 172.16.1.31 [root@m01 ~]# ansible backup --list-hosts #rsync hosts (1): 172.16.1.41 [root@m01 ~]# ansible zeq --list-hosts #集中所有的小组,用于执行一些基础的配置 hosts (3): 172.16.1.31 172.16.1.41 172.16.1.7
1.command 执行命令
2.shell 执行命令
3.yum 安装软件模块
4.copy 配置模块
5.service 启动服务模块
6.user 用户管理
7.file 创建目录,创建文件,往文件写内容
8.cron 定时任务
9.mount 挂载
默认模块, 执行命令
[root@m01 ~]# ansible zeq -a "hostname"
[root@m01 ~]# ansible zeq -m shell -a "ifconfig|grep eth0" -f 50
-f =forks /etc/ansible/ansible.cfg #结果返回的数量
|
|
|
|
|
|
|
|
|
|
|
|
[root@m01 ~]# ansible zeq -m yum -a "name=httpd state=installed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[root@m01 ~]# ansible zeq -m copy -a "src=/etc/hosts dest=/tmp/test.txt owner=www group=www mode=0600"
[root@m01 ~]# ansible zeq -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"
[root@m01 ~]# ansible zeq -m copy -a "content='bgx' dest=/tmp/zeq"
参数 |
说明 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[root@m01 ~]# ansible zeq -m service -a "name=crond state=stopped enabled=yes"
[root@m01 ~]# echo "bgx"| openssl passwd -1 -stdin $1$1kmecnsk$hgnbe86f/xkxufl.n6seb.
[root@m01 ~]# ansible zeq -m user -a 'name=xlw password="$1$1kmecnsk$hgnbe86f/xkxufl.n6seb."'
参数 |
说明 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[root@m01 ~]# ansible zeq -m file -a "path=/tmp/zeq state=diretory" [root@m01 ~]# ansible zeq -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root" [root@m01 ~]# ansible zeq -m file -a "src=/tmp/tt path=/tmp/tt_link state=link"
正常使用
crond
服务
[root@m01 ~]# crontab -l * * * * * /bin/sh /server/scripts/yum.sh
使用
ansible
添加一条定时任务
[root@m01 ~]# ansible zeq -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh /server/scripts/test.sh'" [root@m01 ~]# ansible zeq -m cron -a "job='/bin/sh /server/scripts/test.sh'"
设置定时任务注释信息,防止重复,
name
设定
[root@m01 ~]# ansible zeq -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"
删除相应定时任务
[root@m01 ~]# ansible zeq -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent"
注释相应定时任务,使定时任务失效
[root@m01 scripts]# ansible zeq -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=no"
参数 |
说明 |
|
|
|
|
|
|
|
指定挂载文件类型 |
opts |
设定挂载的参数选项信息 |
path |
指定挂载点 |
[root@m01 ~]# ansible zeq -m mount -a "path=/backup src=10.0.0.31:/data fstype=nfs opts=defautls,noatime state=mounted"
ansible-doc -l --- 查看所有模块说明信息 ansible-doc copy --- 表示指定查看某个模块参数用法信息
[root@m01 ~]# sshpass -p1 ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
[root@m01 ~]# cat /etc/ansible/hosts [web] 172.16.1.7 172.16.1.8 [nfs] 172.16.1.31 [backup] 172.16.1.41
[root@m01 ~]# ansible all -m ping epel、firewalld、selinux、ww
1.所有的主机都需要安装rsync和nfs-utils
2.所有的主机都需要准备对应的rsync客户端的密码文件/etc/rsync.pass
3.所有的主机都需要创建一个uid和gid为666的www用户
4.所有的主机都需要全网备份的脚本,并配置好定时任务
[root@m01 ~]# ansible all -m yum -a "name=rsync,nfs-utils state=installed"
[root@m01 ~]# ansible all -m copy -a "content='1' dest=/etc/rsync.pass owner=root group=root mode=600"
[root@m01 ~]# ansible all -m group -a "name=www gid=666" [root@m01 ~]# ansible all -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin"
[root@m01 ~]# ansible all -m copy -a "src=./scripts/rsync_backup_md5.sh dest=/server/scripts/ mode=755" [root@m01 ~]# ansible all -m cron -a "name='rsync bakcup scripts' hour=01 minute=00 job='/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null'" [root@m01 ~]# pwd /root [root@m01 ~]# mkdir scripts [root@m01 ~]# cat scripts/rsync_backup_md5.sh #!/usr/bin/bash export path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
4.4.5
脚本编写
1.定义变量
host=$(hostname) addr=$(ifconfig eth1|awk 'nr==2{print $2}') date=$(date +%f) dest=${host}_${addr}_${date} path=/backup
2.创建备份目录
[ -d $path/$dest ] || mkdir -p $path/$dest
3.备份对应的文件
cd / && \ [ -f $path/$dest/system.tar.gz ] || tar czf $path/$dest/system.tar.gz etc/fstab etc/rsyncd.conf && \ [ -f $path/$dest/log.tar.gz ] || tar czf $path/$dest/log.tar.gz var/log/messages var/log/secure && \
4.携带md5验证信息
[ -f $path/$dest/${date}.flag ] || md5sum $path/$dest/*.tar.gz >$path/$dest/${date}.flag
5.推送本地数据至备份服务器
export rsync_password=1 rsync -avz $path/ rsync_backup@172.16.1.41::backup
6.本地保留最近7天的数据
find $path/ -type d -mtime +7|xargs rm -rf
1.安装rsync
2.配置rsync,/etc/rsyncd.conf
3.创建目录,创建虚拟用户文件,变更权限
4.启动服务,加入开机自启动
5.配置邮箱,准备对应的脚本
[root@m01 ~]# ansible backup -m yum -a "name=rsync state=installed"
[root@m01 ~]# ansible backup -m copy -a "src=./conf/rsyncd.conf dest=/etc/rsyncd.conf" [root@m01 ~]# pwd /root [root@m01 ~]# mkdir conf [root@m01 ~]# cat conf/rsyncd.conf uid = www gid = www port = 873 fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = false auth users = rsync_backup secrets file = /etc/rsync.password log file = /var/log/rsyncd.log ##################################### [backup] path = /backup [data] path = /data
[root@m01 ~]# ansible backup -m file -a "path=/backup state=directory mode=755 owner=www group=www" [root@m01 ~]# ansible backup -m file -a "path=/data state=directory mode=755 owner=www group=www" [root@m01 ~]# ansible backup -m copy -a "content='rsync_backup:1' dest=/etc/rsync.password mode=600 owner=root group=root"
[root@m01 ~]# ansible backup -m service -a "name=rsyncd state=started enabled=yes"
配置邮箱(配发件服务器)
yum install mailx -y set from=xxxxxxx@qq.com 填写自己邮箱 set smtp=smtps://smtp.qq.com:465 set smtp-auth-user=xxxxxxxx@qq.com 填写自己邮箱 set smtp-auth-password=#填写客户端授权码 set smtp-auth=login set ssl-verify=ignore set nss-config-dir=/etc/pki/nssdb/
1.定义全局的变量
export path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2.定义局部变量
path=/backup date=$(date +%f)
3.查看flag文件,并对该文件进行校验, 然后将校验的结果保存至result_时间
find $path/*_${date} -type f -name "flag"|xargs md5sum -c >$path/result_${date}
4.将校验的结果发送邮件给管理员
mail -s "rsync backup $date" 123@qq.com <$path/result_${date}
5.删除超过7天的校验结果文件, 删除超过180天的备份数据文件
find $path/ -type f -name "result*" -mtime +7|xargs rm -f find $path/ -type d -mtime +180|xargs rm -rf
1.安装nfs-utils
2.配置nfs-utils
3.创建对应的共享目录,并修改权限
4.启动nfs
[root@m01 ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"
[root@m01 ~]# ansible nfs -m copy -a "content='/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' dest=/etc/exports"
4.6.3 创建对应的共享目录,并递归修改权限
[root@m01 ~]# ansible nfs -m file -a "path=/data state=directory recurse=yes owner=www group=www mode=755"
[root@m01 ~]# ansible nfs -m service -a "name=nfs-server state=started enabled=yes"
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
1.验证nfs存储是否可以用
2.验证rsync是否能完成推送
3.backup服务器进行校验
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论