ansible.builtin.lineinfile 模块 – 管理文本文件中的行

注意

此模块是 ansible-core 的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使没有指定 collections 关键字,您也可以使用简短的模块名称 lineinfile。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.lineinfile,以便轻松链接到模块文档并避免与可能具有相同模块名称的其他集合冲突。

概要

参数

参数

注释

attributes

别名:attr

字符串

结果文件系统对象应具有的属性。

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

此字符串应按 lsattr 显示的顺序包含属性。

默认情况下假设使用 = 运算符,否则需要在字符串中包含 +- 运算符。

backrefs

布尔值

state=present 结合使用。

如果设置,line 可以包含反向引用(位置引用和命名引用),如果 regexp 匹配,这些引用将被填充。

此参数会稍微改变模块的操作;insertbeforeinsertafter 将被忽略,如果 regexp 在文件中任何地方都不匹配,则文件将保持不变。

如果 regexp 匹配,则最后匹配的行将被扩展的行参数替换。

search_string 互斥。

选择

  • false ← (default)

  • true

backup

布尔值

创建一个备份文件,其中包括时间戳信息,以便您在不小心覆盖文件时可以恢复原始文件。

选择

  • false ← (default)

  • true

create

布尔值

state=present 结合使用。

如果指定,则如果文件不存在,将创建该文件。

默认情况下,如果文件丢失,它将失败。

选择

  • false ← (default)

  • true

firstmatch

布尔值

insertafterinsertbefore 结合使用。

如果设置,insertafterinsertbefore 将与第一个匹配给定正则表达式的行一起工作。

选择

  • false ← (default)

  • true

group

字符串

应拥有文件系统对象的组的名称,如传递给 chown 一样。

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

insertafter

字符串

state=present 结合使用。

如果指定,则该行将插入到指定正则表达式的最后匹配之后。

如果需要第一个匹配,请使用(firstmatch=yes)。

可以使用一个特殊值;EOF 用于将行插入文件末尾。

如果指定的正则表达式没有匹配项或未传递任何值,则将使用 EOF 代替。

如果设置了 insertbefore,则默认值 EOF 将被忽略。

如果将正则表达式同时传递给 regexpinsertafter,则仅当未找到 regexp 的匹配项时才遵守 insertafter

不能与 backrefsinsertbefore 结合使用。

insertbefore

字符串

state=present 结合使用。

如果指定,则该行将插入到指定正则表达式的最后匹配之前。

如果需要第一个匹配,请使用 firstmatch=yes

可以使用一个值;BOF 用于将行插入文件开头。

如果指定的正则表达式没有匹配项,则该行将插入到文件末尾。

如果将正则表达式同时传递给 regexpinsertbefore,则仅当未找到 regexp 的匹配项时才遵守 insertbefore

不能与 backrefsinsertafter 结合使用。

line

别名:value

字符串

要插入/替换到文件中的行。

对于 state=present 而言是必需的。

如果设置了 backrefs,则可能包含反向引用,这些引用将在与 regexp 正则表达式匹配时使用正则表达式捕获组进行扩展。

mode

any

结果文件系统对象的权限。

对于习惯使用 /usr/bin/chmod 的用户,请记住模式实际上是八进制数。您必须为 Ansible 提供足够的信息才能正确解析它们。为了获得一致的结果,请引用八进制数(例如,'644''1777'),以便 Ansible 接收字符串并可以自行将字符串转换为数字。添加前导零(例如,0755)有时有效,但在循环和其他一些情况下可能会失败。

如果为 Ansible 提供一个不遵循上述任何规则的数字,最终将得到一个十进制数,这将产生意外的结果。

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

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

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

指定 mode 是确保文件系统对象以正确权限创建的最佳方法。有关更多详细信息,请参阅 CVE-2020-1736。

owner

字符串

应拥有文件系统对象的用户的名称,如提供给 chown 的一样。

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

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

path

别名:dest、destfile、name

path / required

要修改的文件。

在 Ansible 2.3 之前,此选项只能用作 destdestfilename

regexp

别名:regex

字符串

在文件每一行中查找的正则表达式。

对于 state=present,如果找到,则要替换的模式。将仅替换找到的最后一行。

对于 state=absent,要删除的行(s) 的模式。

如果正则表达式没有匹配,则该行将根据 insertbeforeinsertafter 设置添加到文件。

修改一行时,正则表达式通常应该匹配行的初始状态以及由 line 替换后的状态,以确保幂等性。

使用 Python 正则表达式。请参阅 https://docs.pythonlang.cn/3/library/re.html.

search_string

字符串

在 ansible-core 2.11 中添加

在文件每一行中查找的文字字符串。它不必匹配整行。

对于 state=present,如果字符串在文件中找到,则要替换的行。将仅替换找到的最后一行。

对于 state=absent,如果字符串在行中,则要删除的行(s)。

如果文字表达式没有匹配,则该行将根据 insertbeforeinsertafter 设置添加到文件。

backrefsregexp 互斥。

selevel

字符串

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

这是 MLS/MCS 属性,有时称为 range

