侧边栏壁纸
博主头像
LLJJJJ的博客

行动起来,活在当下

  • 累计撰写 4 篇文章
  • 累计创建 5 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录

Ansible学习

Administrator
2026-07-22 / 0 评论 / 0 点赞 / 0 阅读 / 0 字

Ansible 简介

一句话概括:Ansible 是一个基于 SSH 的自动化运维工具,可以用它来:

  • 批量执行命令:同时在上百台服务器上执行 uptimeyum update

  • 配置管理:确保所有服务器的 Nginx 配置文件都是统一标准

  • 应用部署:一键将代码发布到所有 Web 服务器

核心优势:不需要在被管理机器上安装任何 Agent,只需要 Python 和 SSH。

幂等性:执行多次和一次效果一样,不会重复执行。

核心概念(3个必须理解的名词)

Module:执行具体操作的功能单元,如 copy、yum、service
HostInventory:记录由Ansible管理的主机信息,包括端口、密码、ip等。
Playbooks:“剧本”YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能。

Ansible 安装

apt install ansible -y
 #验证是否安装成功
ansible --version

Inventory 文件(告诉 Ansible 去操作谁)

ini

# inventory.ini
# 基本写法
192.168.1.10
192.168.1.11

# 分组写法
[web_servers]
192.168.1.10
192.168.1.11

[db_servers]
192.168.1.20

# 带变量的写法
[web_servers]
192.168.1.10 ansible_user=root
192.168.1.11 ansible_user=root

# 按组执行:指定 host group
ansible-playbook -i inventory.ini site.yml --limit web_servers

Playbook 基本结构(最核心)

yaml

---
- hosts: web_servers          # 在哪些机器上执行
  become: yes                  # 是否提权(sudo)
  tasks:                       # 任务列表
    - name: 安装 Nginx        # 任务名称(可读性)
      apt:                     # 使用 apt 模块
        name: nginx
        state: present

    - name: 启动 Nginx
      systemd:
        name: nginx
        state: started
        enabled: yes

常用模块详解

1. apt(Ubuntu/Debian 安装包管理)

yaml

- name: 安装 Docker
  apt:
    name: docker.io
    state: present    # 安装

- name: 卸载 Docker
  apt:
    name: docker.io
    state: absent     # 卸载

- name: 更新软件源
  apt:
    update_cache: yes

2. copy(复制文件到目标机器)

yaml

- name: 复制配置文件
  copy:
    src: ./nginx.conf           # 本地文件
    dest: /etc/nginx/nginx.conf # 目标路径
    mode: '0644'                # 权限
    owner: root
    group: root

3. authorized_key(管理 SSH 公钥)

yaml

- name: 分发 SSH 公钥
  authorized_key:
    user: root
    state: present    # present=添加,absent=移除
    key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"

4. systemd(管理服务)

yaml

- name: 启动并开机自启
  systemd:
    name: nginx
    state: started
    enabled: yes

- name: 重启服务
  systemd:
    name: nginx
    state: restarted

- name: 停止服务
  systemd:
    name: nginx
    state: stopped

5. replace(替换文件内容)

yaml

- name: 替换 apt 源为阿里云
  replace:
    path: /etc/apt/sources.list
    regexp: 'archive.ubuntu.com'
    replace: 'mirrors.aliyun.com'

- name: 替换 Nginx 端口(从 80 改成 8080)
  replace:
    path: /etc/nginx/nginx.conf
    regexp: 'listen 80;'
    replace: 'listen 8080;'

6. get_url(下载文件)

yaml

- name: 下载 Docker Compose
  get_url:
    url: https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64
    dest: /usr/local/bin/docker-compose
    mode: '0755'

7. shell(执行自定义命令,常用来处理简单脚本逻辑)

yaml

- name: 获取机器 IP
  shell: "hostname -I | awk '{print $1}'"
  register: ip_address

- name: 打印 IP
  debug:
    msg: "服务器的 IP 是 {{ ip_address.stdout }}"

8. file(管理文件/目录)

yaml

- name: 创建目录
  file:
    path: /var/log/nginx
    state: directory
    mode: '0755'

- name: 创建软链接
  file:
    src: /opt/app/current
    dest: /usr/local/bin/app
    state: link

六、Handler(被通知才执行的任务)

yaml

---
- hosts: web_servers
  tasks:
    - name: 更新 Nginx 配置
      copy:
        src: nginx.conf
        dest: /etc/nginx/nginx.conf
      notify: restart nginx    # 触发 handler

  handlers:
    - name: restart nginx      # handler 名称
      systemd:
        name: nginx
        state: restarted

特点: 只有配置文件真正发生变化(文件有更新)时,才会触发 restart nginx;如果文件内容没变,不会执行。

七、执行命令

bash

# 基本执行
ansible-playbook -i inventory.ini playbook.yml

# 检查语法(不真正执行)
ansible-playbook -i inventory.ini playbook.yml --check

# 模拟执行(看会做什么)
ansible-playbook -i inventory.ini playbook.yml --dry-run

# 指定单个 Playbook 文件执行
ansible-playbook playbook.yml -i hosts.ini

0

评论区