如何在多个文件上使用ansible blockinfile?

问题描述 投票:0回答:2

嗨,我想在多个文件上使用 blockinfile 函数来更改每个文件中的文本块。

不幸的是blockinfile不支持这个功能。有人可以帮助我吗?

ansible
2个回答
3
投票

要在多个文件上使用 blockinfile 来更改文本块...

您可能想要创建模板并循环 blockinfile 模块。

- blockinfile:
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.template }}"
    create: yes
    path: "{{ item.file }}"
    block: "{{ lookup('template', item.template) }}"
  loop: "{{ files_templates }}"

2
投票

你可以这样做:

- name: Add same block of text in multiple files/paths
  blockinfile:
    path: "{{ item.path }}"
    marker: "###### {mark} Ansible Config #####"
    insertafter: EOF
    state: present
    block: |
     # Some random text comment
     Some random command1
     Some random command2

  loop:
    - {path: '/your/path/one'}
    - {path: '/your/path/two'}
© www.soinside.com 2019 - 2024. All rights reserved.