Ansible 二
Posted xiaomai-rhce
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible 二相关的知识,希望对你有一定的参考价值。
3.script模块 很少用到脚本
# 编写脚本
[[email protected] ~]# mkdir -p /server/scripts
[[email protected] ~]# cat /server/scripts/yum.sh
#!/usr/bin/bash
yum install -y iftop
#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[[email protected] ~]# ansible oldboy -m script -a "/server/scripts/yum.sh"
5.file配置模块
创建目录
[[email protected] ~]# ansible oldboy -m file -a "path=/tmp/oldboy state=diretory"
创建文件
[[email protected] ~]# ansible oldboy -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root" [[email protected] ~]# ansible oldboy -m file -a "src=/tmp/tt path=/tmp/tt_link state=link"
递归
path --- 指定远程主机目录或文件信息
recurse --- 递归授权
state ---
directory --- 在远端创建目录
touch --- 在远端创建文件
link --- link或hard表示创建链接文件
absent --- 表示删除文件或目录
mode --- 设置文件或目录权限
owner --- 设置文件或目录属主信息
group --- 设置文件或目录属组信息
7.group模块
name --- 指定创建的组名
gid --- 指定组的gid
state
absent --- 移除远端主机的组
present --- 创建远端主机的组(默认)
创建组,指定gid
[[email protected] ~]# ansible oldboy -m group -a "name=oldgirl gid=888"
8.user模块
uid --- 指定用户的uid
group --- 指定用户组名称
groups --- 指定附加组名称
password --- 给用户添加密码
shell --- 指定用户登录shell
create_home --- 是否创建家目录
创建oldgirl,设定uid为888,并加入gid为888
[[email protected] ~]# ansible oldboy -m user -a "name=oldgirl uid=888 group=888 shell=/sbin/nologin create_home=no"
随机生成加密字符串(-1使用MD5进行加密 -stdin 非交互式 -salt 加密参数)
[[email protected] ~]# echo "bgx" | openssl passwd -1 -stdin
固定加密字符串
[[email protected] ~]# echo "123"| openssl passwd -1 -stdin -salt ‘salt
创建普通用户,并配置对应的用户密码
[[email protected] ~]# echo "bgx" | openssl passwd -1 -stdin
$1$1KmeCnsK$HGnBE86F/XkXufL.n6sEb.
[[email protected] ~]# ansible oldboy -m user -a ‘name=xlw password="$1$765yDGau$diDKPRoCIPMU6KEVEaPTZ0"‘
9.crond模块
# 正常使用crond服务
[[email protected] ~]# crontab -l
* * * * * /bin/sh /server/scripts/yum.sh
# 使用ansible添加一条定时任务
[[email protected] ~]# ansible oldboy -m cron -a "minute=* hour=* day=* month=* weekday=* job=‘/bin/sh /server/scripts/test.sh‘"
[[email protected] ~]# ansible oldboy -m cron -a "job=‘/bin/sh /server/scripts/test.sh‘"
# 设置定时任务注释信息,防止重复,name设定
[[email protected] ~]# ansible oldboy -m cron -a "name=‘cron01‘ job=‘/bin/sh /server/scripts/test.sh‘"
# 删除相应定时任务
[[email protected] ~]# ansible oldboy -m cron -a "name=‘ansible cron02‘ minute=0 hour=0 job=‘/bin/sh /server/scripts/test.sh‘ state=absent"
# 注释相应定时任务,使定时任务失效
[[email protected] scripts]# ansible oldboy -m cron -a "name=‘ansible cron01‘ minute=0 hour=0 job=‘/bin/sh /server/scripts/test.sh‘ disabled=yes"
10.mount模块
present ---开机挂载,仅将挂载配置写入/etc/fstab
mounted ---挂载设备,并将配置写入/etc/fstab
unmounted ---卸载设备,不会清除/etc/fstab写入的配置
absent ---卸载设备,会清理/etc/fstab写入的配置
仅将挂载的配置写入/etc/fstab,并不会执行挂载操作
[[email protected] ~]# ansible oldboy -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=present"
临时挂载设备,并将挂载信息写入/etc/fstab
[[email protected] ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
临时卸载,不会清理/etc/fstab
[[email protected] ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=unmounted"
卸载,不仅临时卸载,同时会清理/etc/fstab
[[email protected] ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=absent"
yum copy service mount cron user file
1.机器还原快照(firewalld、selinux、配置好仓库)
选择虚拟机-》快照-》恢复
2.推送你的公钥
[[email protected] ~]# sshpass -p1 ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
3.配置Ansible的主机清单
[[email protected] ~]# cat /etc/ansible/hosts
[web]
172.16.1.7
172.16.1.8
[nfs]
172.16.1.31
[backup]
172.16.1.41
检查主机是否都ok
[[email protected] ~]# ansible all -m ping
epel、firewalld、selinux、ww
1.基础环境:
1.所有的主机都需要安装rsync和nfs-utils
2.所有的主机都需要准备对应的rsync客户端的密码文件/etc/rsync.pass
3.所有的主机都需要创建一个uid和gid为666的www用户
4.所有的主机都需要全网备份的脚本,并配置好定时任务
1.安装rsync和nfs-utils
[[email protected] ~]# ansible all -m yum -a "name=rsync,nfs-utils state=installed"
2.准备rsync的客户端密码文件
[[email protected] ~]# ansible all -m copy -a "content=‘1‘ dest=/etc/rsync.pass owner=root group=root mode=600"
3.准备对应的www用户,uid和gid都为666
[[email protected] ~]# ansible all -m group -a "name=www gid=666"
[[email protected] ~]# ansible all -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin"
4.从管理上拷贝对应的脚本文件,然后将其加入定时任务
[[email protected] ~]# ansible all -m copy -a "src=./scripts/rsync_backup_md5.sh dest=/server/scripts/ mode=755"
[[email protected] ~]# ansible all -m cron -a "name=‘Rsync Bakcup Scripts‘ hour=01 minute=00 job=‘/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null‘"
[[email protected] ~]# pwd
/root
[[email protected] ~]# mkdir scripts
[[email protected] ~]# cat scripts/rsync_backup_md5.sh
#!/usr/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#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
#4.推送本地数据至备份服务器
export RSYNC_PASSWORD=1
rsync -avz $Path/ [email protected]::backup
#5.本地保留最近7天的数据
find $Path/ -type d -mtime +7|xargs rm -rf
2.应用环境:(配置rsync服务->Backup服务器)
1.安装rsync
2.配置rsync,/etc/rsyncd.conf
3.创建目录,创建虚拟用户文件,变更权限
4.启动服务,加入开机自启动
5.配置邮箱,准备对应的脚本
1.安装rsync
[[email protected] ~]# ansible backup -m yum -a "name=rsync state=installed"
2.配置rsync,/etc/rsyncd.conf
[[email protected] ~]# ansible backup -m copy -a "src=./conf/rsyncd.conf dest=/etc/rsyncd.conf"
[[email protected] ~]# pwd
/root
[[email protected] ~]# mkdir conf
[[email protected] ~]# 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
3.创建目录,变更权限,创建虚拟用户文件
[[email protected] ~]# ansible backup -m file -a "path=/backup state=directory mode=755 owner=www group=www"
[[email protected] ~]# ansible backup -m file -a "path=/data state=directory mode=755 owner=www group=www"
[[email protected] ~]# ansible backup -m copy -a "content=‘rsync_backup:1‘ dest=/etc/rsync.password mode=600 owner=root group=root"
4.启动服务,加入开机自启动
[[email protected] ~]# ansible backup -m service -a "name=rsyncd state=started enabled=yes"
5.配置邮箱,准备对应的脚本
3.应用环境:(配置nfs服务)
1.安装nfs-utils
2.配置nfs-utils
3.创建对应的共享目录,并修改权限
4.启动nfs
1.安装nfs-utils
[[email protected] ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"
2.配置nfs-utils
[[email protected] ~]# ansible nfs -m copy -a "content=‘/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)‘ dest=/etc/exports"
3.创建对应的共享目录,并递归修改权限
[[email protected] ~]# ansible nfs -m file -a "path=/data state=directory recurse=yes owner=www group=www mode=755"
4.启动nfs
[[email protected] ~]# ansible nfs -m service -a "name=nfs-server state=started enabled=yes"
4.应用环境:(配置web服务,挂载操作)
[[email protected] ~]# 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服务器进行校验
4.实时同步
作业:
基于以上的场景,配置好sersync实时同步,验证是否正常
以上是关于Ansible 二的主要内容,如果未能解决你的问题,请参考以下文章