Question:
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.
Answer:
You can use the lineinfile
built-in in a loop. Here’s an example:
1 2 3 4 5 6 7 8 9 10 |
- 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' } |