首页 > 解决方案 > 多次将同一行添加到 yaml 文件中(ansible playbook)

问题描述

我想在特定的行之间多次添加一行。我尝试使用我的剧本,但只添加到最后一个,它应该与 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

标签: regexansibleyaml

解决方案


尝试replace

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

推荐阅读