lineinfile模块类似linux工具中的sed工具

图片 10

lineinfile模块详整

最后叁次校正于2017-05-24
10:34分,个人原创,转摘请加上原作链接

lineinfile模块相通linux工具中的sed工具,可是网络的篇章通常都唯有简要的实例,复杂点的例证都尚未

上边是自己根据实操计算出来的lineinfile模块的科学普及例子,分享给我们参谋

目录

ansible-doc lineinfile官方文书档案(罗马尼亚语)–看不懂跳过直接看例子

1、须求:替换相配的靶子值… 

2、供给:在匹配值在此以前增添行… 

3、供给:在相配值之后增添行… 

4、须要:相配到就替换行,未相称到就大幅度增加行… 

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; `insertbefore’ 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 should own the file/directory, as would 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 `backrefs’ 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 should 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 should own the file/directory, as would 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 should be there or not.

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

– unsafe_writes

        Normally this module uses atomic operations to prevent data corruption 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 corruption.

        [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.

ansible版本如下

图片 1

实例

下边用校勘防火墙文件来比喻,学习最广泛的改革须求

/etc/sysconfig/iptables文件如下:

图片 2

 

变量

TOMCAT_PORT=6000 

1、必要:替换相称的靶子值

在文书”dest”中,用正则表达式相称“regexp”值:

若相称到“regexp”值=1:替换“regexp”值所在行为 “line”;

若相配到“regexp”值>1:只替换最终三个技艺极其精巧的有效值;

若相称到“regexp”值=0:检查是不是留存“backrefs”,并且值等于yes,即使则不做别的操作;不然在文件末尾新增加行“line”;

– name: Lineinfile-iptables

  lineinfile:

    dest: /etc/sysconfig/iptables

    regexp: "ruler:other start"

line: "{{item.line}}"

backrefs: yes

  with_items:

    – { line: ‘-A INPUT -p tcp -m multiport –dports {{TOMCAT_PORT}} -j ACCEPT’}

先是次运营

 图片 3

 

第三回运营

 图片 4

 

其三遍运行由于存在“backrefs: yes”,故结果同第一次同样。

2、需要:在相配值早先增添行

 

在文书”dest”中,故意设定regexp相配不到,然后相称“insertbefore”文本:

若相称到“insertbefore”值=1:相配“insertbefore”值从前的行,准确相配行“line”;若匹配行“line”到,不做其余操作;若未相配到,则在“insertbefore”值早前增添行“line”。

若相配到“insertbefore”值>1:则独有最终一个“insertbefore”值为使得相称值,别的同上。

– name: Lineinfile-iptables

  lineinfile:

dest: /etc/sysconfig/iptables

regexp: "ruler:other start"

    line: "{{item.line}}"

insertbefore: "ruler:other end"

backrefs: yes

  with_items:

    – { line: ‘-A INPUT -p tcp -m multiport –dports {{TOMCAT_PORT}} -j ACCEPT’}

 图片 5

3、须要:在相称值之后扩充行

在文书”dest”中,故意设定regexp相配不到,然后相配“insertafter”文本:

若相称到“insertafter”值=1:相称“insertafter”值以前的行,正确匹配行“line”;若相配行“line”到,不做任何操作;若未相称到,则在“insertafter”值此前扩张行“line”。

若相配到“insertafter”值>1:则唯有最后三个“insertafter”值为可行匹配值,别的同上。

– name: Lineinfile-iptables

  lineinfile:

dest: /etc/sysconfig/iptables

regexp: "ruler:other start"

    line: "{{item.line}}"

insertafter: " ruler:other start"

backrefs: yes

  with_items:

    – { line: ‘-A INPUT -p tcp -m multiport –dports {{TOMCAT_PORT}} -j ACCEPT’}

 图片 6

小心:尽管不加regexp和backrefs项,那么当相配insertafter的时候,若是相称到就在头里新扩充line,假诺合作不到就能够在文书末尾新扩大line(日常这种气象不是大家所企盼的)

4、须求:相称到就替换行,未相称到就新添行

预先相称“regexp”,若相称到则直接沟通为“line”,未匹配到则分为上面包车型大巴情状

①   存在backrefs: yes 参数:

A相配“insertbefore”文本只怕“insertafter”文本,若都未相称到,则不会做任何操作。

B相称到“insertbefore”文本(相称多个最终三个得力),会从“insertbefore”文本往前相配“line”,若相配不到则新添行“line”,不然不做别的操作。

C相配到“insertafter”文本(相配五个尾数一蹴而就),会从“insertafter”文本未来分外“line”,若相称不到则新添行“line”,不然不做其余操作。

②   不存在backrefs: yes 参数:

A相称“insertbefore”文本或然“insertafter”文本,若都未相配到,则在文书末尾新扩展行“line”。

B相配到“insertbefore”文本(相称七个最终三个可行),直接在“insertbefore”文本前边新添行“line”。

C相称到“insertafter”文本(相称四个最终一个实用),直接在“insertafter”文本前边新扩张行“line”。

– name: Lineinfile-iptables

  lineinfile:

    dest: /etc/sysconfig/iptables

    regexp: "ruler:other start"

    line: "{{item.line}}"

    insertbefore: "ruler:other end"

#    insertafter: "ruler:other start"

    backrefs: yes

  with_items:

    – { line: ‘-A INPUT -p tcp -m multiport –dports {{TOMCAT_PORT}} -j ACCEPT’}

首先次运营

 图片 7

第贰次运营

 图片 8

当存在backrefs: yes,第叁回运转结果与第三遍相符

 图片 9

当不设有backrefs: yes,第2回运营结果

 

 图片 10

注意:假使不加regexp和backrefs项,那么当相配insertbefore的时候,借使同盟到就在前头新扩展line,若是相称不到就能够在文件末尾新扩张line(平日这种状态不是我们所梦想的)