ansible 文件操作 lineinfile & blockinfile (二)

lineinfile

用來確定文件中的特定行存在定罢,或者進(jìn)行修改旁瘫。用來修改單獨(dú)一行時很有用,可以使用python正則表達(dá)式惠况,例如對配置文件進(jìn)行修改宁仔。如果想修改多行可以考慮replace,或者考慮blockinfile 如果想添加/更新一段內(nèi)容权埠。

自己用的不多煎谍,記下來避免忘了??

主要參數(shù):

  1. path: 文件位置 (Aliases: dest, destfile, name)
  2. line:想要插入或替換的行
  3. regexp: 正則表達(dá)式。使用該表達(dá)式去搜索行,只有最后一行會被修改

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'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蓄坏,隨后出現(xiàn)的幾起案子丑念,更是在濱河造成了極大的恐慌,老刑警劉巖脯倚,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件推正,死亡現(xiàn)場離奇詭異恍涂,居然都是意外死亡植榕,警方通過查閱死者的電腦和手機(jī)再沧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門尊残,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人什燕,你說我怎么就攤上這事竞端∈杭矗” “怎么了事富?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵统台,是天一觀的道長。 經(jīng)常有香客問我贱勃,道長谤逼,這世上最難降的妖魔是什么仇穗? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任纹坐,我火速辦了婚禮,結(jié)果婚禮上耘子,老公的妹妹穿的比我還像新娘。我一直安慰自己谷誓,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布端盆。 她就那樣靜靜地躺著费封,像睡著了一般。 火紅的嫁衣襯著肌膚如雪焚鹊。 梳的紋絲不亂的頭發(fā)上韧献,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機(jī)與錄音璧针,去河邊找鬼渊啰。 笑死,一個胖子當(dāng)著我的面吹牛绘证,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播胞枕,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼魏宽,長吁一口氣:“原來是場噩夢啊……” “哼决乎!你這毒婦竟也來了贫悄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鸭津,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肠缨,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了脑慧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡坑律,死狀恐怖囊骤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情也物,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布浪蹂,位于F島的核電站膘魄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏创葡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一洛波、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缚窿,春花似錦焰扳、人聲如沸倦零。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽育瓜。三九已至,卻和暖如春恋脚,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背糟描。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工册倒, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人灿意。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓崇呵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親荒辕。 傳聞我的和親對象是個殘疾皇子犹褒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評論 2 359