Ansible 是一个自动化运维工具,使用 YAML 语法来编写剧本(Playbooks),通过 SSH 协议管理远程主机。以下是 Ansible 的一些常见使用语法和示例:
1. 主机清单(Inventory)
主机清单用于定义 Ansible 管理的目标主机。可以是单个主机、多个主机或主机组。
简单主机清单示例
创建一个名为 hosts
的文件,内容如下:
# 单个主机
webserver1.example.com
# 主机组
[webservers]
webserver2.example.com
webserver3.example.com
[dbservers]
dbserver1.example.com
dbserver2.example.com
使用主机清单
在执行 Ansible 命令时,可以通过 -i
参数指定主机清单文件:
ansible -i hosts webservers -m ping
上述命令使用 hosts
作为主机清单,对 webservers
主机组中的所有主机执行 ping
模块。
2. Ad - Hoc 命令
Ad - Hoc 命令是一次性执行的简单命令,用于快速执行某些任务。
基本语法
ansible <host_pattern> -m <module_name> -a <module_args>
<host_pattern>
:指定目标主机或主机组,可以是单个主机名、主机组名、通配符等。-m <module_name>
:指定要使用的模块。-a <module_args>
:指定模块的参数。
示例
- 执行 ping 模块:
ansible all -m ping
该命令对所有主机执行 ping
模块,检查主机的连通性。
- 执行 shell 命令:
ansible webservers -m shell -a "ls -l /tmp"
该命令对 webservers
主机组中的所有主机执行 ls -l /tmp
命令。
- 文件操作:
#复制本地文件到远程主机
ansible db_servers -m copy -a "src=/data/config.yaml dest=/etc/app/ owner=root mode=0644"
# 创建目录
ansible all -m file -a "path=/var/log/myapp state=directory mode=0755"
- 软件包管理
# Debian/Ubuntu 安装 Nginx
ansible web_servers -m apt -a "name=nginx state=present" --become
# CentOS/RHEL 更新所有软件包
ansible all -m yum -a "name=* state=latest" -b
- 关键参数:
state=present
(安装)、state=latest
(更新)、state=absent
(卸载)。
- 服务管理
# 重启 Nginx 服务
ansible web_servers -m service -a "name=nginx state=restarted" -b
# 设置 Docker 开机自启
ansible docker_hosts -m service -a "name=docker enabled=yes" -b
- 收集系统信息
# 查看所有主机的磁盘空间
ansible all -m shell -a "df -h"
# 获取内核版本
ansible all -m setup -a "filter=ansible_kernel"
- 说明:
setup
模块用于收集主机的系统信息(Facts)。
3. 剧本(Playbooks)
剧本是 Ansible 自动化任务的核心,使用 YAML 语法编写。
基本结构
---
- name: Playbook 名称
hosts: <host_pattern>
become: yes # 是否使用特权执行任务
tasks:
- name: 任务名称
<module_name>:
<module_args>
示例:安装 Apache 服务器
---
- name: Install and start Apache
hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
when: ansible_os_family == "Debian"
- name: Start Apache service
service:
name: apache2
state: started
enabled: yes
when: ansible_os_family == "Debian"
上述剧本的功能是在 webservers
主机组中的所有 Debian 系统主机上安装并启动 Apache 服务器。
运行剧本
ansible-playbook -i hosts install_apache.yml
4. 变量(Variables)
在 Ansible 中可以使用变量来提高剧本的灵活性。
定义变量
可以在剧本中直接定义变量,也可以在单独的文件中定义。
---
- name: Use variables
hosts: webservers
become: yes
vars:
package_name: apache2
tasks:
- name: Install package
apt:
name: "{{ package_name }}"
state: present
when: ansible_os_family == "Debian"
使用变量
在上述示例中,使用 {{ package_name }}
引用变量。
5. 条件语句(Conditionals)
使用 when
关键字可以根据条件执行任务。
---
- name: Conditional tasks
hosts: all
tasks:
- name: Task for Debian systems
apt:
name: nginx
state: present
when: ansible_os_family == "Debian"
- name: Task for Red Hat systems
yum:
name: nginx
state: present
when: ansible_os_family == "RedHat"
6. 循环(Loops)
使用 loop
关键字可以实现循环操作。
---
- name: Loop example
hosts: all
become: yes
tasks:
- name: Install multiple packages
apt:
name: "{{ item }}"
state: present
loop:
- vim
- git
- curl
when: ansible_os_family == "Debian"
上述剧本会在 Debian 系统主机上依次安装 vim
、git
和 curl
包。