You are here

27 October 2016

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
  1. - name: Save lines to conf file
  2.   lineinfile:
  3.   dest: /etc/systemd/system/some.service.d/file.conf
  4. # Use create to make sure the file is there, it also handles directories.
  5.   create: yes
  6.   line: "{{ item.line }}"
  7.   insertafter: "{{ item.insertafter }}"
  8.   state: present
  9. # Iterate through these items.
  10.   with_items:
  11.   - { insertafter: EOF, line: '[Category]' }
  12.   - { insertafter: '^\[Category\]$', line: 'Command=' }
  13.   - { insertafter: '^Command=$', line: 'Command=/usr/bin/dir -v' }
  14. # Use sudo for saving.
  15.   become: yes
Programming Language: YAML