ansible 常用模块 file

发布时间 2023-03-31 15:12:59作者: 小吉猫

模块说明

参数说明

Parameter

Comments

access_time

string

added in Ansible 2.7

此参数表示文件的访问时间应设置为的时间。

不需要修改时应保留,使用默认时间格式时应保留 YYYYMMDDHHMM.SS,或现在保留。

Default 的None意思是preserve是 的默认值state=[file,directory,link,hard]now是 的默认值state=touch

access_time_format

string

added in Ansible 2.7

当与 access_time 一起使用时,指示必须使用的时间格式。

基于默认的 Python 格式

Default: "%Y%m%d%H%M.%S"

attributes

aliases: attr

string

added in Ansible 2.3

生成的文件系统对象应该具有的属性。

要获得支持的标志,请查看目标系统上chattr的手册页。

这个字符串应该包含与lsattr显示的属性相同顺序的属性。

假定=运算符为默认值,否则字符串中需要包含+或-运算符。

follow

 

boolean

added in Ansible 1.8

此标志表示应遵循文件系统链接(如果存在)。
follow=yes和state=link结合mode等参数可以修改src。
在 Ansible 2.5 之前,默认情况下是false。
选择:
  • false

  • true ← (default)

force

 

boolean

在两种情况下强制创建符号链接:源文件不存在(但稍后会出现);目的地存在并且是一个文件(因此,我们需要取消路径文件的链接,并创建指向src文件的符号链接来代替它)。

选择:

  • false ← (default)

  • true

group

string

应该拥有文件系统对象的组的名称,将被馈送到chown。

如果未指定,它将使用当前用户的当前组,除非您是root用户,在这种情况下,它可以保留以前的所有权。

mode

 

any

生成的文件系统对象应该具有的权限。

对于那些习惯于/usr/bin/cmod的人,请记住,模式实际上是八进制数。您必须添加一个前导零,以便Ansible的YAML解析器知道它是一个八进制数(如0644或01777),或者引用它(如“644”或“1777”),以便Anible接收一个字符串,并可以自己从字符串转换为数字。

在不遵循这些规则的情况下给Ansible一个数字,最终会得到一个十进制数字,这将产生意外的结果。

从Ansible 1.8开始,该模式可以被指定为符号模式(例如,u+rwx或u=rw,g=r,o=r)。

如果未指定模式并且目标文件系统对象不存在,则在为新创建的文件系统对象设置模式时,将使用系统上的默认umask。

如果未指定模式,并且目标文件系统对象确实存在,则将使用现有文件系统对象的模式。

指定模式是确保使用正确权限创建文件系统对象的最佳方法

modification_time

 

string

added in Ansible 2.7

此参数指示文件的修改时间应设置为的时间。

当不需要修改时应保留,当使用默认时间格式时应保留YYYYMMDDHMM.SS,或现在保留。

默认值为None,这意味着preserve是state=[file,directory,link,hard]的默认值,现在是state=touch的默认值。

modification_time_format

string

added in Ansible 2.7

与modification_time一起使用时,表示必须使用的时间格式。

基于默认的Python格式

Default: "%Y%m%d%H%M.%S"

owner

 

string

应该拥有文件系统对象的用户的名称,正如将要提供给chown的那样。

如果未指定,它将使用当前用户,除非您是root用户,在这种情况下,它可以保留以前的所有权。

指定数字用户名将被假定为用户ID,而不是用户名。避免使用数字用户名以避免这种混淆。

path

aliases: dest, name

path / required

要管理的文件的路径。

recurse

 

boolean

added in Ansible 1.1

对目录内容递归设置指定的文件属性。

这仅适用于状态设置为目录的情况。

Choices:

  • false ← (default)

  • true

selevel

 

string

SELinux文件系统对象上下文的级别部分。

这是MLS/MCS属性,有时也称为范围。

当设置为_default时,它将使用策略的级别部分(如果可用)。

serole

string

SELinux文件系统对象上下文的角色部分。

当设置为_default时,它将使用策略的角色部分(如果可用)。

setype

string

SELinux文件系统对象上下文的类型部分。

当设置为_default时,它将使用策略的类型部分(如果可用)。

seuser

 

string

SELinux文件系统对象上下文的用户部分。

默认情况下,它会在适用的情况下使用系统策略。

当设置为_default时,它将使用策略的用户部分(如果可用)。

src

 

path

要链接到的文件的路径。

这只适用于state=link和state=hard。

对于state=link,这也将接受一个不存在的路径。

相对路径是相对于正在创建的文件(路径)的,Unix命令ln-s SRC DEST就是这样处理相对路径的。

state

 

string

如果不存在,目录将被递归删除,文件或符号链接将被取消链接。在目录的情况下,如果声明了diff,您将看到path_contents下列出的已删除的文件和文件夹。请注意,如果路径不存在,因为状态没有更改,则不存在不会导致文件失败。

如果是目录,则将创建所有不存在的中间子目录。由于Ansible 1.7,它们将使用提供的权限创建。

If file在没有其他选项的情况下返回路径的当前状态。

如果文件,即使有其他选项(如模式),如果文件存在,也会进行修改,但如果不存在,则不会创建。如果要创建文件(如果文件不存在),请设置为触摸或使用ansible.builtin.copy或ansible-builtin.template模块。

如果硬链接,则会创建或更改硬链接。

如果链接,符号链接将被创建或更改。

如果触摸(1.4中新增),如果文件不存在,将创建一个空文件,而现有文件或目录将接收更新的文件访问和修改时间(类似于触摸在命令行中的工作方式)。

默认值是文件的当前状态(如果存在),如果recurse=yes则为目录,否则为文件。

Choices:

  • "absent"

  • "directory"

  • "file"

  • "hard"

  • "link"

  • "touch"

unsafe_writes

 

boolean

added in Ansible 2.2

影响何时使用原子操作来防止数据损坏或从目标文件系统对象进行不一致的读取。

默认情况下,该模块使用原子操作来防止数据损坏或从目标文件系统对象进行不一致的读取,但有时会以防止这种情况发生的方式配置或破坏系统。一个例子是安装在docker上的文件系统对象,它不能从容器内部进行原子更新,只能以不安全的方式写入。

此选项允许Ansible在原子操作失败时返回到更新文件系统对象的不安全方法(但是,它不会强制Ansible执行不安全的写入)。

重要!不安全的写入会受到竞争条件的影响,并可能导致数据损坏。

Choices:

  • false ← (default)

  • true

示例

- 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

参考文档

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html#ansible-collections-ansible-builtin-file-module