lineinfile模块详解

最后三遍修改于2017-05-24
10:三十一分,个人原创,转摘请加上原文链接

lineinfile模块类似linux工具中的sed工具,不过网上的篇章一般都唯有简要的实例,复杂点的例证都并未

上面是本人依据实际操作总计出来的lineinfile模块的大面积例子,分享给大家参考

目录

ansible-doc lineinfile官方文档(英文)–看不懂跳过一贯看例子

① 、须要:替换匹配的目标值… 

② 、须求:在匹配值从前扩展行… 

三 、须要:在匹配值之后增加行… 

④ 、需要:匹配到就替换行,未匹配到就剧增行… 

lineinfile模块详解

终极两回修改于2017-05-24
10:三贰十二分,个人原创,转摘请加上原文链接

lineinfile模块类似linux工具中的sed工具,可是网上的小说一般都唯有简要的实例,复杂点的例子都并未

上面是自作者依据实际操作总计出来的lineinfile模块的常见例子,分享给大家参考

目录

ansible-doc lineinfile官方文档(英文)–看不懂跳过直接看例子

一 、须要:替换匹配的对象值… 

二 、须要:在匹配值此前扩展行… 

三 、须求:在匹配值之后扩大行… 

④ 、必要:匹配到就替换行,未匹配到就新增行… 

ansible模块-lineinfile实例讲解,ansible-lineinfile

sed流式编辑器

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 

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版本如下

澳门金沙国际 3

实例

上边用修改防火墙文件来比喻,学习最常见的改动需求

/etc/sysconfig/iptables澳门金沙国际 ,文本如下:

澳门金沙国际 4

 

变量

TOMCAT_PORT=6000 

lineinfile模块详解

lineinfile模块类似linux工具中的sed工具,然而网上的篇章一般都唯有大致的实例,复杂点的事例都并未

上边是自小编按照实际操作计算出来的lineinfile模块的广大例子,分享给我们参考

目录

ansible-doc lineinfile官方文档(英文)–看不懂跳过间接看例子

① 、须要:替换匹配的目的值… 

② 、须要:在匹配值在此以前扩张行… 

叁 、需要:在匹配值之后扩大行… 

④ 、要求:匹配到就替换行,未匹配到就新增行… 

 

一 、需要:替换匹配的对象值

在文书”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’}

首先次运营

 澳门金沙国际 5

 

第二次运营

 澳门金沙国际 6

 

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

① 、必要:替换匹配的对象值

在文书”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’}

lineinfile实例讲解,sed流式编辑器。率先次运行

 澳门金沙国际 7

 

第二遍运转

 澳门金沙国际 8

 

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

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版本如下

澳门金沙国际 9

实例

上边用修改防火墙文件来比喻,学习最普遍的改动须求

/etc/sysconfig/iptables文件如下:

澳门金沙国际 10

 

变量

TOMCAT_PORT=6000 

sed是1个流式编辑器(stream editor)。

贰 、需要:在匹配值以前伸张行

 

在文书”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’}

 澳门金沙国际 11

二 、要求:在匹配值以前扩大行

 

在文书”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’}

 澳门金沙国际 12

壹 、要求:替换匹配的靶子值

在文书”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’}

率先次运维

 澳门金沙国际 13

 

第一遍运维

 澳门金沙国际 14

 

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

 

叁 、须求:在匹配值之后增添行

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

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

若匹配到“insertafter”值>1:则唯有最后3个“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’}

 澳门金沙国际 15

瞩目:假若不加regexp和backrefs项,那么当匹配insertafter的时候,若是合作到就在眼下新增line,如若匹配不到就会在文件末尾新增line(一般那种意况不是大家所企望的)

叁 、必要:在匹配值之后扩充行

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

若匹配到“insertafter”值=1:匹配“insertafter”值以前的行,精确匹配行“line”;若匹配行“line”到,不做其余操作;若未匹配到,则在“insertafter”值从前增添行“line”。

若匹配到“insertafter”值>1:则唯有最后3个“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’}

 澳门金沙国际 16

在意:假诺不加regexp和backrefs项,那么当匹配insertafter的时候,如若同盟到就在眼下新增line,即便匹配不到就会在文件末尾新增line(一般这种场地不是我们所企望的)

二 、须要:在匹配值在此以前增添行

在文件”dest”中,匹配“insertbefore”文本:

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

