Ansible 基础教程

Ansible 是一款 IT 自动化工具。主要应用场景有配置系统、软件部署、持续发布及不停服平滑滚动更新的高级任务编排。

Ansible 本身非常简单易用,同时注重安全和可靠性,以最小化变动为特色,使用 OpenSSH 实现数据传输 ( 如果有需要的话也可以使用其它传输模式或者 pull 模式 ),其语言设计非常利于人类阅读,即使是针对不刚接触 Ansible 的新手来讲亦是如此。

Ansible安装配置

1. 安装 Ansible

apt 安装

1
2
3
4
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

pip 安装

1
sudo pip install ansible

2. 基本概念

  • 控制节点:运行Ansible命令的计算机。
  • 受管节点:被Ansible管理的计算机。
  • 清单(Inventory):定义受管节点的列表,通常存储在/etc/ansible/hosts文件中。
  • 模块(Modules):执行任务的单位,Ansible内置了许多模块用于不同的管理任务。
  • 剧本(Playbooks):包含任务的YAML文件,用于定义一组配置或编排任务。
  • 角色(Roles):用于组织和重用Ansible代码的层次结构。

3.配置清单文件

编写 Ansible 的 hosts 文件定义受管节点(主机)。这个文件可以在 /etc/ansible/hosts 路径下找到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 直接定义主机
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
# 分组的主机
[webservers]
alpha.example.org
beta.example.org


# 使用模式匹配来定义一组主机,例如 www[001:006].example.com 表示从 www001.example.com 到 www006.example.com 的主机
# 范围模式
www[001:006].example.com

可以为单个主机或整个组指定变量:

1
2
3
4
5
6
7
# 为单个主机指定变量
alpha.example.org ansible_user=admin ansible_port=2222

# 为整个组指定变量
[webservers:vars]
ansible_user=webadmin
ansible_port=80

为成功连接受控主机,需要配置ssh密钥

1.在控制节点生成 SSH 密钥对

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

将会在 ~/.ssh 目录下生成 id_rsa(私钥)和 id_rsa.pub(公钥)文件。

2.将公钥复制到目标主机

1
2
3
# 连接受控主机并将控制节点的公钥写入authorized_keys
ssh root@alpha.example.org
echo "<您的公钥内容>" >> ~/.ssh/authorized_keys # 注意:用户名root 非root用户需要在上面的hosts文件中指定登录名

4.Ansible使用

列出所有主机

1
2
3
4
列出所有在清单文件中定义的主机:
ansible all --list
# 列出指定组中的主机:
ansible webservers --list-hosts

Ping 所有主机

使用 ping 模块测试所有主机的连通性:

1
ansible all -m ping

在主机上执行命令

在指定的主机组上使用 command 模块执行命令(例如查看磁盘空间):

1
ansible webservers -m command -a "df -h"
在主机上执行 Shell 命令

在指定的主机组上使用 shell 模块执行命令(例如列出 /tmp 目录):

1
ansible webservers -m shell -a "ls -l /tmp"

运行剧本

使用 ansible-playbook 命令运行剧本:

1
ansible-playbook playbook.yml

提升权限

使用 -b--become 提升权限(例如使用 sudo):

1
ansible webservers -b -m command -a "df -h"

Anisble Modules 常用模块

ansible使用“模块”来完成大部分的任务。模块可以完成安装软件,复制文件,使用模板等功能。

模块 功能描述 示例
ping 测试主机是否可达 ansible all -m ping
command 在远程主机上执行命令(不通过 shell) ansible webservers -m command -a "df -h"
shell 在远程主机上执行 shell 命令 ansible webservers -m shell -a "ls -l /tmp"
raw 在远程主机上直接执行命令,不通过 Ansible 模块子系统 ansible all -m raw -a "python --version"
copy 将本地文件复制到远程主机 ansible webservers -m copy -a "src=/path/to/local/file dest=/path/to/remote/file"
fetch 从远程主机上提取文件 ansible webservers -m fetch -a "src=/path/to/remote/file dest=/path/to/local/file"
file 设置文件属性,如权限、所有权等 ansible webservers -m file -a "path=/path/to/file mode=0644"
service 管理服务,如启动、停止、重启等 ansible webservers -m service -a "name=nginx state=started"
apt 使用 apt 包管理器安装软件包(适用于 Debian 系系统) ansible webservers -m apt -a "name=nginx state=present"
yum 使用 yum 包管理器安装软件包(适用于 Red Hat 系系统) ansible webservers -m yum -a "name=httpd state=present"
user 管理用户和组 ansible webservers -m user -a "name=johndoe state=present"
group 管理组 ansible webservers -m group -a "name=admin state=present"
cron 管理 cron 作业 ansible webservers -m cron -a "name='backup' minute='0' hour='2' job='/path/to/backup.sh'"
get_url 从 URL 下载文件 ansible webservers -m get_url -a "url=http://example.com/file dest=/tmp/file"
setup 收集远程主机的详细信息 ansible all -m setup
template 使用 Jinja2 模板渲染文件并部署到远程主机 ansible webservers -m template -a "src=/path/to/template.j2 dest=/path/to/remote/file"
yum_repository 管理 YUM 仓库 ansible webservers -m yum_repository -a "name=epel description=EPEL repo baseurl=https://download.fedoraproject.org/pub/epel/7/x86_64/"
hostname 设置远程主机的主机名 ansible webservers -m hostname -a "name=newhostname"
git 管理 Git 仓库 ansible webservers -m git -a "repo=https://github.com/example/repo.git dest=/path/to/repo"

Ansible playbook 用法

Playbook 结构:

一个 playbook 通常包含以下几个部分:

  • hosts: 指定要执行任务的目标主机或主机组。
  • become: 指定是否以超级用户权限执行任务。
  • vars: 定义 playbook 中使用的变量。
  • tasks: 定义要执行的任务列表,每个任务使用模块来完成特定的操作。
  • handlers: 定义 playbook 中事件的响应动作,例如在某个服务重启后执行特定操作。

Playbook 示例:

以下是一个简单的 playbook 示例,用于安装 Nginx 并启动服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
---
- hosts: web_servers
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present

- name: Start Nginx service
service:
name: nginx
state: started

解释:

  • —: YAML 文件开头标识。
  • hosts: web_servers: 指定 playbook 应用的目标主机组为 web_servers。
  • become: true: 表示以超级用户权限执行任务。
  • tasks: 定义任务列表。
  • - name: Install Nginx: 任务名称,用于描述该任务的作用。
  • apt: 使用 apt 模块安装软件包。
  • name: nginx: 要安装的软件包名称为 nginx。
  • state: present: 确保软件包已安装。
  • - name: Start Nginx service: 启动 Nginx 服务。
  • service: 使用 service 模块管理服务。
  • name: nginx: 要管理的服务名称为 nginx。
  • state: started: 确保服务已启动。

运行 Playbook:

使用以下命令运行 playbook:

1
ansible-playbook <playbook_name.yml>

检查剧本的语法是否正确:

1
ansible-playbook playbook.yml --syntax-check

查看剧本将会执行的操作,但不实际执行:

1
ansible-playbook playbook.yml --check

Ansible 基础教程
http://example.com/2024/05/28/Ansible-基础教程/
作者
Sanli Ma
发布于
2024年5月28日
许可协议