Ansible add multiple lines to configuration file. Add multiple lines in your configuration file. The line will be replaced with the old one. You can set after what will the line be inserted. Use create to make sure the file is there, it also handles directories.
Source code viewer
- name: Save lines to conf file lineinfile: dest: /etc/systemd/system/some.service.d/file.conf # Use create to make sure the file is there, it also handles directories. create: yes line: "{{ item.line }}" insertafter: "{{ item.insertafter }}" state: present # Iterate through these items. with_items: - { insertafter: EOF, line: '[Category]' } - { insertafter: '^\[Category\]$', line: 'Command=' } - { insertafter: '^Command=$', line: 'Command=/usr/bin/dir -v' } # Use sudo for saving. become: yesProgramming Language: YAML