若匹配到“insertbefore”值>1:则只有最终3个“insertbefore”值为有效匹配值,其他同上。

– name: Lineinfile-iptables

  lineinfile:

    dest: /etc/sysconfig/iptables

    line: "{{item.line}}"

    insertbefore: "ruler:other end"

  with_items:

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

 澳门金沙国际 17

在sed中,pattern space和hold
space为多少个缓冲区,起始时数据由标准输入被读入pattern
space,经过处理输出到正式输出。hold
space只是贰个扶植处理的缓冲区,有些命令可以对hold
space缓冲区操作,比如H,h,g,G等。

④ 、须求:匹配到就替换行,未匹配到就新增行

优先匹配“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”文本(匹配四个最后2个使得),直接在“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’}

率先次运转

 澳门金沙国际 18

其次次运营

 澳门金沙国际 19

当存在backrefs: yes,第二遍运维结果与第3回一样

 澳门金沙国际 20

当不存在backrefs: yes,第几回运营结果

 

 澳门金沙国际 21

注意:如果不加regexp和backrefs项,那么当匹配insertbefore的时候,如果合作到就在前面新增line,如若匹配不到就会在文书末尾新增line(一般那种状态不是我们所期待的)

四 、需要:匹配到就替换行,未匹配到就新增行

优先匹配“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”文本(匹配多个最终1个使得),直接在“insertbefore”文本前面新增行“line”。

C匹配到“insertafter”文本(匹配多少个末了3个立见成效),直接在“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’}

率先次运营

 澳门金沙国际 22

首回运转

 澳门金沙国际 23

当存在backrefs: yes,第3遍运维结果与第三遍一样

 澳门金沙国际 24

当不存在backrefs: yes,首回运营结果

 

 澳门金沙国际 25

留神:假使不加regexp和backrefs项,那么当匹配insertbefore的时候,如若同盟到就在头里新增line,假如匹配不到就会在文件末尾新增line(一般那种景色不是我们所希望的)

叁 、须要:在匹配值之后扩充行

在文件”dest”中,匹配“insertafter”文本:

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

若匹配到“insertafter”值>1:则只有最后贰个“insertafter”值为可行匹配值,其他同上。

– name: Lineinfile-iptables

  lineinfile:

    dest: /etc/sysconfig/iptables

    line: "{{item.line}}"

    insertafter: " ruler:other start"

  with_items:

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

 澳门金沙国际 26

 

四 、须要:匹配到就替换行,未匹配到就新增行

预先匹配“regexp”,若匹配到则一贯沟通为“line”,未匹配到则分为上面的动静

①   存在backrefs: yes 参数:

A匹配“insertbefore”文本可能“insertafter”文本,若都未匹配到,则不会做其余操作。

B匹配到“insertbefore”文本(匹配八个最终1个实用),会从“insertbefore”文本往前匹配“line”,若匹配不到则新增行“line”,否则不做此外操作。

C匹配到“insertafter”文本(匹配两个最终一个管用),会从“insertafter”文本以后极度“line”,若匹配不到则新增行“line”,否则不做其他操作。

②   不存在backrefs: yes 参数:

A匹配“insertbefore”文本可能“insertafter”文本,若都未匹配到,则在文件末尾新增行“line”。

B匹配到“insertbefore”文本(匹配两个最终1个得力),直接在“insertbefore”文本前边新增行“line”。

C匹配到“insertafter”文本(匹配三个最后3个灵光),直接在“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’}

率先次运转

 澳门金沙国际 27

第1遍运营

 澳门金沙国际 28

当存在backrefs: yes,第二遍运转结果与第③回一样

 澳门金沙国际 29

当不存在backrefs: yes,第两遍运转结果

 

 澳门金沙国际 30

lineinfile模块详解
lineinfile模块类似linux工具中的sed工具,然而网上的稿子一般都只有简短的实例…

sed [OPTION]… {script-only-if-no-other-script} [input-file]…

 

那是命令的正式格式:

 

讲述:sed是多少个流式编辑器,流式编辑器用来拍卖对一个输入
流举行拍卖。就好像许多运行脚本编撰的编辑器一样,sed只处理一行输入,由此更迅捷。不过sed在管道中过滤文本的职能使它明确有别于其他系列编辑器。

 

-n,

 –quiet, –silent

 

防止pattern space的自动输出成效

 

