【问题标题】:inserting text after a specific searchng for a particular text in ansible在ansible中特定搜索特定文本后插入文本
【发布时间】:2019-11-21 00:15:44
【问题描述】:

我想在搜索特定字符串或文本后插入/添加一行


header 1
      text1
      text2

header 2
      text4
      text5

预期输出

header 1
      text1
      text2
      text3 

header 2
      text4
      text5

我尝试过使用 ansible lineinfile、blockinfile、replace、regexp。我能够添加所需的文本,但它在文件末尾添加,如下所示

header 1
      text1
      text2


header 2
      text4
      text5

      text3

我希望它恰好添加到 header1 下和 text2 之后

【问题讨论】:

标签: ansible


【解决方案1】:

下面是你要找的任务

- lineinfile:
    path: "/tmp/test.txt"
    insertafter: '^\s*text2\s*$'
    line: '      text3'

之前

$ cat /tmp/test.txt 
header 1
      text1
      text2

header 2
      text4
      text5

之后

$ cat /tmp/test.txt 
header 1
      text1
      text2
      text3

header 2
      text4
      text5

【讨论】:

    【解决方案2】:

    请尝试以下方法

      - name: Test
        lineinfile:
         path: /tmp/file.txt
         line: '      text2\n      text3'
         regexp: '      text2'
         backrefs: yes
    

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多