Davide ErTapioca
Davide ErTapioca

Reputation: 31

How to use ansible blockinfile on multiple files?

hi I would like to use the blockinfile function on multiple files going to change blocks of text in each of them.

unfortunately blockinfile does not support this function. can someone help me?

Upvotes: 3

Views: 2670

Answers (2)

solaris
solaris

Reputation: 321

You can do it like this:

- 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'}

Upvotes: 2

Vladimir Botka
Vladimir Botka

Reputation: 68024

To use blockinfile on multiple files to change blocks of text ...

you might want to create templates and loop the blockinfile module.

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

Upvotes: 3

Related Questions