Ansible Module lineinfile: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:


The module is mainly used to look for or change a single line in a text file. A typical use case is to ensure that a particular line is in a file, based on a regular expression. Another case is to replace an existing line using a <font color=darkgray>back-referenced</font> regular expression.
The module is mainly used to look for or change a single line in a text file. A typical use case is to ensure that a particular line is in a file, based on a regular expression. Another case is to replace an existing line using a <font color=darkgray>back-referenced</font> regular expression.
=Module Parameters=
==<tt>state</tt>==
==<span id='line'></span><tt>line</tt> (<tt>value</tt>)==
The line to be inserted.
==<tt>insertafter</tt>==
Used with <code>[[#state|state]]=present</code>. The specified line will be inserted after the last match of the regular expression presented as value of <tt>insertafter</tt>, or, if the regular expression is not provided, after EOF.


=Example=
=Example=

Revision as of 04:31, 5 July 2021

External

Internal

Overview

The module is mainly used to look for or change a single line in a text file. A typical use case is to ensure that a particular line is in a file, based on a regular expression. Another case is to replace an existing line using a back-referenced regular expression.

Module Parameters

state

line (value)

The line to be inserted.

insertafter

Used with state=present. The specified line will be inserted after the last match of the regular expression presented as value of insertafter, or, if the regular expression is not provided, after EOF.

Example

- name: "Adjust Java version to {{ jdk.version }}"
  lineinfile:
    path: "{{ settings_path }}/settings.conf"
    regexp: '^JAVA_VERSION='
    line: "JAVA_VERSION={{ jdk.version }}"

Use Cases

Append Lines to a Text File in a Loop