We are running ansible playbook on every boot and it does bunch of checks and configuration tasks on the host. We also have few systemd services that needs to run on that host. How do I make those services dependent on the ansible playbook completion? I want those services to run only after my ansible playbook execution is complete. Is there any direct way in systemd to establish that dependency or should I write some file on path after ansible completion and use that as a check in my systemd service file? Please let me know if you have a better way of doing this.
-
1How do you execute your ansible playbook? Is it a systemd unit or something else? – Gerald Schneider Nov 05 '20 at 09:50
-
Yes it is a systemd unit file that is run on every boot – ram Nov 05 '20 at 14:56
-
Then where is the problem to define dependencies? – Gerald Schneider Nov 05 '20 at 14:56
-
How do I add dependency is my question. I cant simply say app service depends on ansible service right? Or can I? I want the app to start after ansible playbook execution is complete. – ram Nov 05 '20 at 22:36
3 Answers
It seems to me that it would be easier to start those services at the end of the Ansible playbook rather than trying to implement that in systemd. I would just add:
- name: start service
service:
name: service_name
state: started
to the end of the playbook. Ansible fails fast so that when that task is at the end of the play the completion of the rest of the tasks is ensured.

- 9,380
- 2
- 28
- 39
-
Hi Henrik. Thanks for your suggestion but the above is not an option for us. The playbook is generic and runs on multiple hosts but the services vary from host to host. We dont have a defined list of services to start that from inside ansible playbook. – ram Nov 04 '20 at 19:03
-
-
@MichaelHampton I dont decide which services to start. Both steps are done by different teams. Only the base ansible playbook in under my control. I will need to tell the other team to start their services only after this playbook run is complete. – ram Nov 05 '20 at 11:47
You could configure some variables if you say the playbook is generic, a short example:
Hosts file:
[t1]
host1
[t2]
host2
[t1:vars]
service=chronyd
[t2:vars]
service=ntpd
[t:children]
t1
t2
You run it against - hosts: t
, at the end of the play:
- name: start specific
service:
name: "{{ service }}"
state: started

- 136
- 2
Create drop-ins for the unit definitions of the services you want to start after your script.
drop-ins are files that override directives in the original unit definition. They belong into directories with the schema /etc/systemd/system/<servicename>.service.d
.
For a concrete example, let's use apache.
First you need to find out the current After=
line of the unit definition. You can find the unit definition by running systemctl status
:
$ systemctl status apache2
* apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Now let's take a look at /lib/systemd/system/apache2.service
:
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
The After=
line is what we want to modify.
Next we create a directory for the drop-in files (You could have this created automatically using sudo systemctl edit apache2.service
, but I want you to know how this works):
sudo mkdir /etc/systemd/system/apache2.service.d
Into this directory you can place one ore more drop in files.
sudo vi /etc/systemd/system/apache2.service.d/10-after-ansible.conf
This drop in file then only contains the lines we want to override:
[Unit]
After=network.target remote-fs.target nss-lookup.target my-ansible-script.service
Save the file and have systemd reload the service definitions:
sudo systemctl daemon-reload
From now on the apache2
service will only start after my-ansible-script
ran.
You can add Requires=
in the same file if needed.

- 23,274
- 8
- 57
- 89