ansible.builtin.file 模块 – 管理文件和文件属性
注意
此模块是 ansible-core
的一部分,并包含在所有 Ansible 安装中。在大多数情况下,即使不指定 集合关键字,也可以使用简短的模块名称 file
。但是,我们建议您使用完全限定集合名称 (FQCN) ansible.builtin.file
,以便轻松链接到模块文档,并避免与其他可能具有相同模块名称的集合冲突。
概要
设置文件、目录或符号链接及其目标的属性。
或者,删除文件、符号链接或目录。
许多其他模块都支持与 ansible.builtin.file 模块相同的选项,包括 ansible.builtin.copy、ansible.builtin.template 和 ansible.builtin.assemble。
对于 Windows 目标,请改用 ansible.windows.win_file 模块。
参数
参数 |
注释 |
---|---|
此参数指示文件访问时间应设置为的时间。 当不需要修改时应为 默认值为 |
|
结果文件系统对象应具有的属性。 要获取支持的标志,请查看目标系统上 chattr 的手册页。 此字符串应包含与 lsattr 显示的顺序相同的属性。
|
|
此标志指示应遵循文件系统链接(如果存在)。
在 Ansible 2.5 之前,默认为 在创建目标不存在的符号链接时,请将 选择
|
|
应该拥有文件系统对象的所有者的组的名称,就像提供给 chown 一样。 如果未指定,则使用当前用户的当前组,除非您是 root 用户,在这种情况下,可以保留之前的属主。 |
|
结果文件系统对象应具有的权限。 对于那些习惯于使用 * /usr/bin/chmod * 的人,请记住模式实际上是八进制数。您必须给 Ansible 足够的信息来正确解析它们。为了获得一致的结果,请引用八进制数(例如, 如果不遵循这些规则中的任何一个,则 Ansible 将得到一个十进制数,这将产生意想不到的结果。 从 Ansible 1.8 开始,模式可以指定为符号模式(例如, 如果未指定 如果未指定 指定 |
|
此参数指示应将文件的修改时间设置为的时间。 当不需要修改时应为 默认值为 None,表示对于 |
|
应该拥有文件系统对象的用户的名称,就像传递给 *chown* 一样。 如果未指定,则使用当前用户,除非您是 root 用户,在这种情况下,可以保留之前的属主。 指定数字用户名将被假定为用户 ID 而不是用户名。避免使用数字用户名以避免这种混淆。 |
|
要管理的文件的路径。 |
|
SELinux 文件系统对象上下文的级别部分。 这是 MLS/MCS 属性,有时称为 当设置为 |
|
SELinux 文件系统对象上下文的角色部分。 当设置为 |
|
SELinux 文件系统对象上下文的类型部分。 当设置为 |
|
SELinux 文件系统对象上下文的用户部分。 默认情况下,它使用 当设置为 |
|
要链接到的文件的路径。 这仅适用于 对于 相对路径相对于正在创建的文件( |
|
如果为 如果为 如果为 如果为 如果为 如果为 如果为 默认值为文件(如果存在)的当前状态,如果 选择
|
|
影响何时使用原子操作以防止数据损坏或从目标文件系统对象读取不一致的内容。 默认情况下,此模块使用原子操作来防止数据损坏或从目标文件系统对象读取不一致的内容,但有时系统的配置方式或损坏方式会阻止这种情况。一个例子是 docker 挂载的文件系统对象,这些对象无法从容器内部原子更新,并且只能以不安全的方式写入。 此选项允许 Ansible 在原子操作失败时回退到更新文件系统对象的不安全方法(但是,它不会强制 Ansible 执行不安全的写入)。 重要提示!不安全的写入容易受到竞争条件的影响,并可能导致数据损坏。 选择
|
属性
属性 |
支持 |
描述 |
---|---|---|
支持: 完整 |
可以在 check_mode 模式下运行,并返回变更状态预测,而不会修改目标。如果不支持,则会跳过该操作。 |
|
支持: 部分 会显示权限和所有权,但不会显示 absent/touch 操作的文件内容。 |
当处于 diff 模式时,会返回已更改的内容(或在 check_mode 模式下可能需要更改的内容)的详细信息 |
|
平台: posix |
可以操作的目标操作系统/系列 |
参见
参见
- ansible.builtin.assemble
从片段组装配置文件。
- ansible.builtin.copy
将文件复制到远程位置。
- ansible.builtin.stat
检索文件或文件系统状态。
- ansible.builtin.template
将模板文件输出到目标主机。
- ansible.windows.win_file
创建、触摸或删除文件或目录。
示例
- name: Change file ownership, group and permissions
ansible.builtin.file:
path: /etc/foo.conf
owner: foo
group: foo
mode: '0644'
- name: Give insecure permissions to an existing file
ansible.builtin.file:
path: /work
owner: root
group: root
mode: '1777'
- name: Create a symbolic link
ansible.builtin.file:
src: /file/to/link/to
dest: /path/to/symlink
owner: foo
group: foo
state: link
- name: Create two hard links
ansible.builtin.file:
src: '/tmp/{{ item.src }}'
dest: '{{ item.dest }}'
state: hard
loop:
- { src: x, dest: y }
- { src: z, dest: k }
- name: Touch a file, using symbolic modes to set the permissions (equivalent to 0644)
ansible.builtin.file:
path: /etc/foo.conf
state: touch
mode: u=rw,g=r,o=r
- name: Touch the same file, but add/remove some permissions
ansible.builtin.file:
path: /etc/foo.conf
state: touch
mode: u+rw,g-wx,o-rwx
- name: Touch again the same file, but do not change times this makes the task idempotent
ansible.builtin.file:
path: /etc/foo.conf
state: touch
mode: u+rw,g-wx,o-rwx
modification_time: preserve
access_time: preserve
- name: Create a directory if it does not exist
ansible.builtin.file:
path: /etc/some_directory
state: directory
mode: '0755'
- 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: Recursively change ownership of a directory
ansible.builtin.file:
path: /etc/foo
state: directory
recurse: yes
owner: foo
group: foo
- 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
返回值
通用返回值已在此处记录 这里,以下是此模块独有的字段
键 |
描述 |
---|---|