ansible 文件操作 lineinfile & blockinfile (二)

lineinfile

用来确定文件中的特定行存在,或者进行修改。用来修改单独一行时很有用,可以使用python正则表达式,例如对配置文件进行修改。如果想修改多行可以考虑replace,或者考虑blockinfile 如果想添加/更新一段内容。

自己用的不多,记下来避免忘了

主要参数:

  1. path: 文件位置 (Aliases: dest, destfile, name)
  2. line:想要插入或替换的行
  3. regexp: 正则表达式。使用该表达式去搜索行,只有最后一行会被修改

ansible-doc lineinfile:

> LINEINFILE    (/usr/lib/python2.7/site-packages/ansible/modules/files/lineinfile.py)

        This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression. 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 check [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):

- attributes
        Attributes the file or directory should have. To get supported flags look at the man page for `chattr' on the target system. This
        string should contain the attributes in the same order as the one displayed by `lsattr'.
        (Aliases: attr)[Default: None]
        version_added: 2.3

- 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.
        [Default: no]
        type: bool
        version_added: 1.1

- backup
        Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it
        incorrectly.
        [Default: no]
        type: bool

- 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.
        [Default: no]
        type: bool

- firstmatch
        Used with `insertafter' or `insertbefore'. If set, `insertafter' and `inserbefore' find a first line has regular expression
        matches.
        [Default: no]
        type: bool
        version_added: 2.5

- 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. If the
        first match is required, use(firstmatch=yes). 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. If the
        first match is required, use(firstmatch=yes). 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)]
        version_added: 1.1

- 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' or `01777'). 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]

= path
        The file to modify.
        Before 2.3 this option was only usable as `dest', `destfile' and `name'.
        (Aliases: dest, destfile, name)

- 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(s) to remove. Uses Python regular expressions. See
        http://docs.python.org/2/library/re.html.
        [Default: (null)]
        version_added: 1.7

- 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: absent, present)[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]
        type: bool
        version_added: 2.2

- 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.
        [Default: None]


NOTES:
      * As of Ansible 2.3, the `dest' option has been changed to `path' as default, but `dest' still works as well.

AUTHOR: Daniel Hokka Zakrissoni (@dhozac), Ahti Kitsik (@ahtik)
        METADATA:
          status:
          - preview
          supported_by: core

EXAMPLES:

# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
- lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: 'SELINUX=enforcing'

- lineinfile:
    path: /etc/sudoers
    state: absent
    regexp: '^%wheel'

- lineinfile:
    path: /etc/hosts
    regexp: '^127\.0\.0\.1'
    line: '127.0.0.1 localhost'
    owner: root
    group: root
    mode: 0644

- lineinfile:
    path: /etc/httpd/conf/httpd.conf
    regexp: '^Listen '
    insertafter: '^#Listen '
    line: 'Listen 8080'

- lineinfile:
    path: /etc/services
    regexp: '^# port for http'
    insertbefore: '^www.*80/tcp'
    line: '# port for http by default'

# Add a line to a file if it does not exist, without passing regexp
- lineinfile:
    path: /tmp/testfile
    line: '192.168.1.99 foo.lab.net foo'

# Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs.
- lineinfile:
    path: /etc/sudoers
    state: present
    regexp: '^%wheel\s'
    line: '%wheel ALL=(ALL) NOPASSWD: ALL'

# Yaml requires escaping backslashes in double quotes but not in single quotes
- lineinfile:
    path: /opt/jboss-as/bin/standalone.conf
    regexp: '^(.*)Xms(\\d+)m(.*)$'
    line: '\1Xms${xms}m\3'
    backrefs: yes

# Validate the sudoers file before saving
- lineinfile:
    path: /etc/sudoers
    state: present
    regexp: '^%ADMIN ALL='
    line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
    validate: '/usr/sbin/visudo -cf %s'

你可能感兴趣的:(ansible 文件操作 lineinfile & blockinfile (二))