1、主机清单
1、基于密码的方式
[root@localhost ~]
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web02]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[root@localhost ~]
[root@localhost ~]
ping所有
[root@localhost ~]
ansible_ssh_user :用户名
ansible_ssh_port :端口
ansible_ssh_pass :密码
2、基于变量密码的方式
[root@localhost ~]
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
[web01:vars]
ansible_ssh_pass='1'
3、一个分组配置多主机
[root@localhost ~]
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
[web01:vars]
ansible_ssh_pass='1'
4、基于密钥的方式登录
生成密钥:
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
5、分组组合
[root@localhost ~]
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
[web02]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web:children]
web01
web02
[root@localhost ~]
2、ansible的模块
1、ansible-hoc 临时命令
2、ansible-hoc 返回结果
绿色: 代表被管理端主机没有被修改
黄色: 代表被管理端主机发现变更
红色: 代表出现了故障,注意查看提示
2.1、command模块
command模块是ansible中默认模块。
参数:
chdir : 运行命令的目录
creates:在执行命令之前,判断是否已经存在该路径
案例:
使用 command 模块创建100个文件夹
[root@localhost test]
2.2、shell模块
shell 模块跟 command 模块功能非常相似,都是执行命令的模块;但是shell模块支持特殊符号,性能没有 command 模块高。
参数:
chdir : 运行命令的目录
removes :判断一个文件是否存在,存在则运行。
[root@localhost 1]
creates:在执行命令之前,判断是否已经存在该路径
案例:
将 /etc 目录中文件名称包含 root 的文件打包到 /tmp 目录
[root@localhost ~]
3.3、script模块
script模块主要是用来执行脚本文件的。
参数:
chdir : 运行命令的目录
removes :判断一个文件是否存在,存在则运行。
creates :在执行命令之前,判断是否已经存在该路径
[root@localhost ~]
3.4、yum模块
安装软件包的模块。
参数:
name : 软件包名称
state :指定 yum 模块运行的状态
利用 yum 模块,安装部署mariadb
[root@localhost ~]
3.5、yum_repository模块
yum仓库模块
参数:
baseurl : 仓库地址
description : 仓库描述
enabled : 是否启用
gpgcheck :是否验证gpg key
gpgkey :gpgkey 验证地址
name : 仓库名称
[root@localhost yum.repos.d]
案例:
卸载所有版本的Nginx,使用官方仓库安装Nginx
[root@localhost yum.repos.d]
[root@localhost yum.repos.d]
[root@localhost yum.repos.d]
[root@localhost yum.repos.d]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类