Ansible Module lineinfile: Difference between revisions
(6 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
==<span id='line'></span><tt>line</tt> (<tt>value</tt>)== | ==<span id='line'></span><tt>line</tt> (<tt>value</tt>)== | ||
The line to be inserted. | The line to be inserted. | ||
==<tt>create</tt>== | |||
Used with <code>[[#state|state]]=present</code>. If "yes" os specified, the file will be created if it does not already exist. By default, the value is "no", so the module will fail if the file is missing. | |||
==<tt>insertafter</tt>== | ==<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. | 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. | ||
Line 18: | Line 21: | ||
<font color=darkgray>TODO</font>. | <font color=darkgray>TODO</font>. | ||
= | =Use Cases= | ||
====Insert a Line to a Text File==== | |||
<syntaxhighlight lang='yaml'> | |||
- name: Insert a line into a file if it does not exist already | |||
lineinfile: | |||
path: test.txt | |||
line: 'this is a line' | |||
create: yes | |||
</syntaxhighlight> | |||
The line will be inserted, but on subsequent executions, the file will not be changed anymore. | |||
====Append Lines to a Text File in a Loop==== | |||
<syntaxhighlight lang='yaml'> | |||
- name: Append lines to a text file | |||
lineinfile: | |||
path: test.txt | |||
line: 'line {{ item }}' | |||
create: yes | |||
with_items: | |||
- A | |||
- B | |||
- C | |||
</syntaxhighlight> | |||
The result will be: | |||
<syntaxhighlight lang='text'> | |||
line A | |||
line B | |||
line C | |||
</syntaxhighlight> | |||
upon the first execution, then the file will not be changes upon subsequent executions. | |||
====Adjust a Line based on a Regular Expression==== | |||
<syntaxhighlight lang='yaml'> | <syntaxhighlight lang='yaml'> | ||
- name: "Adjust Java version to {{ jdk.version }}" | - name: "Adjust Java version to {{ jdk.version }}" | ||
Line 27: | Line 59: | ||
line: "JAVA_VERSION={{ jdk.version }}" | line: "JAVA_VERSION={{ jdk.version }}" | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 04:47, 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.
create
Used with state=present
. If "yes" os specified, the file will be created if it does not already exist. By default, the value is "no", so the module will fail if the file is missing.
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.
TODO.
Use Cases
Insert a Line to a Text File
- name: Insert a line into a file if it does not exist already
lineinfile:
path: test.txt
line: 'this is a line'
create: yes
The line will be inserted, but on subsequent executions, the file will not be changed anymore.
Append Lines to a Text File in a Loop
- name: Append lines to a text file
lineinfile:
path: test.txt
line: 'line {{ item }}'
create: yes
with_items:
- A
- B
- C
The result will be:
line A
line B
line C
upon the first execution, then the file will not be changes upon subsequent executions.
Adjust a Line based on a Regular Expression
- name: "Adjust Java version to {{ jdk.version }}"
lineinfile:
path: "{{ settings_path }}/settings.conf"
regexp: '^JAVA_VERSION='
line: "JAVA_VERSION={{ jdk.version }}"