1. 安装 Ansible
1. 通过源码包安装
步骤:
- 下载 Ansible 源码包:
wget https://kkgithub.com/ansible/ansible/archive/refs/tags/v2.9.0.zip
- 解压源码包:
unzip v2.9.0.zip
- 进入解压后的目录:
cd ansible-2.9.0
- 构建包:
python3 setup.py build
- 安装包:
python3 setup.py install
验证安装:
ansible --version
2. 通过发行包安装
对于 RHEL/CentOS(RPM 包):
- 安装 EPEL 仓库:
sudo yum install epel-release -y
- 安装 Ansible:
sudo yum install ansible -y
对于 Ubuntu/Debian(Deb 包):
- 更新包列表:
sudo apt update
- 安装 Ansible:
sudo apt install ansible -y
验证安装:
ansible --version
3. 通过 PIP 包管理器安装
步骤:
- 安装
pip
(如果未安装):
sudo yum install python3-pip -y # 对于 RHEL/CentOS
sudo apt install python3-pip -y # 对于 Ubuntu/Debian
- 使用
pip
安装 Ansible:
pip3 install ansible
验证安装:
ansible --version
4. 通过容器安装(Ansible Navigator)
步骤:
- 安装 Docker(如果未安装):
sudo yum install docker -y # 对于 RHEL/CentOS
sudo apt install docker.io -y # 对于 Ubuntu/Debian
- 启动 Docker 服务:
sudo systemctl start docker
sudo systemctl enable docker
- 拉取 Ansible 容器镜像:
docker pull quay.io/ansible/ansible-navigator
- 运行 Ansible 容器:
docker run -it quay.io/ansible/ansible-navigator /bin/bash
验证安装:
在容器内运行:
ansible --version
总结
安装方式 | 适用场景 | 优点 | 缺点 |
---|---|---|---|
源码包安装 | 需要特定版本或自定义构建 | 灵活性高,适合开发环境 | 步骤复杂,依赖较多 |
发行包安装 | 快速安装,适合生产环境 | 简单快捷,易于维护 | 版本可能较旧 |
PIP 安装 | 需要最新版本或特定依赖 | 版本灵活,适合 Python 环境 | 需要手动管理依赖 |
容器安装 | 隔离环境,快速部署 | 环境干净,易于迁移 | 需要 Docker 环境,资源占用较大 |
2. 配置 Ansible
2.1 配置文件 (ansible.cfg
)
Ansible 的配置文件通常位于 /etc/ansible/ansible.cfg
,但可以自定义。以下是一个示例配置:
[defaults]
inventory = /path/to/your/inventory
remote_user = root
ask_pass = True
host_key_checking = False
将自定义配置文件放在当前目录或 ~/.ansible.cfg
,Ansible 会自动加载。
2.2 Inventory 文件
Inventory 文件用于定义管理的主机。默认路径为 /etc/ansible/hosts
,但可以自定义。
基本示例:
[webservers]
192.168.1.101
192.168.1.102
[dbservers]
192.168.1.201
高级用法:
- 定义变量:
[webservers]
192.168.1.101 http_port=80
192.168.1.102 http_port=8080
[webservers:vars]
ansible_user=admin ansible_password=secret
- 主机组嵌套:
[webservers]
192.168.1.101
192.168.1.102
[dbservers]
192.168.1.201
[allservers:children]
webservers dbservers
3. 基本使用
3.1 测试连接
使用 ping
模块测试与目标主机的连接:
[root@localhost ~]# ansible -i /etc/ansible/hosts all -m ping
3.2 执行 Ad-hoc 命令
在 webservers
组的所有主机上运行命令:
[root@localhost ~]# ansible -i /etc/ansible/hosts webservers -a "uptime"
4. 常用模块示例
4.1 yum
模块:安装软件包
在 webservers
组的所有主机上安装 nginx
:
[root@localhost ~]# ansible -i /etc/ansible/hosts webservers -m yum -a "name=nginx state=present" --become
4.2 copy
模块:复制文件
将本地文件复制到 webservers
组的所有主机上:
[root@localhost ~]# ansible -i ~/ansible/hosts webservers -m copy -a "src=/path/to/local/file dest=/path/to/remote/file" --become
4.3 service
模块:管理服务
在 webservers
组的所有主机上启动 nginx
服务:
[root@localhost ~]# ansible -i ~/ansible/hosts webservers -m service -a "name=nginx state=started" --become
5. 编写 Playbook
Playbook 是 Ansible 的自动化脚本,使用 YAML 格式编写。
5.1 示例 Playbook
以下是一个在 webservers
组的所有主机上安装并启动 nginx
的 Playbook:
---
- hosts: webservers
become: yes
tasks:
- name: Install nginx
yum:
name: nginx
state: present
- name: Start nginx service
service:
name: nginx
state: started
保存为 nginx_playbook.yml
,然后运行:
[root@localhost ~]# ansible-playbook -i ~/ansible/hosts ~/ansible/nginx_playbook.yml
6. 高级功能
6.1 使用 Roles 组织 Playbook
Roles 是一种组织 Playbook 的方式,可以将任务、变量、文件等模块化。
创建 Role:
[root@localhost ~]# ansible-galaxy init ~/ansible/roles/nginx
在 Playbook 中使用 Role:
---
- hosts: webservers
roles:
- nginx
6.2 使用 Ansible Vault 加密敏感数据
Ansible Vault 用于加密敏感数据,例如密码或密钥。
创建加密文件:
[root@localhost ~]# ansible-vault create ~/ansible/secrets.yml
在 Playbook 中使用加密文件:
---
- hosts: webservers
vars_files:
- ~/ansible/secrets.yml
tasks:
- name: Use encrypted variable
debug:
msg: "The secret is {{ my_secret }}"
运行 Playbook 时提供 Vault 密码:
[root@localhost ~]# ansible-playbook -i ~/ansible/hosts ~/ansible/nginx_playbook.yml --ask-vault-pass
6.3 使用 Templates 动态生成文件
Ansible 使用 Jinja2 模板引擎生成动态文件。
创建模板文件:
[root@localhost ~]# vi ~/ansible/templates/nginx.conf.j2
模板内容:
server {
listen {{ http_port }};
server_name {{ server_name }};
}
在 Playbook 中使用模板:
---
- hosts: webservers
vars:
http_port: 80
server_name: example.com
tasks:
- name: Configure nginx
template:
src: ~/ansible/templates/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart nginx
6.4 使用 Handlers 触发任务
Handlers 是在任务中触发执行的特殊任务,通常用于服务重启。
---
- hosts: webservers
tasks:
- name: Install nginx
yum:
name: nginx
state: present
notify: Restart nginx
handlers:
- name: Restart nginx
service:
name: nginx
state: restarted
6.5 使用 Tags 控制任务执行
在 Playbook 中,可以为任务添加标签,以便选择性执行。
---
- hosts: webservers
tasks:
- name: Install nginx
yum:
name: nginx
state: present
tags: install
- name: Start nginx service
service:
name: nginx
state: started
tags: start
只执行带有 install
标签的任务:
[root@localhost ~]# ansible-playbook -i ~/ansible/hosts ~/ansible/nginx_playbook.yml --tags "install"
7. 调试与日志
7.1 启用详细日志
[root@localhost ~]# ansible-playbook -i ~/ansible/hosts ~/ansible/nginx_playbook.yml -vvv
7.2 使用 debug
模块
---
- hosts: webservers
tasks:
- name: Debug variables
debug:
var: ansible_facts
完结
Comments NOTHING