【问题标题】:Want to insert a text in a file beforealine using lineinfile in ansible playbook想在ansible playbook中使用lineinfile在文件中插入文本
【发布时间】:2020-04-18 06:23:21
【问题描述】:

我需要在文件中的参数末尾添加一个文本。我需要在</abc> 之前插入我的文本。我正在使用 lineinfile,它在<abc> 之前输入文本而不是</abc>

我当前的文本文件:

<domain-log-broadcast-filter>MultiDataSourceLogFilter</domain-log-broadcast-filter>
      <abc>Info is already here. i just need to append the text at end of this parameter. new values to be inserted here</abc>

我的剧本:

  - lineinfile:
         path: /tmp/tochange.xml
         insertbefore:  '\<\/abc\>'
         line: "Tisisthelinetobeinsertedbyme"
         state: present

我的输出是:

<domain-log-broadcast-filter>MultiDataSourceLogFilter</domain-log-broadcast-filter>
Thisisthelinetobeinsertedbyme
      <abc>Info is already here. i just need to append the text at end of this parameter. new values to be inserted here</abc>

现在我有两个要求:

  1. 我想在&lt;/abc&gt; 之前插入我的文本
  2. 我的文件中有 4 个&lt;/abc&gt; 参数。我想忽略第一个 &lt;/abc&gt; 并在其他 3 个 &lt;/abc&gt; 参数之前插入该行。

请给我一个最好的方法,我只需要完成这项工作,使用剧本中的任何模块任何脚本。提前致谢。

【问题讨论】:

    标签: shell ansible devops ansible-template


    【解决方案1】:

    问:“...与剧本中的任何模块任何脚本。”

    答:Ansible file modules 做不到。您需要模块 commandshell 来运行文件编辑工具,例如 sedawk

    模块lineinfile 无法满足要求。该模块修改文件中的单行。无法更改匹配 regexp 的 4 行中的 3 行。恐怕replaceblockinfile 模块都不会帮助你。模块template 修改整个文件。下一个选项可能是模块patch

    引用lineinfile:

    "...对于其他情况,请参阅复制或模板模块。"

    【讨论】:

    • 你能帮我解决任何可以完成这项工作的模块或外壳吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 2022-01-22
    • 2021-01-06
    • 2021-04-13
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多