ansible.builtin.lineinfile 模块 – 管理文本文件中的行
注意
此模块是 ansible-core
的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使不指定 collections 关键词,您也可以使用简短的模块名称 lineinfile
。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.lineinfile
,以便轻松链接到模块文档并避免与可能具有相同模块名称的其他集合冲突。
概要
此模块确保特定行位于文件中,或使用反向引用的正则表达式替换现有行。
这主要在您只想更改文件中的一行时很有用。
如果您想更改多个类似的行,请参阅 ansible.builtin.replace 模块;如果您想在文件中插入/更新/删除一行代码块,请检查 ansible.builtin.blockinfile;对于其他情况,请参阅 ansible.builtin.copy 或 ansible.builtin.template 模块。
参数
参数 |
注释 |
---|---|
生成的 filesystem 对象应具有的属性。 要获取支持的标志,请查看目标系统上 chattr 的手册页。 此字符串应包含与 lsattr 显示的顺序相同的属性。 默认情况下假定使用 |
|
与 如果设置, 此参数会稍微更改模块的操作; 如果 与 选项
|
|
创建一个包含时间戳信息的备份文件,以便您可以找回原始文件,以防您意外地将其覆盖。 选项
|
|
与 如果设置, 选项
|
|
应该拥有 filesystem 对象的组的名称,如同提供给 chown 一样。 如果未指定,它将使用当前用户的当前组,除非您是 root 用户,在这种情况下,它可以保留以前的拥有权。 |
|
与 如果指定,则该行将在指定正则表达式的最后一次匹配之后插入。 如果需要第一次匹配,请使用 (firstmatch=yes)。 可以使用一个特殊值; 如果指定的正则表达式没有匹配项,则将使用 EOF。 如果设置了 如果同时为 不能与 选项
|
|
与 如果指定,则会在指定正则表达式的最后一次匹配之前插入该行。 如果需要第一次匹配,请使用 可用值; 如果指定的正则表达式没有匹配项,则会在文件末尾插入该行。 如果同时为 不能与 选项
|
|
生成的 filesystem 对象应具有的权限。 对于习惯使用/usr/bin/chmod的用户,请记住模式实际上是八进制数。您必须为 Ansible 提供足够的信息才能正确解析它们。为了获得一致的结果,请引用八进制数(例如, 如果不遵循这两个规则中的任何一个而向 Ansible 提供数字,则最终会得到一个十进制数,这将产生意外的结果。 从 Ansible 1.8 开始,模式可以指定为符号模式(例如, 如果未指定 如果未指定 指定 |
|
应拥有 filesystem 对象的用户名称,如同提供给chown一样。 如果未指定,则使用当前用户,除非您是 root 用户,在这种情况下,它可以保留之前的拥有者。 指定数字用户名将被假定为用户 ID 而不是用户名。避免使用数字用户名以避免这种混淆。 |
|
要在文件的每一行中查找的正则表达式。 对于 对于 如果没有匹配正则表达式,则会根据 修改行时,正则表达式通常应同时匹配行的初始状态及其被 使用 Python 正则表达式。请参阅 https://docs.pythonlang.cn/3/library/re.html。 |
|
要在文件的每一行中查找的字面字符串。它不必匹配整行。 对于 对于 如果没有匹配字面表达式,则会根据 |
|
SELinux 文件系统对象上下文的级别部分。 这是 MLS/MCS 属性,有时称为 设置为 |
|
SELinux 文件系统对象上下文的角色部分。 设置为 |
|
SELinux 文件系统对象上下文的类型部分。 设置为 |
|
SELinux 文件系统对象上下文的用户部分。 默认情况下,它使用 设置为 |
|
该行是否存在。 选项
|
|
影响何时使用原子操作来防止目标文件系统对象的数据损坏或不一致读取。 默认情况下,此模块使用原子操作来防止目标文件系统对象的数据损坏或不一致读取,但有时系统配置有误或存在故障,从而阻止此操作。一个示例是 Docker 挂载的文件系统对象,无法从容器内部以原子方式更新,只能以非安全方式写入。 此选项允许 Ansible 在原子操作失败时回退到不安全的文件系统对象更新方法(但是,它不会强制 Ansible 执行不安全写入)。 重要!不安全写入容易出现竞争条件,并可能导致数据损坏。 选项
|
|
在将更新后的文件复制到最终目标之前运行的验证命令。 使用临时文件路径进行验证,通过“%s”传入,必须像下面的示例一样存在。 此外,命令以安全方式传递,因此 shell 功能(例如扩展和管道)将不起作用。 有关如何处理比此选项提供的更复杂的验证方法的示例,请参阅 处理复杂验证。 |
属性
属性 |
支持 |
描述 |
---|---|---|
支持:完全支持 |
可以在 check_mode 下运行并返回更改状态预测,而无需修改目标,如果不支持,则将跳过该操作。 |
|
支持:完全支持 |
在差异模式下,将返回有关已更改内容(或在 check_mode 中可能需要更改的内容)的详细信息。 |
|
平台:posix |
可以对其进行操作的目标操作系统/系列。 |
|
支持:完全支持 |
使用 Ansible 的严格文件操作功能来确保正确的权限并避免数据损坏。 |
|
支持:不支持 |
可以自动解密 Ansible 保险库文件。 |
备注
另请参阅
另请参阅
- ansible.builtin.blockinfile
插入/更新/删除由标记行包围的文本块。
- ansible.builtin.copy
将文件复制到远程位置。
- ansible.builtin.file
管理文件和文件属性。
- ansible.builtin.replace
使用反向引用的正则表达式替换文件中特定字符串的所有实例。
- ansible.builtin.template
将文件模板输出到目标主机。
- 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