设置为 _default 时,它将使用策略中可用的 level 部分。

serole

字符串

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

设置为 _default 时,它将使用策略中可用的 role 部分。

setype

字符串

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

设置为 _default 时,它将使用策略中可用的 type 部分。

seuser

字符串

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

默认情况下,它使用 system 策略(如果适用)。

设置为 _default 时,它将使用策略中可用的 user 部分。

state

字符串

该行是否存在。

选择

  • "absent"

  • "present" ← (default)

unsafe_writes

布尔值

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

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

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

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

选择

  • false ← (default)

  • true

validate

字符串

在将更新后的文件复制到最终目标之前运行的验证命令。

使用临时文件路径进行验证,通过 %s 传递,它必须像下面示例中那样存在。

此外,该命令以安全方式传递,因此 shell 功能(如扩展和管道)将不起作用。

有关如何处理比此选项提供的更复杂的验证的示例,请参阅 处理复杂的验证.

属性

属性

支持

描述

check_mode

支持:完全支持

可以在 check_mode 下运行并返回更改状态预测,而不会修改目标,如果不受支持,则操作将被跳过。

diff_mode

支持:完全支持

将在差异模式下返回有关更改了什么(或者可能在 check_mode 下需要更改什么)的详细信息

platform

平台: posix

可以针对其进行操作的目标操作系统/系列

safe_file_operations

支持:完全支持

使用 Ansible 的严格文件操作函数来确保适当的权限并避免数据损坏

vault

支持:不支持

可以自动解密 Ansible 加密的 vault 文件

备注

注意

  • 从 Ansible 2.3 开始,dest 选项已更改为 path 作为默认值,但 dest 仍然有效。

另请参阅

另请参阅

ansible.builtin.blockinfile

插入/更新/删除由标记行包围的文本块。

ansible.builtin.copy

将文件复制到远程位置。

ansible.builtin.file

管理文件和文件属性。

ansible.builtin.replace

使用反向引用正则表达式替换文件中所有特定字符串的实例。

ansible.builtin.template

将文件模板化到目标主机。

community.windows.win_lineinfile

community.windows.win_lineinfile 模块的官方文档。

示例

# NOTE: Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
- name: Ensure SELinux is set to enforcing mode
  ansible.builtin.lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=enforcing

- name: Make sure group wheel is not in the sudoers configuration
  ansible.builtin.lineinfile:
    path: /etc/sudoers
    state: absent
    regexp: '^%wheel'

- name: Replace a localhost entry with our own
  ansible.builtin.lineinfile:
    path: /etc/hosts
    regexp: '^127\.0\.0\.1'
    line: 127.0.0.1 localhost
    owner: root
    group: root
    mode: '0644'

- name: Replace a localhost entry searching for a literal string to avoid escaping
  ansible.builtin.lineinfile:
    path: /etc/hosts
    search_string: '127.0.0.1'
    line: 127.0.0.1 localhost
    owner: root
    group: root
    mode: '0644'

- name: Ensure the default Apache port is 8080
  ansible.builtin.lineinfile:
    path: /etc/httpd/conf/httpd.conf
    regexp: '^Listen '
    insertafter: '^#Listen '
    line: Listen 8080

- name: Ensure php extension matches new pattern
  ansible.builtin.lineinfile:
    path: /etc/httpd/conf/httpd.conf
    search_string: '<FilesMatch ".php[45]?$">'
    insertafter: '^\t<Location \/>\n'
    line: '        <FilesMatch ".php[34]?$">'

- name: Ensure we have our own comment added to /etc/services
  ansible.builtin.lineinfile:
    path: /etc/services
    regexp: '^# port for http'
    insertbefore: '^www.*80/tcp'
    line: '# port for http by default'

- name: Add a line to a file if the file does not exist, without passing regexp
  ansible.builtin.lineinfile:
    path: /tmp/testfile
    line: 192.168.1.99 foo.lab.net foo
    create: yes

# NOTE: Yaml requires escaping backslashes in double quotes but not in single quotes
- name: Ensure the JBoss memory settings are exactly as needed
  ansible.builtin.lineinfile:
    path: /opt/jboss-as/bin/standalone.conf
    regexp: '^(.*)Xms(\d+)m(.*)$'
    line: '\1Xms${xms}m\3'
    backrefs: yes

# NOTE: Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs.
- name: Validate the sudoers file before saving
  ansible.builtin.lineinfile:
    path: /etc/sudoers
    state: present
    regexp: '^%ADMIN ALL='
    line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
    validate: /usr/sbin/visudo -cf %s

# See https://docs.pythonlang.cn/3/library/re.html for further details on syntax
- name: Use backrefs with alternative group syntax to avoid conflicts with variable values
  ansible.builtin.lineinfile:
    path: /tmp/config
    regexp: ^(host=).*
    line: \g<1>{{ hostname }}
    backrefs: yes

作者

  • Daniel Hokka Zakrissoni (@dhozac)

  • Ahti Kitsik (@ahtik)

  • Jose Angel Munoz (@imjoseangel)