Ansible 常用模块

Ansible 常用模块简述

Ansible 模块是执行具体任务的工具,常用模块按功能分为以下几类:


一、文件操作类

模块 功能 常用参数 示例
copy 复制文件到远程主机 src, dest, mode, owner copy src=/local/file dest=/remote/file mode=0644
file 管理文件属性 path, state, mode, owner file path=/tmp/dir state=directory mode=0755
lineinfile 管理文件中的行 path, line, regexp, state lineinfile path=/etc/hosts line="127.0.0.1 localhost"
blockinfile 管理多行文本块 path, block, marker blockinfile path=/etc/profile block="export JAVA_HOME=/usr/java"
fetch 从远程拉取文件 src, dest fetch src=/remote/file dest=/local/
stat 获取文件状态 path stat path=/etc/passwd

二、系统管理类

模块 功能 常用参数 示例
user 管理用户 name, state, groups, shell user name=john state=present groups=wheel
group 管理用户组 name, state, gid group name=developers state=present
systemd 管理 systemd 服务 name, state, enabled systemd name=nginx state=started enabled=yes
service 管理 SysVinit 服务 name, state, enabled service name=httpd state=restarted
cron 管理定时任务 name, minute, job, state cron name="backup" minute=0 hour=2 job="/backup.sh"
reboot 重启系统 reboot_timeout, pre_reboot_delay reboot reboot_timeout=300
shutdown 关机 minutes, delay shutdown minutes=5

三、软件包管理类

模块 功能 适用系统 示例
yum yum 包管理 RedHat/CentOS yum name=nginx state=present
dnf dnf 包管理 RedHat 8+ dnf name=python3 state=latest
apt apt 包管理 Ubuntu/Debian apt name=nginx state=present update_cache=yes
pip Python 包管理 通用 pip name=requests version=2.28 state=present
npm Node.js 包管理 通用 npm name=express state=present global=yes

四、命令执行类

模块 功能 适用场景 示例
command 执行命令(默认) 简单命令,无管道/重定向 command cat /etc/passwd
shell 执行 shell 命令 需要管道、重定向、变量 `shell "ps aux
raw 执行原始命令 无 Python 环境时 raw "yum install -y python"
script 执行本地脚本 脚本分发执行 script /local/script.sh

五、网络与防火墙类

模块 功能 常用参数 示例
firewalld 管理 firewalld service, port, permanent, state firewalld service=http permanent=yes state=enabled
iptables 管理 iptables 规则 chain, source, jump, protocol iptables chain=INPUT source=1.1.1.1 jump=ACCEPT
uri 发送 HTTP 请求 url, method, body, status_code uri url=http://localhost/health method=GET
get_url 下载文件 url, dest, checksum get_url url=http://example.com/file dest=/tmp/

六、数据库类

模块 功能 示例
mysql_db MySQL 数据库管理 mysql_db name=mydb state=present
mysql_user MySQL 用户管理 mysql_user name=app password=pass priv=*.*:ALL
postgresql_db PostgreSQL 数据库管理 postgresql_db name=mydb state=present

七、常用模块速查表

任务场景 推荐模块 示例
安装 Nginx yum/dnf/apt yum name=nginx state=present
启动 Nginx systemd/service systemd name=nginx state=started
复制配置文件 copy/template copy src=nginx.conf dest=/etc/nginx/
创建用户 user user name=appuser groups=nginx
创建目录 file file path=/data state=directory
下载文件 get_url get_url url=https://... dest=/tmp/
执行脚本 script script /local/install.sh
重启服务器 reboot reboot reboot_timeout=300

八、模块使用示例

yaml 复制代码
# Playbook 综合示例
- hosts: webservers
  tasks:
    # 1. 安装 Nginx
    - name: Install nginx
      yum:
        name: nginx
        state: present

    # 2. 复制配置文件
    - name: Copy nginx config
      copy:
        src: /local/nginx.conf
        dest: /etc/nginx/nginx.conf
        mode: '0644'
      notify: restart nginx

    # 3. 创建应用目录
    - name: Create app directory
      file:
        path: /var/www/myapp
        state: directory
        owner: nginx
        mode: '0755'

    # 4. 启动服务
    - name: Start nginx
      systemd:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: restart nginx
      systemd:
        name: nginx
        state: restarted

九、常用命令

bash 复制代码
# 查看模块文档
ansible-doc copy          # 查看 copy 模块详细文档
ansible-doc -l | grep yum # 列出 yum 相关模块

# 临时执行(ad-hoc)
ansible all -m ping                                    # 连通性测试
ansible all -m copy -a "src=/local/file dest=/remote/" # 复制文件
ansible all -m yum -a "name=nginx state=present"       # 安装软件
ansible all -m shell -a "df -h"                        # 执行命令

一句话总结

Ansible 常用模块覆盖**文件(copy/file/lineinfile)、系统(user/systemd)、软件包(yum/apt)、命令(shell/command)、网络(firewalld/uri)**五大类,通过模块化实现配置管理自动化。

相关推荐
Olivia051405147 分钟前
Voohu:CHIP LAN在2.5G/5G多速率以太网中的共模抑制带宽要求与选型
网络
lbb 小魔仙11 分钟前
基于Python构建RAG(检索增强生成)系统:从原理到企业级实战
开发语言·python
SunnyDays101129 分钟前
Python 如何精准统计 Word 文档的页数、字数、行数
python·word文档字数统计
小陈的进阶之路1 小时前
Python系列课(2)——判断
java·前端·python
缝艺智研社1 小时前
2026年 自动化缝纫模板机 机器人工作站市场洞察与排名
大数据·网络·人工智能·自动化·制造·新人首发·自动化缝纫机
静心观复1 小时前
从短连接到 gRPC:一文读懂 HTTP 连接模型的演进
网络·网络协议·http
哼?~1 小时前
应用层协议TCP
网络·tcp
脉动数据行情1 小时前
Python 实现融通金行情数据对接(实时推送 + K 线 + 产品列表)
开发语言·python
xhbh6661 小时前
Win系统实现网络转发与端口映射:从 IPEnableRouter 到 RRAS 完整步骤
网络·端口转发·流量端口转发·ssh端口转发·端口转发工具
wltx16882 小时前
谷歌SEO如何做插床优化?
大数据·人工智能·python