【问题标题】:Multiple times adding same line into yaml file (ansible playbook)多次将同一行添加到 yaml 文件中(ansible playbook)
【发布时间】:2020-02-27 13:34:24
【问题描述】:

我想在特定的行间多次添加一行。我尝试使用我的剧本,但只添加到最后一个,它应该与 From Earth 添加相同的行。

我的剧本

- name: Update the file
  lineinfile:
    dest: /sample/config.file
    insertafter: ' Earth .* '
    line: '    This is template'

我的初始文件

Hello World
   [From Earth]
Hello World
   [From Earth]
Hello World
   [From Earth]
Hello World
   [From Earth]

我的愿望输出

Hello World
   [From Earth]
   This is template
Hello World
   [From Earth]
   This is template
Hello World
   [From Earth]
   This is template
Hello World
   [From Earth]
   This is template

【问题讨论】:

    标签: regex ansible yaml


    【解决方案1】:

    试试replace:

     - name: Replace
       replace:
           path: file.txt
           regexp: 'Earth]\n(?!{{repl_str}})'
           replace: 'Earth]\n{{repl_str}}'
       vars:
         repl_str: '   This is template\n'
    

    【讨论】:

    • 我尝试了您的解决方案,但我错过了一些东西并更新了我的问题并添加了特殊字符 []。我怎么能这样使用?
    • 为什么需要特殊字符?例子?
    • 我更新了我的主要问题文件和输出部分。 [来自地球]
    • 啊,我不得不刷新。更新了添加字符的答案。
    • 这不会是幂等的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    相关资源
    最近更新 更多