Skip to main content
Documents
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Ansible Modules 常用模块

1. 基础命令执行类(不推荐大量使用)

command

执行命令(不经 shell 解析)

- name: List directory
  command: ls -l /etc

shell

执行 shell 命令(允许重定向、管道)

- name: Use pipe
  shell: "ps aux | grep nginx"

⚠️ 最佳实践:能用专用模块就不要用 shell/command

2. 文件与目录操作模块

file

创建目录、文件、软链接、修改权限

- name: Create directory
  file:
    path: /data/www
    state: directory
    mode: "0755"

copy

复制文件到远程节点

- name: Copy config
  copy:
    src: nginx.conf
    dest: /etc/nginx/nginx.conf

template

基于 Jinja2 模板渲染文件

- name: Render template
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf

lineinfile

替换匹配行(非常常用)

- name: Update config line
  lineinfile:
    path: /etc/sysctl.conf
    regexp: "^net.ipv4.ip_forward"
    line: "net.ipv4.ip_forward = 1"

blockinfile

插入多行文本

- blockinfile:
    path: /etc/profile
    block: |
      export GOPROXY=https://proxy.golang.org

synchronize

使用 rsync 同步文件(速度快)

- synchronize:
    src: ./dist/
    dest: /var/www/

unarchive

解压文件

- unarchive:
    src: app.tar.gz
    dest: /opt/app/
    remote_src: yes

3. 系统用户 / 权限模块

user

管理用户

- user:
    name: martin
    shell: /bin/bash
    state: present

group

管理用户组

- group:
    name: developers
    state: present

authorized_key

管理 SSH key

- authorized_key:
    user: root
    key: "{{ lookup('file', 'id_rsa.pub') }}"

4. 服务管理模块

service

控制服务(systemd)

- service:
    name: nginx
    state: restarted
    enabled: yes

systemd

更复杂的 systemd 操作

- systemd:
    name: docker
    daemon_reload: yes
    state: restarted

5. 软件包管理模块

package(自动选择 apt/yum/dnf)

- package:
    name: htop
    state: present

yum

- yum:
    name: httpd
    state: present

apt

- apt:
    name: nginx
    update_cache: yes

pip

- pip:
    name: uvicorn
    state: present

6. 网络与防火墙模块

firewalld

- firewalld:
    port: 80/tcp
    permanent: yes
    state: enabled

iptables

- iptables:
    chain: INPUT
    protocol: tcp
    destination_port: 22
    jump: ACCEPT

hostname

- hostname:
    name: web01

7. 数据库模块

mysql_db

- mysql_db:
    name: appdb
    state: present

mysql_user

- mysql_user:
    name: appuser
    password: secret
    priv: "appdb.*:ALL"
    state: present

postgresql_db

- postgresql_db:
    name: mydb
    state: present

postgresql_user

- postgresql_user:
    db: mydb
    name: martin
    password: "pwd123"
    priv: ALL

8. Docker / 容器模块(强烈推荐)

docker_container

- docker_container:
    name: nginx
    image: nginx:latest
    state: started
    ports:
      - "80:80"

docker_image

- docker_image:
    name: python:3.12
    source: pull

docker_network

- docker_network:
    name: mynet
    state: present

9. Kubernetes(k8s)资源模块

Ansible 可直接管理 CRD、Deployment、Service 等。

k8s

- k8s:
    state: present
    definition:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: nginx:1.25

k8s_info

获取资源信息

- k8s_info:
    kind: Pod
    namespace: default

10. 系统配置与信息模块

setup

收集系统 facts

- setup:

sysctl

修改内核参数(K8s 调优常用)

- sysctl:
    name: net.ipv4.ip_forward
    value: 1
    state: present
    reload: yes

timezone

- timezone:
    name: Asia/Shanghai

11. Git / 下载 / 请求模块

git

- git:
    repo: "https://github.com/martin/app.git"
    dest: /opt/app
    version: main

get_url

- get_url:
    url: https://example.com/app.tar.gz
    dest: /tmp/app.tar.gz
    mode: "0644"

uri

发送 HTTP 请求

- uri:
    url: https://httpbin.org/get
    method: GET
    return_content: yes

12. 压缩 / 文本 / JSON 模块

assemble

将多个文件拼接为一个

- assemble:
    src: /etc/nginx/conf.d.d/
    dest: /etc/nginx/nginx.conf

json_query

过滤 JSON

- debug:
    msg: "{{ result | json_query('items[*].metadata.name') }}"

13. 控制流相关(非常常用)

include_tasks

按条件加载任务

- include_tasks: install.yml
  when: ansible_os_family == "RedHat"

import_tasks

编译时加载

- import_tasks: common.yml

until(重试)

- shell: curl http://127.0.0.1/healthz
  register: result
  retries: 5
  delay: 3
  until: result.rc == 0

🧱 14. Role 相关模块

include_role

- include_role:
    name: nginx

import_role

- import_role:
    name: mysql

🛠 15. 逻辑控制(when / register / with_items)

register

- command: cat /etc/hostname
  register: out

- debug:
    msg: "{{ out.stdout }}"

when 条件

- yum:
    name: httpd
  when: ansible_os_family == "RedHat"

with_items

- user:
    name: "{{ item }}"
    state: present
  with_items:
    - alice
    - bob

📌 16. 专用于 DevOps 的常用模块

cron

- cron:
    name: backup
    minute: "0"
    hour: "2"
    job: "/usr/bin/backup.sh"

mount

挂载磁盘

- mount:
    path: /data
    src: /dev/vdb1
    fstype: ext4
    state: mounted

reboot

- reboot:
    msg: "Rebooting for kernel upgrade"
    reboot_timeout: 600

📚 17. 类比对照速查(最常用)

需求 推荐模块
创建目录 file
上传配置文件 template / copy
修改配置文件一行 lineinfile
插入多行 blockinfile
重启服务 service
安装软件 package / apt / yum
管理用户 user
管理 Docker docker_* 系列
管理 k8s k8s
下载 URL 文件 get_url
使用 rsync 同步 synchronize
管理系统参数 sysctl
管理定时任务 cron