Ansible module-lineinfile example, ansible-lineinfile

Source: Internet
Author: User

Ansible module-lineinfile example, ansible-lineinfile
Lineinfile module details

The lineinfile module is similar to the sed tool in linux, but the articles on the Internet generally only have simple instances, and there are no examples of complicated points.

The following is a common example of the lineinfile Module I have summarized based on my actual operations.

Directory

Ansible-doc lineinfile official documentation (English) -- skip this step and check the example.

1. Requirement: Replace the Matching target value...

2. Requirement: Add rows before matching values...

3. Requirement: add a line after the matching value...

4. Requirement: Replace the row when matching, and add a new row if not matching...

Ansible-doc lineinfile

> LINEINFILE

 

This module will search a file for a line, and ensure that it is present or absent. this is primarily useful when you want to change a single line in a file only. see the [replace] module if you want to change multiple, similar lines or chec [blockinfile] if you want to insert/update/remove a block of lines in a file. for other cases, see the [copy] or [template] modules.

 

Options (= is mandatory ):

 

-Backrefs

Used with 'State = present '. If set, line can contain backreferences (both positional and named) that will get populated

If the 'regexp' matches. this flag changes the operation of the module slightly; 'insert' and 'insertafter 'will be ignored, and if the 'regexp' doesn' t match anywhere in the file, the file will be left unchanged. if the 'regexp 'does match, the last matching line will be replaced by the expanded line parameter.

(Choices: yes, no) [Default: no]

-Backup

Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.

(Choices: yes, no) [Default: no]

-Create

Used with 'State = present '. If specified, the file will be created if it does not already exist. By default it will fail if the file is missing.

(Choices: yes, no) [Default: no]

= Dest

The file to modify.

-Group

Name of the group that shocould own the file/directory, as wocould be fed to 'chown'

[Default: None]

-Insertafter

Used with 'State = present '. if specified, the line will be inserted after the last match of specified regular expression. A special value is available; 'eof 'for inserting the line at the end of the file. if specified regular expression has no matches, EOF will be used instead. may not be used with 'backrefs '.

(Choices: EOF, * regex *) [Default: EOF]

-Insertbefore

Used with 'State = present '. if specified, the line will be inserted before the last match of specified regular expression. A value is available; 'bof' for inserting the line at the beginning of the file. if specified regular expression has no matches, the line will be inserted at the end of the file. may not be used with 'backrefs '.

(Choices: BOF, * regex *) [Default: (null)]

-Line

Required for 'State = present '. the line to insert/replace into the file. if 'backgrounds' is set, may contain backreferences that will get expanded with the 'regexp' capture groups if the regexp matches.

[Default: (null)]

-Mode

Mode the file or directory shocould be. for those used to '/usr/bin/chmod' remember that modes are actually octal numbers (like 0644 ). leaving off the leading zero will likely have unexpected results. as of version 1.8, the mode may

Be specified as a symbolic mode (for example, 'U + rwx 'or 'U = rw, g = r, o = R ').

[Default: None]

-Others

All arguments accepted by the [file] module also work here.

[Default: (null)]

-Owner

Name of the user that shocould own the file/directory, as wocould be fed to 'chown'

[Default: None]

-Regexp

The regular expression to look for in every line of the file. For 'State = present ', the pattern to replace if found;

Only the last line found will be replaced. For 'State = absent ', the pattern of the line to remove. Uses Python regular expressions; see http://docs.python.org/2/library/re.html.

[Default: (null)]

-Selevel

Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the 'range'. '_ default'

Feature works as for 'seuser '.

[Default: s0]

-Serole

Role part of SELinux file context, '_ default' feature works as for 'seuser '.

[Default: None]

-Setype

Type part of SELinux file context, '_ default' feature works as for 'seuser '.

[Default: None]

-Seuser

User part of SELinux file context. Will default to system policy, if applicable. If set to '_ default', it will use the 'USER' portion of the policy if available

[Default: None]

-State

Whether the line shoshould be there or not.

(Choices: present, absent) [Default: present]

-Unsafe_writes

Normally this module uses atomic operations to prevent data upload uption or inconsistent reads from the target files, sometimes systems are configured or just broken in ways that prevent this. one example are docker mounted files, they cannot be updated atomically and can only be done in an unsafe manner.

