As already mentioned within the comments it is recommended to use an other approach, in exampe the ini_file
module.
Manage (add, remove, change) individual settings in an INI-style file without having to manage the file as a whole with, say, ansible.builtin.template
...
For a test.service
file
[Service]
ExecStart=-/sbin/agetty -o '-p -- \\u' --noclear %I $TERM
a minimal example playbook
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- ini_file:
path: "/home/{{ ansible_user }}/test/test.service"
section: Service
option: ExecStart
value: "-/sbin/agetty -a {{ ansible_user }} %I $TERM"
will result into the output of
[Service]
ExecStart = -/sbin/agetty -a user %I $TERM
Similar Q&A