27.ansible基本模块

ansible 和saltstack对比

 SaltStack 底层有一个消息队列:Zero-MQ(message queue)

# 1.ansible是基于ssh协议来工作的,但是saltstack也有支持ssh协议的模式

# 2.但是一般来说,选择saltstack就是为了使用客户端和服务端的方式(速度快)

# 3.因为saltstack底层有一个zmq做消息队列

# 4.ansible基于ssh,如果ssh服务挂了,ansible也不能工作,saltstack可以,会起两个端口4505,4506

Ansible命令模块

command

shell

script

Ansible软件管理模块

  • yum
name: 服务名
state:
	present:安装
	absent:卸载
	latest:安装最新版本
  • yum_repository
name:指定仓库名字,如果没有配置file,则文件名和仓库名一致
file:指定文件名
baseurl:指定源
gpgcheck:指定是否检查秘钥
	yes:检查
	no:不检查
description:指定对仓库的描述,也就是repo文件中的name
enabled:是否开启仓库
	yes:开启
	no:不开启
state:
	present:添加仓库
	absent:删除仓库

ansible文件管理模块

copy

[root@m01 ~]# ansible backup -m copy -a 'src=/root/zls_xxx.conf dest=/etc/rsyncd.conf owner=root group=root mode=0644'

src:指定源文件位置(管理机上的文件)
dest:指定你要推送到主机的目标位置
owner:指定属主
group:指定属组
mode:指定权限
backup:是否备份,第一次推,没有每份,对端机器存在该文件,并且内容不一致,才会做备份
	yes:推送之前,先备份目标主机的源文件
	no:不备份
remote_src:源文件是否在远端的机器上
	yes:是
	no:否
content:往指定目标文件中写入内容

file

作用:

  • 授权
  • 创建目录
  • 创建文件
  • 创建软连接
  • 删除目录,文件,软连接
[root@m01 ~]# ansible all -m file -a 'path=/opt/test/zls owner=www group=www mode=0722 state=directory'
[root@m01 ~]# ansible all -m file -a 'path=/code owner=www group=www recurse=yes'

path:指定文件或目录的路径
owner:指定属主
group:指定数组
mode:指定权限
src:做 软/硬 链接的时候使用,指定源文件
recurse:是否递归授权
	yes:递归授权
	no:仅授权当前目录
state:
	directory:创建目录
	touch:创建文件
	link:做软链接
	hard:做硬链接
	absent:删除
	file:配合 modification_time  access_time  修改文件的属性,stat

get_url

类似于:wget

[root@m01 ~]# ansible backup -m get_url -a 'url=http://test.driverzeng.com/Nginx_File/nginx.txt dest=/root checksum=md5:8f8dd0f79bc6ef2148ca3494070a86a1'

url:指定下载文件的地址
dest:指定下载的路径
checksum:指定加密的算法
	sha256
	md5

ansible服务管理模块

service、systemd

[root@m01 ~]# ansible 'c6,backup' -m service -a 'name=crond state=stopped'
name:指定服务名称
state:
	started:启动服务
	stopped:停止服务
	restarted:重启服务
	reloaded:重新加载服务
enabled         #开机自启

ansible用户管理模块

group

[root@m01 ~]# ansible all -m group -a 'name=xxxx gid=10010 state=present'

name:指定组名
gid:指定组id
state:
	present:创建
	absent:删除

user

[root@m01 ~]# ansible all -m user -a 'name=zlsqqq uid=10201 group=root shell=/sbin/nologin create_home=false'

name:指定用户名
uid:指定uid       -u
group:只能指定组名,不能指定gid     -g
shell:指定登录的方式   -s
create_home:是否创建家目录
	true,yes:创建
	false,no:不创建
comment:指定注释   -c
groups:指定附加组(配合append,如果不加append覆盖) -G
append:创建附加组的时候,追加 -a
remove:删除用户的时候,是否同时删除家目录和邮件文件
	true,yes:删除
	fasle,no:不删除
state
	present:创建
	absent:删除

generate_ssh_key:是否创建秘钥对
	yes:创建
	no:不创建
ssh_key_bits:指定秘钥对加密长度
ssh_key_file:指定私钥文件的位置
system:是否是系统用户  -r
	yes:是系统用户
	no:不是系统用户

ansible定时任务模块

cron

# 创建
[root@m01 ~]# ansible all -m cron -a "name='sync time' minute=*/5 job='ntpdate time1.aliyun.com &>/dev/null'"

# 删除(删除是根据注释来删除的 name)
[root@m01 ~]# ansible all -m cron -a "name='time'  state=absent"

name:指定定时任务的名字(添加一个备注)
state:
	present:创建定时任务
	absent:删除定时任务

minute:分 (0-59) */5     10-20     10,20
hour:时(0-23)
day:日(1-31)
month:月(1-12)
weekday:周(0-6)

ansible磁盘挂载模块

mount

[root@m01 ~]# ansible web_group -m mount -a 'path=/mnt src=10.0.0.31:/web_data fstype=nfs state=mounted'

path:挂载到本地的目录
src:对端目录
fstype:文件系统类型
	nfs
	ext4
	ext3
state:
	present:只写入开机自动挂载的文件中,不挂载
	mounted:既写入文件,又挂载
	
	absent:卸载设备,并且清理开机自动挂载文件
	unmounted:只卸载不清理文件

推荐:
	- 挂载的时候:mounted
	- 卸载的时候:absent

ansible关闭selinux模块

[root@m01 ~]# ansible all -m selinux -a 'state=disabled'
state:
	enforcing
	permisive
	disabled

ansible防火墙模块

# 开放端口的写法
[root@m01 ~]# ansible all -m firewalld -a 'port=443/tcp permanent=no state=enabled'

# 开放服务名称的写法
[root@m01 ~]# ansible all -m firewalld -a 'service=http permanent=no state=enabled'


[root@nfs ~]# firewall-cmd --list-all      # 查看防火墙状态

猜你喜欢

转载自www.cnblogs.com/jkz1/p/13170830.html