-e script, –expression=script

 

把script出席到执行命令中

 

-f

 script-file, –file=script-file

 

命令输入来源为文件

 

-i[SUFFIX],

 –in-place[=SUFFIX]

 

原地编辑

 

-l

 N, –line-length=N

 

设定自动换行的尺寸

 

–posix

 

闭馆全体GNU扩充

 

-r, –regexp-extended

 

利用伸张的正则表明式

 

-s, –separate

 

把多少个文件作为三个流

 

 -u, –unbuffered

 

老是从文件读入更少数据更频仍的flush到输出

 

设若没有-e
恐怕-f选项,第三个非选项参数作为sed脚本运转。其他具备的参数都为输入文件,如若没有输入文件,从标准输入读取。

 

COMMAND SYNOPSIS

这只是2个sed的明显教程

 

零地址命令:

 

:label

 

为 b 和 t命令用的label

 

#comment

 

以至下一行命令都为注释(恐怕-e 命令的终极)

 

零可能三个地方命令:

 

=

 

打印当前行号

 

a text:

 

参与text,可以用反斜杠换行

 

i text:

 

安顿text,可以用反斜杠换行

 

q

 

立马退出sed脚本不处理任何越多的输入,但即使没有禁用自动打印,将打印当前情势空间。

 

Q

 

随即退出sed脚本不处理其余越多的输入

 

r filename

 

加入从filename读入的text

 

R filename

 

加盟从filaname读入一行数据

 

Commands which accept address ranges

b label

 

              Branch to label; if label is omitted, branch to end of
script.

 

跳转到label执行,要是没有就到终极

 

 t label

 

              If a s/// has done a successful substitution since the
last input line was  read  and

 

              since  the  last t or T command, then branch to label; if
label is omitted, branch to

 

              end of script.

 

 

从今上次输入或许上次T/t命令后s替换命令执行成功,跳转到label,没哟label,到终极

 

  T label

              If no s/// has done a successful substitution since the
last input line was read  and

              since  the  last t or T command, then branch to label; if
label is omitted, branch to

              end of script.

自从上次输入只怕上次T/t命令后s替换命令执行不成事,跳转到label,没哟label,到终极

 

c text:

 

轮换text,可以用反斜杠换行

 

d 从下2个循环往复早先删除pattern space

 

D删除pattern space的率先行,假使内部有多少持续处理,否则从读取输入

 

h

 H    Copy/append pattern space to hold space.

 

       g G    Copy/append hold space to pattern space.

 

x      Exchange the contents of the hold and pattern spaces.

 

l      List out the current line in a ”visually unambiguous” form.

明明列出当前行号

 

       n N    Read/append the next line of input into the pattern space.

 

       p      Print the current pattern space.

 

       P      Print up to the first embedded newline of the current
pattern space.

 

       w filename

              Write the current pattern space to filename.

 

 

       W filename

              Write the first line of the current pattern space to
filename.

s/regexp/replacement/

              Attempt to match regexp against the pattern space.  If
successful, replace that  por-

              tion  matched  with replacement.  The replacement may
contain the special character &

              to refer to that portion of the pattern space which
matched, and the special  escapes

              \1 through \9 to refer to the corresponding matching
sub-expressions in the regexp.

用replacement替换regexp匹配的字段,replacement中恐怕带有&字符代指与regexp匹配的字符串,用\1到\9代指相应匹配的子段

 

y/source/dest/

              Transliterate  the characters in the pattern space which
appear in source to the cor-

              responding character in dest.

 

Addresses

一旦三个地点参数也没有,对拥有行开展处理,若是有3个地方,对哪一行进行拍卖,若是有多个地点,对两行之间的行处理,哪怕参数2低于参数1,倘诺地方2是三个正则表明式,不会合作第一个参数行。

 

前方加!符号,反向采取

 

number 具体一行

 

 first~step

 

从first行伊始的大幅为step的行

 

$ 最终一行

 

 /regexp/ 与此正则表达式匹配的一行

 

\cregexpc  c可以为别的字符,同上,只但是把换一种形式

 

addr1,+N 从addr1开始的N行

 

addr1,~N 从addr1伊始的N的整数倍行

sed是二个流式编辑器(stream
editor)。 在sed中,pattern space和hold
space为八个缓冲区,开端时数据由专业输入被读入pattern space,…

相关文章