← 返回首页
🔧

Ansible 入门基础

📂 devops ⏱ 3 min 479 words

Ansible 入门基础

什么是 Ansible

Ansible 是一个开源的 IT 自动化工具,用于配置管理、应用部署、任务自动化和编排。它使用 SSH 协议与被管理节点通信,无需在被管理节点上安装代理(Agentless)。

核心概念

安装 Ansible

Linux

# Ubuntu/Debian
sudo apt update
sudo apt install ansible

# CentOS/RHEL
sudo yum install epel-release
sudo yum install ansible

# pip 安装
pip install ansible

macOS

brew install ansible

验证安装

ansible --version

配置 Inventory

INI 格式

# /etc/ansible/hosts 或 inventory.ini
[webservers]
web1.example.com ansible_host=192.168.1.10
web2.example.com ansible_host=192.168.1.11

[dbservers]
db1.example.com ansible_host=192.168.1.20
db2.example.com ansible_host=192.168.1.21

[all:vars]
ansible_user=deploy
ansible_ssh_private_key_file=~/.ssh/id_rsa

YAML 格式

# inventory.yml
all:
  children:
    webservers:
      hosts:
        web1.example.com:
          ansible_host: 192.168.1.10
        web2.example.com:
          ansible_host: 192.168.1.11
    dbservers:
      hosts:
        db1.example.com:
          ansible_host: 192.168.1.20
  vars:
    ansible_user: deploy
    ansible_ssh_private_key_file: ~/.ssh/id_rsa

第一个 Playbook

简单示例

# first-playbook.yml
---
- hosts: webservers
  become: yes
  vars:
    http_port: 80
    
  tasks:
    - name: 安装 Nginx
      apt:
        name: nginx
        state: present
        update_cache: yes
      when: ansible_os_family == "Debian"
    
    - name: 启动 Nginx
      service:
        name: nginx
        state: started
        enabled: yes
    
    - name: 复制配置文件
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: Restart Nginx
    
    - name: 确保端口开放
      ufw:
        rule: allow
        port: "{{ http_port }}"
        proto: tcp

  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted

常用命令

Ad-hoc 命令

# 测试连接
ansible all -m ping

# 执行命令
ansible webservers -m shell -a "uptime"

# 复制文件
ansible webservers -m copy -a "src=/local/file dest=/remote/file"

# 安装包
ansible dbservers -m yum -a "name=mysql state=present"

# 服务管理
ansible webservers -m service -a "name=nginx state=restarted"

Playbook 命令

# 执行 Playbook
ansible-playbook first-playbook.yml

# 检查模式(不执行)
ansible-playbook first-playbook.yml --check

# 限制主机
ansible-playbook first-playbook.yml --limit webservers

# 调试模式
ansible-playbook first-playbook.yml -vvv

# 指定 Inventory
ansible-playbook -i inventory.yml first-playbook.yml

变量使用

定义变量

# 在 Playbook 中定义
vars:
  http_port: 80
  max_clients: 200

# 在 Inventory 中定义
[webservers]
web1 http_port=8080

# 在 vars 文件中定义
# group_vars/webservers.yml
http_port: 80

使用变量

tasks:
  - name: 使用变量
    debug:
      msg: "端口号是 {{ http_port }}"
    
  - name: 条件判断
    debug:
      msg: "这是 Web 服务器"
    when: http_port == 80

常用模块

文件操作

- name: 创建目录
  file:
    path: /opt/app
    state: directory
    mode: '0755'

- name: 复制文件
  copy:
    src: files/app.conf
    dest: /etc/app/app.conf
    owner: root
    group: root
    mode: '0644'

包管理

- name: 安装软件包
  apt:
    name:
      - nginx
      - git
      - curl
    state: present
    update_cache: yes

服务管理

- name: 确保服务运行
  service:
    name: nginx
    state: started
    enabled: yes

实践案例

部署 Web 应用

---
- hosts: webservers
  become: yes
  vars:
    app_name: myapp
    app_port: 8080
    
  tasks:
    - name: 安装依赖
      apt:
        name:
          - python3
          - python3-pip
          - nginx
        state: present
        update_cache: yes
    
    - name: 创建应用目录
      file:
        path: /opt/{{ app_name }}
        state: directory
        owner: www-data
        group: www-data
    
    - name: 复制应用代码
      copy:
        src: ../app/
        dest: /opt/{{ app_name }}/
        owner: www-data
        group: www-data
    
    - name: 安装 Python 依赖
      pip:
        requirements: /opt/{{ app_name }}/requirements.txt
        executable: pip3
    
    - name: 复制 Nginx 配置
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/sites-available/{{ app_name }}
      notify: Reload Nginx
    
    - name: 启用站点
      file:
        src: /etc/nginx/sites-available/{{ app_name }}
        dest: /etc/nginx/sites-enabled/{{ app_name }}
        state: link
      notify: Reload Nginx
    
    - name: 启动应用
      systemd:
        name: {{ app_name }}
        state: started
        enabled: yes

  handlers:
    - name: Reload Nginx
      service:
        name: nginx
        state: reloaded

最佳实践

总结

Ansible 是一个强大的自动化工具,通过简单的 YAML 语法即可实现复杂的配置管理和应用部署任务。掌握 Ansible 可以大大提高运维效率。