day16.1

Ansible常用模块

看ansible的帮助手册

# [root@m01 ~]# ansible-doc 模块名

AANnsible命令相关模块

command

# ansible all -m command -a '系统命令'	# 不支持特殊符号

shell

# ansible all -m shell -a '系统命令'	# 不支持特殊符号

script

# ansible管理节点上的脚本执行到管理端
ansible all -m 'script' -a '脚本路径'

Ansible文件管理模块

file模块

# 动作
path	# 指定文件或者目录的路径
owner	# 指定文件或目录的属主
group	# 指定文件或目录的属组
mode	# 指定文件或目录的权限
src		# 做软链接或硬链接时要指定的源文件
dest	# 做软链接或硬链接时要指定的目标路径
recurse	# 递归
state	# 状态
	- absent	# 删除path指定的文件或者目录
	- touch		# 创建path指定的文件
	- directory	# 创建path指定的目录
	- link		# 创建src指定的源文件做软链接到dest指定的,目标路径
	- hard		# 创建src指定的源文件做硬链接到dest指定的目标路径

[root@m01 ~]# ansible web_group -m file -a 'path=/data owner=www group=wwwstate=directory'

copy模块

# 作用 :统一管理配置文件

# 动作
src		# 指定源文件路径
dest	# 指定目标路径
owner	# 指定属主
group	# 指定属组
mode	# 指定权限
content	# 指定内容写入文件
backup	# 备份
	- yes/true	# 备份
	- no/false	# 不备份 (默认)
remote_src	# 远端的源文件
	- yes/true
	- no/false

# 将管理端的配置文件下发到被管理端
[root@m01 ~]# ansible web_group -m copy -a 'src=/root/cjk.txt dest=/opt'

# 使用backup做备份,会生成备份文件
[root@m01 ~]# ansible web_group -m copy -a 'src=/root/cjk.txt dest=/opt backup=yes'

[root@web01 ~]# ll /opt/
total 8
-rw------- 1 root root  9 Jun 27 15:04 cjk.txt
-rw------- 1 root root 11 Jun 27 15:03 cjk.txt.16181.2

# 指定属主和属组
ansible web_group -m copy -a 'src=/root/cjk.txt dest=/opt owner=www group=www mode=644 backup=yes'

# remote_src指定源文件在远端
[root@m01 ~]# ansible web_group -m copy -a 'src=/opt/cjk.txt dest=/tmp remote_src=true'

# content往指定文件写入内容
[root@m01 ~]# ansible web_group -m copy -a 'dest=/opt/2.txt content='123' mode=600'

[root@web01 ~]# cat /opt/2.txt 
123

get_URL

# 动作
URL		# 下载网址
dest	# 下载的路径
mode	# 指定权限

# 下载WordPress到/opt下并指定权限
[root@m01 ~]# ansible web_group -m get_url -a 'url="http://test.driverzeng.com/Nginx_Code/QQ2.8.zip" dest=/opt mode=777"'

Asible软件管理模块

yum模块

# 动作
name	# 安装包的名字
	- http://	从指定URL下载
	- file://	从本地rpm包安装
	- 包名       从yum仓库中下载
	
state
	- absent/removed	# 卸载
	- present/installed	# 安装
	- latest			# 安装最新版本
	
download_only		# 只下载不安装

# 安装rsync
[root@m01 ~]# ansible web_group -m yum -a 'name=rsync'

yum_repository

# 管理yum源,yum仓库

# 动作
name	# 仓库名字
description 	# 仓库描述信息
baseurl	# 仓库URl地址
file	# 如果没有指定file则文件名和name指定的一致,文件名file指定的内容,仓库名为name指定的内容
owner	# 指定属主
group	# 指定数组
mode	# 指定权限
gpgcheck # 密钥对检测
	- yes/true	gpgcheck=1
	- no/false	gpgcheck=0
enabled	# 是否开启仓库
	- yes/true enable=1
	- no/false enable=0
state
	- present	# 创建仓库
	- absent	# 删除仓库
	
# 使用file指定yum源的文件名
ansible web_group -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=false enabled=true file=nginx'

