How can I install a systemd service using Ansible?

Question:

I want to install a systemd service from a Jinja2 template. How do I do this?

Do I have to use copy module to copy the file to /lib/systemd/system and then use systemd module to enable it?

Is there a better way?

Answer:

I use the template module to install the .service file into the /etc/systemd/system. /lib/systemd/system should be reserved for packages bundled with the OS itself, and third party services should be defined in /etc/systemd/system.

With ansible’s systemd module I’d start the service with daemon_reload=yes.

Prior to Ansible 2.2: I do a systemctl daemon-reload afterward (can use an ansible handler for this if appropriate) to prod systemd to pick up the new file.

Leave a Reply