first commit
This commit is contained in:
78
README.md
Normal file
78
README.md
Normal file
@ -0,0 +1,78 @@
|
||||
要使用 Ansible 在 Debian 服务器上进行批量操作,你需要按照以下步骤进行设置:
|
||||
|
||||
### 1. 安装 Ansible
|
||||
|
||||
在你的 Mac 上安装 Ansible。可以通过 Homebrew 来安装,运行以下命令:
|
||||
|
||||
```bash
|
||||
brew install ansible
|
||||
```
|
||||
|
||||
### 2. 配置 SSH 访问
|
||||
|
||||
确保你可以通过 SSH 无密码访问所有 Debian 服务器。你可以生成 SSH 密钥并将公钥添加到每个服务器的 `~/.ssh/authorized_keys` 文件中:
|
||||
|
||||
```bash
|
||||
ssh-keygen -t rsa
|
||||
```
|
||||
|
||||
然后使用 `ssh-copy-id` 将公钥复制到每台服务器:
|
||||
|
||||
```bash
|
||||
ssh-copy-id user@server_ip
|
||||
```
|
||||
|
||||
### 3. 创建 Ansible 主机清单
|
||||
|
||||
在你的 Mac 上创建一个主机清单文件,例如 `hosts.ini`,内容如下:
|
||||
|
||||
```ini
|
||||
[debian_servers]
|
||||
server1_ip
|
||||
server2_ip
|
||||
server3_ip
|
||||
```
|
||||
|
||||
### 4. 测试连接
|
||||
|
||||
使用 Ansible 测试连接是否成功:
|
||||
|
||||
```bash
|
||||
ansible -i hosts.ini debian_servers -m ping
|
||||
```
|
||||
|
||||
如果配置正确,你应该会看到“pong”响应。
|
||||
|
||||
### 5. 编写 Ansible Playbook
|
||||
|
||||
创建一个简单的 Ansible Playbook,例如 `playbook.yml`,来执行你想要的操作。以下是一个示例 Playbook,更新所有软件包:
|
||||
|
||||
```yaml
|
||||
---
|
||||
- hosts: debian_servers
|
||||
become: yes
|
||||
tasks:
|
||||
- name: Update all packages
|
||||
apt:
|
||||
update_cache: yes
|
||||
upgrade: dist
|
||||
```
|
||||
|
||||
### 6. 执行 Playbook
|
||||
|
||||
使用以下命令运行 Playbook:
|
||||
|
||||
```bash
|
||||
ansible-playbook -i hosts.ini playbook.yml
|
||||
```
|
||||
|
||||
### 7. 管理和扩展
|
||||
|
||||
你可以根据需求扩展 Playbook,添加更多的任务,比如安装软件、管理文件等。
|
||||
|
||||
### 参考文档
|
||||
|
||||
- [Ansible 官方文档](https://docs.ansible.com/)
|
||||
- [Ansible 适用于 Debian 的模块](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html)
|
||||
|
||||
通过这些步骤,你就可以在你的 Debian 服务器上使用 Ansible 进行批量操作了。
|
Reference in New Issue
Block a user