一、基础属性对比
| 维度 | ansible.builtin | ansible.posix |
| 归属 | Ansible 核心内置模块集(ansible-core)| Ansible 官方维护的 POSIX 专属集合 |
| 安装方式 | 随 ansible-core 预装,无需额外安装 | 需手动安装:`ansible-galaxy collection install ansible.posix` |
| 适用范围 | 跨平台(Linux/Windows/BSD 等)| 仅 POSIX 系统(Linux/Unix 类)|
| 核心定位 | 通用、基础、跨平台模块 | Linux 系统级、精细化管理模块 |
| 调用方式 | 可省略前缀(如 `command:` 等效 `ansible.builtin.command:`) | 必须写全前缀(如 `ansible.posix.acl:`) |
二、核心模块对比
✅ ansible.builtin(通用基础模块) 覆盖所有场景的"基础款",无需额外依赖:
| 模块类型 | 代表模块 | 用途 |
| 命令执行 | command、shell | 执行远程命令 |
| 文件管理 | file、copy、template | 管理文件/目录/配置 |
| 包管理 | yum、apt、dnf | 安装软件包 |
| 服务管理 | service、systemd | 管理系统服务 |
| 变量/调试 | debug、set_fact | 调试、定义变量 |
| 条件/流程 | when、block/rescue | 控制执行流程 |
| 定时任务 | cron | 管理 crontab 任务 |
✅ ansible.posix(Linux 专属增强模块) 补充 `builtin` 未覆盖的 Linux 系统级能力,更精细化:
| 模块类型 | 代表模块 | 用途 |
| 权限精细化 | acl | 管理文件 ACL 权限 |
| SSH 管理 | authorized_key | 配置 SSH 免密登录 |
| 系统调优 | sysctl | 管理内核参数 |
| 挂载管理 | mount | 管理文件系统挂载 |
| 防火墙 | firewalld | 管理 firewalld 规则 |
| 文件同步 | synchronize | 基于 rsync 同步文件 |
| SELinux 管理 | seboolean、selinux | 管理 SELinux 策略 |
三、核心使用原则
- 优先用 ansible.builtin - 场景:通用操作(如 copy 文件、启动服务、安装软件);
- 优势:无需额外安装、跨平台、稳定性高;
- 需精细化 Linux 操作时用 ansible.posix - 场景:Linux 系统级精细化管理(如 ACL 权限、SSH 公钥、内核参数); - 前提:先安装集合 `ansible-galaxy collection install ansible.posix`; - 示例:
四、关键注意事项
-
调用前缀: - `ansible.builtin` 可省略(如 `command:` 等效 `ansible.builtin.command:`); - `ansible.posix` 必须写全前缀(不能只写 `acl:`,必须写 `ansible.posix.acl:`)。
-
依赖差异: - `builtin` 模块无额外依赖(Ansible 自带); - `posix` 模块可能依赖目标主机的系统工具(如 `acl` 模块依赖 `setfacl` 命令)。
-
版本兼容: - `builtin` 随 ansible-core 版本更新,兼容性强; - `posix` 需单独升级(`ansible-galaxy collection install ansible.posix --upgrade`)。
总结
-
ansible.builtin:Ansible 核心"通用工具箱",预装、跨平台、覆盖基础操作;
-
ansible.posix:Linux 专属"增强工具箱",需手动装、仅适用于 POSIX 系统、做精细化管理;
-
核心选择逻辑:通用操作用 `builtin`,Linux 系统级精细化操作(ACL/SELinu/rsync 等)用 `posix`。 简单记:`builtin` = 基础通用,`posix` = Linux 专属增强。