Ansible: lineinfile for several lines

ansible

The same way there is a module lineinfile to add one line in a file, is there a way to add several lines?

I do not want to use a template because you have to provide the whole file.
I just want to add something to an existing file without necessarily knowing what the file already contains so a template is not an option.

Best Answer

You can use the lineinfile built-in in a loop. Here's an example:

- name: Set some kernel parameters
  lineinfile:
    dest: /etc/sysctl.conf
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
  loop:
    - { regexp: '^kernel.shmall', line: 'kernel.shmall = 2097152' }
    - { regexp: '^kernel.shmmax', line: 'kernel.shmmax = 134217728' }
    - { regexp: '^fs.file-max', line: 'fs.file-max = 65536' }
Related Topic