网站首页 > 技术文章 正文
一、简介
Ansible是一个自动化统一配置管理工具,有如下特点:
- 远程执行
- 批量配置软件服务
- 事件驱动
- 便于二次开发
- 跨平台、跨系统
官网地址:
https://www.ansible.com
二、安装Ansible
系统需求:
- 一个控制节点,支持Python2.7+或Python3.5+的系统,如: Red Hat, Debian, CentOS, macOS, any of the BSDs 等。
- 受控端,通过ssh/sftp(也可以使用scp)与控制节点连接,同样需要支持Python2.7+或Python3.5+
- 如果受控端启用了SELinux,那需要安装libselinux-python
# 安装epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 安装ansible
yum install ansible -y
ansible --version
三、Inventory资产清单
Inventory通过配置文件来记录被控制节点,清单默认位置:
/etc/ansible/hosts。
命令行可以通过-i来指定hosts的位置:
ansible -i ./hosts
Inventory支持多种格式,如:
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
# 未分组主机
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
# 分组到 webservers
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
# 使用通配符
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
# Here's another example of host ranges, this time there are no
# leading 0s:
# 一个主机段
## db-[99:101]-node.example.com
可以给主机定义别名、指定端口号,如:
[webs]
web01 ansible_ssh_host=192.168.1.3 ansible_ssh_port=22
五、 模块
ansible使用模块来完成安装软件、复制软件等大部分任务。
1. ping
ansible webs -m ping
ping所有主机
ansible all -m ping
2. command来执行简短指令
ansible all -m command -a "echo Heelo World"
3. 使用shell模块
ansible webs -m shell -a 'ls /home'
其它可选参数:
-b - “成为”,在运行命令时告诉可以成为另一个用户。
--become-user=root - 以用户“root”运行以下命令(例如,使用命令使用“sudo”)。我们可以在此定义任何现有的用户。
-a 用于将任何参数传递给定义的模块 -m
4. Debian系操作系统使用apt模块
ansible -i ./hosts local --connection=local -b --become-user=root \
-m apt -a 'name=nginx state=installed update_cache=true'
- 上一篇: Go 并发可视化解释 — 通道
- 下一篇: 一文带你了解Ansible
猜你喜欢
- 2024-12-01 Ansible的原理与配置
- 2024-12-01 Ansible系列教程-playbook详解(三)
- 2024-12-01 prometheus实战之一:用ansible部署
- 2024-12-01 ansible快速入门手册
- 2024-12-01 学习下运维自动化工具ansible
- 2024-12-01 Ansible 批量100台服务器添加 Crontab
- 2024-12-01 利用Ansible自动化工具对多台linuxz主机配置网卡文件
- 2024-12-01 如何在 Rocky Linux 8 上安装 Ansible 自动化工具?
- 2024-12-01 Ansible自动化部署利器的实战运用
- 2024-12-01 Ansible自动化运维
- 1509℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 523℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 491℃MySQL service启动脚本浅析(r12笔记第59天)
- 470℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 468℃启用MySQL查询缓存(mysql8.0查询缓存)
- 448℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 428℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 425℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)