Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I install a systemd service using Ansible?

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?

like image 376
ATOzTOA Avatar asked Oct 18 '16 16:10

ATOzTOA


People also ask

What is systemd module in Ansible?

Ansible SystemD module helps to control the systemd units such as services and timers created on the Linux server. SystemD is a daemon that manages the services and timer units on the Linux system and we mostly interact with it using the following ways. service file. systemctl command. journalctl command.

How do I start Ansible service?

Use systemctl restart ansible-tower to restart services on clustered environments instead. Also you must restart each cluster node for certain changes to persist as opposed to a single node for a localhost install. For more information on clustered environments, see the Clustering section.

What is the purpose of systemd?

systemd is a software suite that provides an array of system components for Linux operating systems. Its main aim is to unify service configuration and behavior across Linux distributions; Its primary component is a "system and service manager"—an init system used to bootstrap user space and manage user processes.


1 Answers

I use the template module to install the .service file into the /etc/systemd/system. According to this digital ocean blog post /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.

- name: install myservice systemd unit file   template: src=myservice.j2 dest=/etc/systemd/system/myservice.service  - name: start myservice   systemd: state=started name=myservice daemon_reload=yes   # For ansilble < 2.2 only #- name: reload systemd unit configuration #  command: systemctl daemon-reload 
like image 96
Peter Lyons Avatar answered Oct 09 '22 07:10

Peter Lyons