1. 简介

Ansible 是一个简单但功能强大的 IT 自动化工具。在本教程中,我们将探讨使用 Ansible 创建目录的多种方式,并涵盖不同使用场景,包括设置权限、创建目录树、删除目录等。

如果你希望自动化部署服务器环境、管理文件结构,Ansible 提供了非常方便的解决方案。接下来我们将通过多个示例演示 Ansible 是如何简化这些操作的。

2. 准备工作

在开始之前,请确保满足以下前提条件:

  • 控制机上已安装 Ansible ✅
  • 控制机与远程主机之间可通过 SSH 连接 ✅
  • 已配置 inventory 文件,列出目标远程主机 ✅

准备好环境后,我们就可以开始使用 Ansible 来操作目录了。

3. Ansible 中的 file 模块

Ansible 的 file 模块是处理文件和目录的核心工具之一。它支持创建、修改和删除操作。常用参数包括:

  • path:指定要操作的路径
  • state:定义期望状态(如 directory 表示创建目录,absent 表示删除)
  • mode:设置权限,使用八进制表示法(如 0750

掌握了这些参数,我们就可以开始实战了。

4. 创建目录

要创建一个目录,只需使用 file 模块并设置 state: directory

---
- hosts: client1
  become: true
  tasks:
    - name: 创建目录
      file:
        path: /opt/my_dir
        state: directory

说明:

  • hosts: client1:作用于 inventory 文件中定义的 client1 主机
  • become: true:以 root 权限运行(因为写入 /opt 需要权限)
  • file:调用 file 模块
  • path:指定目录路径
  • state: directory:确保目录存在

执行:

ansible-playbook -i project_inventory.ini creating_directory.yml

验证:

ls /opt/

你会看到 my_dir 目录已被创建。

5. 创建带权限的目录

使用 mode 参数可设置目录权限。例如创建一个权限为 0750 的目录:

---
- hosts: client1
  become: true
  tasks:
    - name: 创建带权限的目录
      file:
        path: /opt/secured_directory
        state: directory
        mode: '0750'

执行后:

ls -ll /opt/

输出:

drwxr-x--- 2 root root 4096 Sep 17 04:01 secured_directory

权限说明:

  • 0750:所有者可读写执行,组用户可读执行,其他用户无权限 ❗

6. 创建目录树

Ansible 的 file 模块会自动创建缺失的父目录,因此只需指定完整路径即可:

---
- hosts: client1
  become: true
  tasks:
    - name: 创建目录树
      file:
        path: /opt/parent/child/grandchild
        state: directory

执行后,/opt/parent/child/grandchild 会被完整创建。

验证:

tree /opt/parent/

输出:

/opt/parent/
└── child
    └── grandchild

✅ 无需手动创建每一层目录,Ansible 自动搞定。

7. 一次性创建多个目录

使用 with_items 可以在一个任务中创建多个目录,减少冗余:

---
- hosts: client1
  become: true
  tasks:
    - name: 创建多个目录
      file:
        path: "{{ item }}"
        state: directory
      with_items:
        - /opt/dir1
        - /opt/dir2
        - /opt/dir3

执行后:

ls /opt/dir*

你会看到 dir1, dir2, dir3 均被创建。

8. 创建带时间戳的目录

使用 Ansible 内置变量 ansible_date_time 可动态生成时间戳目录名:

---
- hosts: client1
  become: true
  tasks:
    - name: 创建带时间戳的目录
      file:
        path: "/opt/backup_{{ ansible_date_time.date }}_{{ ansible_date_time.time }}"
        state: directory

执行后:

ls /opt/

输出类似:

backup_2024-09-17_04:28:44

这个技巧在备份、日志等场景非常实用 ✅

9. 删除目录

state 设置为 absent 即可删除目录:

---
- hosts: client1
  become: true
  tasks:
    - name: 删除目录
      file:
        path: /opt/dir1
        state: absent

执行后:

ls /opt/

你会发现 dir1 已被删除。如果目录不存在,任务也会正常执行,但不会标记为 changed ❗

10. 使用 install 命令创建目录

除了 file 模块,你也可以使用 command 模块调用 Linux 命令 install

---
- hosts: client1
  become: true
  tasks:
    - name: 使用 install 创建目录
      command: install -d -m 0755 /opt/install_directory

执行后:

ls -l /opt/

输出:

drwxr-xr-x 2 root root 4096 Sep 17 04:04 install_directory

说明:

  • -d:创建目录
  • -m 0755:设置权限为 0755

⚠️ 注意:install 是系统命令,不是 Ansible 模块,适用于需要更细粒度控制的场景。

11. 小结

本文介绍了 Ansible 中创建目录的多种方式,包括:

操作 模块 说明
创建目录 file 最常用
设置权限 file 使用 mode
创建目录树 file 自动创建父目录
创建多个目录 file + with_items 避免重复任务
时间戳目录 file + ansible_date_time 适用于日志、备份
删除目录 file 使用 state: absent
使用系统命令 command 灵活但需注意兼容性

熟练掌握这些方法,可以大大提高你使用 Ansible 自动化运维的效率 ✅

如果你有更复杂的目录管理需求,也可以结合 copytemplate 等模块进行扩展。


原始标题:Create a Directory Using Ansible