[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
enabled = 1
gpgcheck = 0
name = nginx stable repo

# 删除仓库
[root@m01 ~]# ansible web_group -m yum_repository -a 'name=nginx-stable file=nginx state=absent'

Ansible服务管理模块

server、systemd

# 管理服务启停

# 动作
name	# 指定服务名字
state
	- started	# 开启服务
	- reloaded 	# 重新加载服务
	- stopped	# 停止服务
	- restarted	# 重启服务

enable	# 开机自启
	- yes/true	# 加入开机自启
	- no/false	# 不加入开机自启(默认)
	
# 启动nginx并加入开机自启
[root@m01 ~]# ansible web_group -m service -a 'name=nginx state=started enabled=true'

Ansible用户管理模块

user

# 动作
name	# 用户名
comment	# -c 指定用户描述信息
uid		# -u 指定用户的uid
group		# -g 指定用户的gid
shell	# -s 指定用户登录的shell -s /sbin/nologin
append	# -a 指定附加组并追加附加组
groups	# -G 指定用户的附加组
state	
	- absent 删除用户
	- present 创建用户
remove
	- yes/true	删除用户和用户相关文件
	- no/false	默认
ssh_key_bits	# 创建用户时,创建私钥,私钥位数 2048
ssh_key_file	# 指定私钥位置
create_home
	- yes/true	# 创建用户同时创建家目录	默认
	- no/false	# 创建用户不创建家目录

# 创建www用户禁止用户登录,不创建家目录
[root@m01 ~]# ansible web_group -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=false'

# 删除用户
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent remove=yes'

group

# 管理用户组

# 动作
name	# 指定组名字
gid		# 指定组gid
state
	- present 	创建组 默认
	- absent 	删除组

# 创建www组并且gid是666
[root@m01 ~]# ansible web_group -m group -a 'name=www gid=666'

Ansible定时任务模块

# 动作
name	# 定时任务注释信息
minute	# 分
hour	# 时
day		# 日
month	# 月
weekday	# 周
job		# 执行的任务
state
	- present	# 创建定时任务(默认)
	- absent	# 删除定时任务
	
# 创建定时同步任务
[root@m01 ~]# ansible web_group -m cron -a 'name=time minute=00 hour=01 job="/usr/bin/ntpdate timel.aliyun.com &>/dev/null"'

[root@web01 ~]# crontab -e
#Ansible: time
00 01 * * * /usr/bin/ntpdate timel.aliyun.com &>/dev/null

# 删除定时任务
[root@m01 ~]# ansible web_group -m cron -a 'name=time state=absent'

Ansible磁盘挂载模块

# 动作
path	# 挂载路径
src		# 挂载源
fstype	# 文件类型
state
	- present	# 只将挂载信息记录在/etc/fstab中(开机挂载)
	- mounted	# 立刻挂载,并将配置文件写入/etc/fstab中
	- unmounted	# 卸载设备,但是不会清楚/etc/fstab中的内容
	- absent	# 卸载设备,并清除/etc/fastab中的内容
opts	# 指定挂载路径是否可读可写

# 挂载nfs
[root@m01 ~]# ansible web_group -m mount -a 'path=/code/wordpress/wp-content/uploads
src=172.16.1.31:/data fstype=nfs state=mounted'

搭建rsync

作业: 

1.部署rsync 

2.部署nfs 

3.部署httpd,将zuoye代码部署进去,挂载上传作业的目录

编辑sync配置文件

```bash
[root@m01 ~]# vim /etc/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.passwd
log file = /var/log/rsyncd.log
```

```bash
# 部署rsync服务端(backup)
#1.安装rsync
ansible backup -m yum -a 'name=rsync' 

#2.修改配置文件
ansible backup -m copy -a 'src=/etc/rsyncd.conf dest=/etc/'

#3.创建密码文件
ansible backup -m copy -a 'dest=/etc/rsync.passwd content=rsync_backup:123456 mode=600'

#4.创建www用户
ansible backup -m group -a 'name=www gid=666'
ansible backup -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=false'

#5.创建备份目录和实时同步目录
ansible backup -m file -a 'name=/backup owner=www group=www state=directory'
ansible backup -m file -a 'name=/data owner=www group=www state=directory'

#6.启动服务
ansible backup -m service -a 'name=rsyncd state=start enabled=true'

#部署rsync客户端(nfs)
#安装rsync和inotify
ansible nfs -m yum -a 'name=rsync,inotify-tools'

#1.创建密码文件
ansible nfs -m copy -a 'content="123456" dest=/etc/rsync.passwd mode=600'

#部署nfs服务端(nfs)
#1.安装nfs服务
ansible nfs -m yum -a 'name=nfs-utils'

#2.修改nfs配置文件
ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)" dest=/etc/exports'

#3.创建www用户和组
ansible nfs -m group -a 'name=www gid=666'
ansible nfs -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=false'

#4.创建共享目录
ansible nfs -m file -a 'name=/data owner=www group=www state=directory'

#5.启动nfs并加入开机自启
ansible nfs -m service -a 'name=nfs state=start enable=true'

#部署nfs客户端(web01、web02)
#安装nfs
ansible web_group -m yum -a 'name=nfs-utils'

#部署web网站(web01、web02)
#1.安装httpd和php
ansible web_group -m yum -a 'name=httpd,php'

#2.修改httpd的配置文件
ansible web_group -m copy -a 'src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/'

#3.创建www用户和组
ansible web_group -m group -a 'name=www gid=666'
ansible web_group -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=false'

#4.部署代码
ansible web_group -m copy -a 'src=/kaoshi dest=/var/www/html/'

# 5.创建用户上传目录并授权
ansible web_group -m file -a 'name=/var/www/html/user_data owner=www group=www state=directory'

# 6.启动httpd服务
ansible web_group -m service -a 'name=httpd state=started enabled=ture'

# 7.挂载用户上传数据目录
ansible web_group -m mount -a 'path=/var/www/html/user_data src=172.16.1.31:/data fstype=nfs state=mounted'

#实时同步脚步
ansible nfs -m copy -a 'src=/inotify.sh dest=/inotify.sh'

#创建实时同步定时任务
ansible nfs -m cron -a 'name="data_sync" minute=00 hour=00 job="/bin/sh /inotify.sh &>/dev/null"'
```
# 添加主机池
[root@m01 ~]# vim /etc/ansible/hosts 

[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8

# 准备rsync配置文件
uid = rsync
gid = rsync
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.pass
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup

# 1.安装rsync
ansible web_01 -m yum -a 'name=rsync'

# 2.修改配置文件
ansible web01 -m copy -a 'src=/root/rsyncd.conf dest=/etc/'

# 3.创建密码文件
ansible web01 -m copy -a 'content="rsync_backup:123456" dest=/etc/rsync.pass mode=600'

# 4.创建www用户
ansible web01 -m group -a 'name=www gid=666'
ansible web01 -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin'

# 5.创建备份目录
ansible web01 -m file -a 'path=/backup owner=www group=www state=directory'

# 6.启动服务
ansible web01 -m service -a 'name=rsyncd state=started enabled=true'

# 7.客户端
ansible web02 -m yum -a 'name=rsync'

# 8.创建密码文件
ansible web02 -m copy -a 'content="123456" dest=/etc/rsync.pass mode=600'
posted @ 2022-06-27 19:50  Gabydawei  阅读(22)  评论(0编辑  收藏  举报