03-Ansible模块

257 阅读2分钟

copy模块

# 文件自动生成
ansible all -m copy -a "src=/opt/copy.txt dest=/opt/copy.txt owner=root group=root mode=644 backup=true"
# 目录copy,src必须是目录,如果dest目录不存在或者/结尾,则会自动创建目录
ansible all -m copy -a "src=/opt/copy dest=/opt/ owner=root group=root directory_mode=755 backup=true"

fetch模块

# 和copy模块相反,从客户端拉取文件至服务端
# fetch 只支持文件,并且从客户端拉取过来会在本地生成客户端的ip命名的文件夹,flat参数可以解决这个问题
ansible all -m fetch -a "src=/opt/copy/123.txt dest=/opt/"

file模块

# file模块常用于设置文件权限,创建软链,删除文件
ansible all -m file -a "path=/opt/copy/123.txt owner=root group=root mode=777" # 设置文件权限
ansible all -m file -a "src=/opt/copy/123.txt dest=/usr/bin/123 state=link" # 创建软链接
ansible all -m file -a "path=/opt/copy/123.txt state=absent" # 删除文件

hostname模块

# hostname注意需要结合参数一起使用,下面的命令永远不要执行
ansible all -m hostname -a "name=kube-master" 

cron模块:定时任务

# cron支持:minute、hour、day、month、week
# cron支持参数:backup-备份,disabled-禁止任务,需要把job打出来,env-支持参数,kv键值对,reboot-需要重启才能执行的任务
# state-设置变量或者job可用/不可用,user-执行任务的用户
ansible all -m cron -a "name=timedtask user=root minute=*/1 job='/usr/bin/echo 123 >> /opt/copy/123.txt'"
ansible all -m cron -a "name=timedtask job='/usr/bin/echo 123 >> /opt/copy/123.txt' state=absent"

yum模块:安装软件

# 安装软件,disable_gpg_check禁止gpg检测
ansible all -m yum -a "name=vsftpd disable_gpg_check=true state=latest"
ansible all -m yum -a "name=vsftpd state=absent" # 删除应用

Service模块:启动服务

# enabled=true 开机自启动,runlevel-启动级别
ansible all -m service -a "name=vsftpd enabled=true state=started" # state状态还有stopped,reloaded,restarted

User模块:管理用户

# 用户支持参数比较多,根据实际情况使用
ansible all -m user -a "name=devil group=devil groups=root shell=/sbin/nologin uid=100 system=true home=/opt/devil state=present "

Group模块: 管理组

ansible all -m group -a "name=devil gid=100 system=yes state=present" # state=absent 表示删除用户