0

This code is broken because specials characters, I think.

- ansible.builtin.lineinfile:
    path: /etc/systemd/system/getty.target.wants/getty@tty1.service
    regexp: "^ExecStart=-/sbin/agetty -o '-p -- \\u' --noclear %I $TERM"
    line: "ExecStart=-/sbin/agetty -a {{ ansible_user_id }} %I $TERM"

How could I fix that?

U880D
  • 8,601
  • 6
  • 24
  • 40
  • A better approach to work with `systemd` service files is to use the [`template`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html) or [`ini_file`](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html) module. – U880D Mar 22 '23 at 19:02
  • can you please share what makes you think the code is broken? Do you get errors? Is the effect of the code not as expected? What is the effect? – ruud Mar 23 '23 at 08:13
  • I thing my trouble comme from the cloud init images, I used : https://stackoverflow.com/q/75810079/21356847 – Jonathan Coutu Mar 26 '23 at 03:17

2 Answers2

1

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

U880D
  • 8,601
  • 6
  • 24
  • 40
0
ls -al /etc/systemd/system/getty.target.wants/
total 8
drwxr-xr-x  2 root root 4096 Jan 24 04:24 .
drwxr-xr-x 11 root root 4096 Jan 24 04:27 ..
lrwxrwxrwx  1 root root   34 Jan 24 04:24 getty@tty1.service -> /lib/systemd/system/getty@.service
- name: Auto-login on debian
  become: true
  community.general.ini_file:
    path: /lib/systemd/system/getty@.service
    section: Service
    option: ExecStart
    value: "-/sbin/agetty -a {{ ansible_user_id }} %I $TERM"
    mode: "0644"