Ansible的常见用法

目录

[一、Ansible 安装](#一、Ansible 安装)

[1.pip 安装(推荐,版本最新)](#1.pip 安装(推荐,版本最新))

[2.CentOS/RHEL 安装](#2.CentOS/RHEL 安装)

[3.Ubuntu/Debian 安装](#3.Ubuntu/Debian 安装)

[二、Ansible 基础配置](#二、Ansible 基础配置)

[1. 免密登录配置(核心)](#1. 免密登录配置(核心))

[2. 配置主机清单(Inventory)](#2. 配置主机清单(Inventory))

[3. 核心配置文件(ansible.cfg)](#3. 核心配置文件(ansible.cfg))

[三、Ansible 常见用法](#三、Ansible 常见用法)

[1. 基础命令(Ad-Hoc 临时命令)](#1. 基础命令(Ad-Hoc 临时命令))

[2. Playbook(剧本)](#2. Playbook(剧本))

[3. 常用模块速查](#3. 常用模块速查)


一、Ansible 安装

Ansible 是基于 Python 开发的自动化工具,支持多种安装方式,这里推荐最常用的 pip 安装系统包管理器安装

前置条件

  • 控制节点(运行 Ansible 的机器):Linux/macOS(Windows 需用 WSL),Python 3.8+
  • 被管理节点:无需安装 Ansible,只需开启 SSH 服务,且控制节点能免密登录

1.pip 安装(推荐,版本最新)

bash 复制代码
# 升级 pip
pip3 install --upgrade pip

# 安装 Ansible
pip3 install ansible

# 验证安装
ansible --version

2.CentOS/RHEL 安装

bash 复制代码
# 安装 EPEL 源(CentOS 7)
yum install -y epel-release

# 安装 Ansible
yum install -y ansible

# CentOS 8/RHEL 8 需先启用 PowerTools
dnf install -y epel-release
dnf config-manager --set-enabled powertools
dnf install -y ansible

3.Ubuntu/Debian 安装

bash 复制代码
# 更新源
apt update

# 安装依赖
apt install -y software-properties-common

# 添加 Ansible 源
add-apt-repository --yes --update ppa:ansible/ansible

# 安装 Ansible
apt install -y ansible

二、Ansible 基础配置

1. 免密登录配置(核心)

Ansible 通过 SSH 连接被管理节点,免密登录是基础:

bash 复制代码
# 1. 在控制节点生成 SSH 密钥(一路回车即可)
ssh-keygen -t rsa -b 4096

# 2. 将公钥拷贝到被管理节点(替换为目标主机 IP 和用户名)
ssh-copy-id root@192.168.1.100

2. 配置主机清单(Inventory)

主机清单是 Ansible 管理的节点列表,默认路径:/etc/ansible/hosts

bash 复制代码
# 编辑主机清单
vim /etc/ansible/hosts

# 示例配置(按实际节点修改)
[web_servers]  # 分组名(自定义)
192.168.1.100 ansible_ssh_user=root ansible_ssh_port=22
192.168.1.101 ansible_ssh_user=ubuntu

[db_servers]
192.168.1.200

# 分组嵌套
[all_servers:children]
web_servers
db_servers

3. 核心配置文件(ansible.cfg)

默认路径:/etc/ansible/ansible.cfg,常用配置项:

bash 复制代码
[defaults]
# 主机清单路径
inventory = /etc/ansible/hosts
# 禁用 SSH 密钥检查(避免首次连接报错)
host_key_checking = False
# 默认远程用户
remote_user = root
# 超时时间
timeout = 10

[privilege_escalation]
# 提权(如需 sudo)
become = True
become_method = sudo
become_user = root
become_ask_pass = False  # 无需输入 sudo 密码

三、Ansible 常见用法

1. 基础命令(Ad-Hoc 临时命令)

临时命令用于快速执行简单操作,无需编写 Playbook,格式:

bash 复制代码
ansible <主机/分组> -m <模块> -a <模块参数>

常用示例:

bash 复制代码
# 1. 测试所有节点连通性(ping 模块)
ansible all -m ping

# 2. 在 web_servers 分组执行 shell 命令(查看内存)
ansible web_servers -m shell -a "free -h"

# 3. 安装 nginx(yum/apt 模块)
# CentOS
ansible web_servers -m yum -a "name=nginx state=present"
# Ubuntu
ansible web_servers -m apt -a "name=nginx state=present update_cache=yes"

# 4. 启动并开机自启 nginx(service 模块)
ansible web_servers -m service -a "name=nginx state=started enabled=yes"

# 5. 拷贝文件到被管理节点(copy 模块)
ansible web_servers -m copy -a "src=/root/nginx.conf dest=/etc/nginx/nginx.conf mode=644"

# 6. 创建目录(file 模块)
ansible all -m file -a "path=/data/test state=directory mode=755"

# 7. 查看远程主机信息(setup 模块,收集facts)
ansible 192.168.1.100 -m setup

2. Playbook(剧本)

Playbook 是 Ansible 的核心,用 YAML 格式编写,用于执行复杂的自动化任务。

示例:部署 nginx 的 Playbook(nginx.yml)

bash 复制代码
---
- name: 部署 Nginx 服务  # Play 名称
  hosts: web_servers       # 目标主机/分组
  gather_facts: yes        # 收集主机信息(默认开启)
  become: yes              # 提权

  tasks:                   # 任务列表
    - name: 安装依赖包     # 任务1:安装依赖
      yum:
        name: epel-release
        state: present
      when: ansible_os_family == "RedHat"  # 仅 CentOS/RHEL 执行

    - name: 安装 Nginx     # 任务2:安装 nginx
      package:
        name: nginx
        state: present

    - name: 拷贝 nginx 配置文件  # 任务3:替换配置
      copy:
        src: /root/nginx.conf
        dest: /etc/nginx/nginx.conf
        mode: 0644
      notify: 重启 Nginx  # 配置变更时触发 handler

    - name: 启动 Nginx 并开机自启  # 任务4:启动服务
      service:
        name: nginx
        state: started
        enabled: yes

  handlers:  # 处理器(仅被触发时执行)
    - name: 重启 Nginx
      service:
        name: nginx
        state: restarted

执行 Playbook:

bash 复制代码
ansible-playbook nginx.yml

# 可选参数:
# -v:显示详细输出
# --check:模拟执行(不实际修改)
# --limit:指定仅执行部分主机
ansible-playbook nginx.yml --limit 192.168.1.100

3. 常用模块速查

模块名 用途 核心参数
ping 测试主机连通性
shell 执行 shell 命令 command(要执行的命令)
yum/apt 安装软件包 name(包名)、state(present/absent)
service 管理系统服务 name、state(started/stopped/restarted)、enabled
copy 拷贝文件 / 目录 src、dest、mode
file 管理文件 / 目录权限 path、state(directory/file/link)、mode
setup 收集主机信息(facts) filter(过滤参数,如 ansible_os_family)
相关推荐
陈桴浮海7 小时前
【Linux&Ansible】学习笔记合集三
linux·运维·云原生·ansible
Sheffield1 天前
command和shell模块到底区别在哪?
linux·云计算·ansible
陈桴浮海1 天前
【Linux&Ansible】学习笔记合集二
linux·学习·ansible
馨谙1 天前
Ansible模块化Playbook管理:静态导入与动态包含详解
运维·ansible
馨谙2 天前
Ansible 事实(Facts)全面指南:自动化运维中的主机信息管理
运维·ansible
馨谙2 天前
Ansible处理程序完全指南:实现智能的任务触发机制
运维·ansible
馨谙3 天前
Ansible 多 Play 编写与模块使用全解析
运维·ansible
馨谙3 天前
Ansible 清单详解:静态清单的构建与管理
ansible
馨谙3 天前
Ansible 配置文件详解:让自动化管理更轻松
运维·github·ansible