当前位置: 首页 > article >正文

Ansible内置模块之file

ansible.builtin.file 模块用于管理文件和目录的属性。可以创建、删除文件或目录,修改文件权限、所有者等属性。

1. 选 项 说 明

选项必须类型默认值说明
pathstr要管理的文件或目录的路径
statestr状态或类型。可为 file,directory,absent,link 等
modestr权限,如 0644、0755 等
ownerstr所有者
groupstr属组
recurseboolno是否递归更改目录及其内容的所有者、组和权限
srcstr用于创建符号链接时,指向源文件的路径
forceboolno在创建符号链接或硬链接时,如果目标已存在,是否强制替换
selevelstr设置文件或目录的 SELinux 安全级别
serolestr设置文件或目录的 SELinux 角色
setypestr设置文件或目录的 SELinux 类型
seuserstr设置文件或目录的 SELinux 用户

2. 用 例

# 创建或修改一个文件的属主,属组及权限
- name: Change file ownership, group and permissions
  ansible.builtin.file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

#
创建一个目录
- name: Create a directory if it does not exist
  ansible.builtin.file:
    path: /etc/some_directory
    state: directory
    mode: '0755'

#
创建一个带httpd_sys_content_t标签的目录
- name: Create the /webdev directory with SELinux label
  ansible.builtin.file:
    path: /webdev
    state: directory
    group: webdev
    setype: httpd_sys_content_t
    mode: '2775'

#
递归修改目录属主和属组
- name: Recursively change ownership of a directory
  ansible.builtin.file:
    path: /etc/foo
    state: directory
    recurse: yes
    owner: foo
    group: foo

#
创建一个连接文件
- name: Create a symbolic link
  ansible.builtin.file:
    src: /file/to/link/to
    dest: /path/to/symlink
    owner: foo
    group: foo
    state: link

#
强制创建/更新link地址
- name: Force replace an existing symbolic link
  ansible.builtin.file:
    src: /etc/newsourcefile
    path: /etc/linkfile
    state: link
    force: yes

#
更新修改和访问时间
- name: Update modification and access time of given file
  ansible.builtin.file:
    path: /etc/some_file
    state: file
    modification_time: now
    access_time: now

#
更新文件访问时间
- name: Set access time based on seconds from epoch value
  ansible.builtin.file:
    path: /etc/another_file
    state: file
    access_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'

#
删除文件
- name: Remove file (delete file)
  ansible.builtin.file:
    path: /etc/foo.txt
    state: absent

#
递归删除目录
- name: Recursively remove directory
  ansible.builtin.file:
    path: /etc/foo
    state: absent

          

Ansible相关文章推荐

  • Ansible 命令行指南

  • Ansible内置模块之file

  • 在RHEL 9上使用Ansible安装GitLab CE

  • 16张PPT让你一小时学会Ansible

  • 一文看懂Ansible自动化运维

  • Ansible内置模块之assemble

  • Ansible内置模块之authorized_key

  • Ansible内置模块之copy

  • Ansible内置模块之cron

  • 红帽RHCE9.0课程介绍
  • 红帽RHCA云技术课程介绍

http://www.kler.cn/a/545459.html

相关文章:

  • 实现pytorch注意力机制-one demo
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第七节】
  • [LeetCode] 二叉树 III — 110#平衡二叉树 | 257#二叉树的所有路径 | 404#左叶子之和 | 222#完全二叉树的节点个数
  • Vue.js组件开发:从入门到实践
  • 【PL/SQL】常用操作复习20250212
  • PHP防伪溯源查询系统小程序
  • 基于 Filebeat 的日志收集
  • windows 通过docker 安装mysql
  • 数据集成实例分享:金蝶云星空对接旺店通实现库存管理自动化
  • Java 后端开发:如何提取请求头中的 Cookie 和身份验证 Token
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第五节】
  • AI 网关对决:Higress 与 OneAPI 的功能对比
  • 使用MaxKB及deepseek搭建本地AI知识库
  • 开发美颜相机录制视频时,对于音频数据的处理
  • 数据结构 动态顺序表-vector
  • 《8天入门Trustzone/TEE/安全架构》
  • uniapp - iconfont下载本地并且运用至项目上
  • vscode C/C++ CMake 调试
  • DeepSeek 本地部署(电脑安装)
  • “PEP 8: W292 no newline at end of file“报错 IntelliJ IDEA自动添加空行问题