ansible核心模块
模块介绍
模块分类 |
ansible-doc -l 显示所有模块名 |
命令和脚本模块 |
command模块 默认的模块 执行简单命令,不支持特殊符号 |
|
shell模块 执行命令,支持特殊符号 |
|
script模块 分发脚本并执行 |
文件 |
file 创建目录,文件,软连接 |
|
copy 远程分发文件,修改权限,所有者,备份 |
服务 |
systemd 服务管理 |
|
service 服务管理(了解) |
软件包 |
yum源 yum_repository |
|
yum 命令 |
|
get_url 下载软件 |
系统管理 |
mount模块 挂载 |
|
cron模块 定时任务 |
用户管理 |
group模块 管理用户组 |
|
user模块 管理用户 |
其他可以研究 |
压缩解压(unarchive),rsync模块(synchronize),数据库模块(mysql_db,mysql_user)…… |
其他 |
ansible管理docker k8s zabbix grafana …… |
用于调试模块 |
ping 模块检查ansible与其他节点连通性 |
|
debug 模块 用于检查/显示 变量 |
如何查询帮助
通过命令
ansible-doc -s 模块名
通过官网
https://docs.ansible.com/ansible/latest/
ans-命令与脚本类模块
ansible中的颜色:
绿色表示正常
黄色表示执行正常,状态变化
红色表示错误,输出错误信息
紫色表示警告,建议排查对应问题
1)command模块
# 仅支持简单命令,不支持特殊符号,如管道符、星号等等……
# 这个模块时默认模块,ansible不加上模块,默认使用这个模块
ansible all -m command -a '命令'
ansible all -a '命令'
2)shell模块
# 与command模块类似,shell模块支持特殊符号
ansible all -m shell -a '命令'
例如:
ansible all -m shell -a 'ip a s eth0|sed -n 3p'
3)script模块-传输脚本到被管理端并执行
# 传输脚本
# 执行脚本
ansible db -m script -a '脚本的绝对路径'
例如:
安装ipvsadm
#yum.sh内容
yum install -y ipvsadm
ansible db -m script -a '/data/software/shell/yum.sh'
4)命令与脚本模块
模块 |
含义 |
应用 |
command模块 |
执行命令 |
简单命令,不含特殊符号,默认的模块 ansible all -a ‘hostname’ |
shell模块 |
执行命令或脚本 |
执行含有特殊符号的命令,或执行脚本(脚本需要在被管理端) |
script模块 |
先传输脚本,然后运行脚本 |
一般用于执行脚本 |
Ans-文件与目录管理模块
file 创建文件,目录
copy 远程传输文件,目录,类似于scp
1)file模块
file模块中的选项 |
|
path |
路径(目录,文件)必须要写 |
src |
源文件 一般用于link(创建软连接模式)用于指定源文件 |
state |
状态(模式),state=directory 创建目录,state=file(默认) 更新文件,如果文件不存在也不创建,state=link 创建软连接,state=touch 创建文件,state=absent 删除 |
创建目录
ansible all -m file -a 'path=/oldboy state=directory'
ansible all -a 'ls -ld /oldboy'
创建文件
ansible all -m file -a 'path=/oldboy/oldbboy.txt state=touch'
ansible all -a 'ls -l /lodboy'
创建软连接
ansible all -m file -a 'src=/oldboy/oldboy.txt path=/tmp/oldboy.txt.soft state=link'
ansible all -a 'ls -l /tmp/oldboy.txt.soft
删除目录/文件/软连接
ansible all -m file -a 'path=/oldboy state=absent' #删除目录
ansible all -m file -a 'path=/oldboy/oldboy.txt state=absent' #删除文件
ansible all -m file -a 'path=/tmp/oldboy.txt.soft state=absent' #删除软连接
创建文件/tmp/oldboy.txt, 所有者root 用户组root 权限755
ansible all -m file -a 'path=/tmp/oldboy.txt owner=root group=root mode=755 state=touch'
ansible all -a 'ls -l /tmp/oldboy.txt.soft
2)copy远程传输模块
copy模块 |
|
src |
source 源文件 |
dest |
destination 目标 |
backup |
backup=yes 则会在覆盖前进行备份 |
mode |
修改权限 |
owner |
修改为指定所有者 |
group |
修改为指定用户组 |
传输/etc/hosts文件到/etc/hosts
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts'
ansible all -a 'tail -2 /etc/hosts'
Ans-服务管理模块
systemctl 命令
启动/关闭/重启服务
开机自启动/开机不自启动
1)systemd
systemd模块 |
|
name |
用于指定服务名称 |
enabled |
yes开机自启动 |
state |
表示服务开,关,重启… ,state=started 开启, state=stopped 关闭, state=reloaded 重读配置文件(服务支持) 很少支持,如sshd,nfs支持, state=restarted 重启(关闭再开启) |
daemon_reload |
yes 是否重新加载对应的服务的管理配置文件 |
关闭firewalld
关闭正在运行的
关闭开机自启动
ansible all -m systemd -a 'name=firewalld enabled=no state=stopped'
ansible all -a 'systemctl status firewalld'
开启sshd服务
ansible all -m systemd -a 'name=sshd enabled=yes state=started'
ansible all -a 'systemctl status sshd'
重启backup这台机器上面的rsync服务
ansible backup -m systemd -a 'name=rsyncd state=restarted'
2)service (了解
服务管理模块 |
systemd模块 |
service模块 |
开机自启动 |
enabled |
enabled |
服务名称 |
name |
name |
服务开关重启 |
state |
state |
运行级别 |
无 |
runlevel |
重启加载systemctl配置 |
daemon_reload=yes 或者 systemctl daemon-reload |
无 |
3)服务管理模块小结
模块 |
|
systemd |
管理linux 红帽7以上推荐使用,Ubuntu等…… |
service |
6及以下 |
Ans-软件包管理模块
yum_repository(管理yum源)
yum(yum命令)
get_url(wget命令)
1)yum源配置管理模块
yum源模块 yum_repository |
yum源配置文件内容 |
|
name |
[epel] |
yum源中名字 (中括号里面的名字即可) |
description |
name=xxxxxx |
yum源的注释说明 对应的 是name的内容 |
baseurl |
baserurl= |
yum源中下载地址的地址(可以访问到repodata目录) |
enabled=yes或no |
enabled=1或0 |
是否启动这个源 yes/no |
gpgcheck=yes或no |
gpgcheck=0 |
是否启动gpgcheck功能 no |
file(可以不加) |
无 |
指定yum源的文件 自动添加.repo 例如file=xxx 默认与模块名字一致 |
批量添加nginx-yum源
[nginx]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
ansible web -m yum_repository -a 'name=nginxdescription="nginx stable repo" baseurl="http://nginx.org/packages/centos/$releasever/$basearch/" gpgcheck=noenabled=yes'
# 注意未来也可以通过copy模块实现
2)yum模块
通过yum命令安装软件
yum模块 |
|
name |
指定软件包名字 |
state |
installed 安装(present), removed删除(absent), latest安装或更新 |
安装lrzsz
ansible all -m yum -a 'name=lrzsz state=installed'
安装多个
ansible web -m yum -a 'name=sl,cowsay,aalib state=installed'
sl --命令sl 跑火车
cowsay --命令animalsay "文字" 动物字符画
aalib --命令aafire 火焰
3)get_url模块(wget)
get_url下载功能 |
|
url |
指定要下载的地址 |
dest |
下载到哪个目录 |
ansible web -m file -a 'path=/server/tools/ state=directory'
ansible web -m get_url -a 'url=https://tengine.taobao.org/download/tengine-2.3.3.tar.gz dest=/server/tools/'
ansible web -a 'tree /server/tools/'
Ans-系统管理模块
mount 挂载nfs模块
cron 管理定时任务
1)mount模块
mount |
|
fstype |
指定文件系统 |
src |
源地址(nfs地址 eg: 192.168.26.103/data) |
path |
挂载点(要把源挂载到哪里) |
state |
参考下表 |
mount模块的state参数可使用的值 |
|
absent |
卸载并修改fstab |
unmounted |
卸载不修改/etc/fstab |
present |
仅修改/etc/fstab 不挂载 |
mounted |
挂载(mount命令)并修改/etc/fstab (永久挂载) |
remounted |
重新挂载 |
web01 把 nfs共享的目录/data目录挂载到 web01的/upload_video
步骤01 web01上创建挂载点/upload_video
ansible web -m file -a 'path=/upload_video state=directory'
步骤02 挂载nfs
ansible web -m mount -a 'fstype=nfs src="192.168.26.103:/data" path=/upload_video state=mounted '
由于nfs那台实际没有nfs共享目录,所以会挂载失败
ansible web -a 'df -h'
ansible web -a 'tail -2 /etc/fstab'
2)cron定时任务模块
cron模块 |
定时任务配置中的内容 |
|
name |
#及后面的内容 |
定时任务名字(一定要加上),对应下面注释的内容 |
minute |
*/2 |
分钟 minute=”*/2”,如果没有用到不用填写即可。 |
hour |
|
小时 |
day |
|
日期 |
month |
|
月份 |
week |
|
周几 |
job |
命令/脚本 |
指定命令或脚本(定向到空) job=”/sbin/ntpdate ntp1.aliyun.com &>/dev/null” |
state |
|
present 添加定时任务 (默认是添加) absent 删除 |
#1 sync time
*/2 * * * * /sbin/ntpdate ntp1.aliyun.com &>/dev/null
步骤01 备份数据
ansible all -a 'cp /var/spool/cron/root /tmp/' a n s ibl e a l l - a ' l s -l /tmp/root'
ansible all -a 'ls -l /tmp/root/'
ansible all -a 'crontab -r'
步骤02 书写定时任务
ansible all -m cron -a 'name="sync time by lidao 996 20221111" minute="*/2" job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null" state=present'
Ans-用户管理模块
group 创建组模块
user 创建用户模块
1)user
user模块 |
|
name |
wwww 用户名 |
uid |
指定uid |
group |
指定用户组 |
shell |
指定命令解释器 |
create_home |
是否创建家目录(yes/no) |
state |
present 添加 absent删除 |
创建用户lidao996
ansible web -m user -a 'name=lidao996'
创建虚拟用户tengine,指定uid为10086
命令解释器:/sbin/nologin
不创建家目录
ansible web -m user -a 'name=tengine uid=190086 shell=/sbin/nologin create_home=no state=present'
2)group模块
group |
|
name |
指定用户组名字 |
gid |
指定组的gid |
state |
present添加 absent 删除 |