This boolean option allows ansible to fall back to unsafe methods of updating files for those cases in which you do not have any other choice. be aware that this is subject to race conditions and can lead to data Partition uption.

[Default: False]

-Validate

The validation command to run before copying into place. the path to the file to validate is passed in via '% s' which must be present as in the example below. the command is passed securely so shell features like expansion and pipes won't work.

The ansible version is as follows:

Instance

The following describes how to modify a firewall file to learn the most common modification requirements.

/Etc/sysconfig/iptablesThe file is as follows:

 

Variable

TOMCAT_PORT = 6000 

1. Requirement: Replace the Matching target value

In the file "dest", use a regular expression to match the "regexp" value:

If "regexp" value = 1: Replace "regexp" with "line ";

If the value matches "regexp"> 1: only the valid value of the last match is replaced;

If the value matches "regexp" = 0: Check whether "backrefs" exists and the value is equal to yes. If yes, no operation is performed. Otherwise, a line "line" is added at the end of the file ";

-Name: Lineinfile-iptables

Lineinfile:

Dest:/etc/sysconfig/iptables

Regexp: "rther: other start"

Line: "{item. line }}"

Backrefs: yes

With_items:

-{Line: '-a input-p tcp-m multiport -- dports {TOMCAT_PORT}-j ACCEPT '}

First Run

 

 

Second run

 

 

The result of the third operation is the same as that of the second operation because "backrefs: yes" exists.

2. Requirement: Add rows before matching values

In the file "dest", match the "insertbefore" text:

If the value of "insertbefore" is 1: The line before the value of "insertbefore" is matched, the line is precisely matched. If the line matches the value of "line", no operation is performed; if no match is found, the line "line" is added before the value of "insertbefore ".

If the value of "insertbefore" is matched to> 1: only the last "insertbefore" value is the valid value.

-Name: Lineinfile-iptables

Lineinfile:

Dest:/etc/sysconfig/iptables

Line: "{item. line }}"

Insertbefore: "rrule: other end"

With_items:

-{Line: '-a input-p tcp-m multiport -- dports {TOMCAT_PORT}-j ACCEPT '}

 

3. Requirement: Add rows after matching values

In the file "dest", match the "insertafter" text:

If the value of "insertafter" is matched to 1: The line before the value of "insertafter" is matched to the exact line "line". If the line matches the value of "line", no operation is performed; if no match is found, the line "line" is added before the value of "insertafter ".

If the value of "insertafter" is matched to> 1: only the last "insertafter" value is the valid value.

-Name: Lineinfile-iptables

Lineinfile:

Dest:/etc/sysconfig/iptables

Line: "{item. line }}"

Insertafter: "rther: other start"

With_items:

-{Line: '-a input-p tcp-m multiport -- dports {TOMCAT_PORT}-j ACCEPT '}

 

4. Requirement: Replace the row when matching, and add the row if not matching.

Match "regexp" first. If it matches, it is directly replaced with "line". If it does not match, it is divided into the following situations.

① Exist"Backrefs: yes"Parameters:

A matches the "insertbefore" text or "insertafter" text. If none of them match, no operation is performed.

B matches the "insertbefore" text (matching multiple last valid ones), and matches "line" from the "insertbefore" text. If no match is found, the line "line" is added ", otherwise, no operation is performed.

C matches the "insertafter" text (matching multiple last valid ones) and matches "line" from the "insertafter" text. If no match is found, the line "line" is added ", otherwise, no operation is performed.

② Does not exist"Backrefs: yes"Parameters:

A matches the "insertbefore" text or "insertafter" text. If none of them match, A line "line" is added to the end of the file ".

B matches the "insertbefore" text (matches multiple last valid ones), and adds a line "line" directly before the "insertbefore" text ".

C matches the "insertafter" text (matches multiple last valid ones) and adds a line "line" after the "insertafter" text ".

-Name: Lineinfile-iptables

Lineinfile:

Dest:/etc/sysconfig/iptables

Regexp: "rther: other start"

Line: "{item. line }}"

Insertbefore: "rrule: other end"

# Insertafter: "rther: other start"

Backrefs: yes

With_items:

-{Line: '-a input-p tcp-m multiport -- dports {TOMCAT_PORT}-j ACCEPT '}

First Run

 

Second run

 

If backrefs: yes exists, the result of the third operation is the same as that of the second operation.

 

If backrefs: yes does not exist, the result of the third operation

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.