模块分类 | 模块 |
---|---|
命令和脚本 | 模块 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 -m 指定模块 -a 指定模块中的选项
命令与脚本类模块
command模块
是ansible默认的模块,适用于执行简单的命令,不支持特殊符号
用法案例
- 批量获取所有主机的主机名
ansible all -m command -a ‘ip a s eth0’
shell模块
与command模块类似,但是shell支持特殊符号
用法案例
- 批量获取ip地址
ansible all -m shell -a "ip a s eth0 |awk -F’[ /]+’ ‘NR==3{print $3}’”
⚠️shell模块不推荐执行较为复杂的指令,如果需要执行放在脚本中执行
script模块
分发脚本, 运行脚本
用法案例
ansible all -m script -a ‘脚本地址’
文件相关模块
file模块
管理文件,管理目录,软连接
file模块 | 模块说明 |
---|---|
path | 路径(目录,文件) 必须要写 |
src | 源文件一般用于link(创建软连接模式) 用于指定源文件 |
state | 状态(模式) state=directory 创建目录 |
state=file (默认) | 更新文件,如果文件不存在也不创建 |
state=link | 创建软连接 |
state=touch | 创建文件 |
state state=absent | 删除 (注意如果是目录递归删除目录 ) mode mode=755创建并修改权限 |
onwer | onwer=root |
group | group=root |
案例
创建文件
ansible all -m file -a 'path=/opt/test.txt state=touch
创建目录
ansible all -m file -a ‘path=/app/a/b/c/ state=directory’
创建软连接 /etc/hosts创建软连接到/opt/下
ln -s /etc/hosts /opt/hosts
ansible all -m file -a ’ src=/etc/hosts path=/opt/hosts state=link ’
创建/demo 目录 所有者是test
ansible all -m file -a ‘path=/demo/ owner=test group=test mode=700 state=directory’
删除
ansible all -m file -a ‘path=/demo/ state=absent’
copy模块
copy模块 | |
---|---|
src | source 源文件 |
dest | destination 目标 |
backup backup=yes | 则会在覆盖前进行备份 |
mode | 修改权限 |
owner | 修改为指定所有者 |
group | 修改为指定用户组 |
案例
ansible all -m copy -a ‘src=/etc/hosts dest=/etc/hosts backup=yes’
服务管理-systemd
#启动服务
ansible all -m systemd -a ‘name=crond enabled=yes state=started’
#关闭服务
ansible all -m systemd -a ‘name=firewalld enabled=no state=stopped’
#重启ssh
ansible all -m systemd -a ‘name=sshd state=reloaded/restarted’
软件管理
yum模块
yum模块 | |
---|---|
name | 指定软件包名字 |
state | installed 安装(present)(默认) removed 删除 (absent) lastest 安装或更新 |
update_cache | 可以设置为no加加速,表示不更新本地yum缓存.实际应用建议开启 |
ansible all -m yum -a 'name=htop,tree,lrzsz ’
** get_url模块**
cget_url下载功能 | |
---|---|
url | 指定要下载的地址 |
dest | 下载到哪个目录 |
https: ˌtengine.taobao.org/download/tengine-2.3.3.tar.gz下载到/app/tools目录下面
ansible all -m file -a ‘path=/app/tools/ state=directory’
ansible all -m get_url -a ‘url=“https://tengine.taobao.org/download/tengine- 2.3.3.tar.gz” dest=/app/tools/’
** yum_repository模块**
yum源模块 yum_repository name | |
---|---|
yum | 源中名字 [epel] |
description | yum源的注释说明 对应的 是name的内容 |
baseurl | yum源中 baseurl 下载地址 |
enabled | 是否启动这个源 yes/no |
gpgcheck | 是否启动gpgcheck功能 no |
file | 指定yum源的文件 自动添加.repo 默认与模块名字一致 |
书写好,yum配置文件,copy分发过去即可
root@m01 ~]# cat /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:// etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
-m yum_repository
-a 'name=epel
description="Extra Packages for Enterprise Linux 7 - $basearch"
baseurl="http://mirrors.aliyun.com/epel/7/$basearch"
enabled=yes
gpgcheck=no '
用户管理
案例
创建www-ans用户uid 2000虚拟用户
ansible all -m user -a ‘name=www-ans uid=2000 shell=/sbin/nologin create_home=no state=present’
批量更新密码
ansible all -m shell -a ‘echo 1 |passwd --stdin demo’