Reputation: 10467
Suppose I need to enable a service and then start the service (e.g. some web server), should I create a task to enable the service then create another task to start the service? Or should I create a task to enable the service, in this service, create a notify, which triggers a handler to start the service?
If using the strategy of notify-handler, then the problem is if the service is successfully enabled but somehow not started, then later on when I run the playbook again. since the service is already enabled, it will not be started anymore.
I think putting them in separate tasks maybe better? In general, how do people implement them in Ansible?
Here is my code if write tasks separately:
tasks:
- name: enable airflow service
systemd:
name: airflow-worker.service
enabled: yes
- name: start airflow service
service:
name: airflow-worker
state: started
Upvotes: 4
Views: 4834
Reputation: 453
Systemd wraps the Centos6 service scripts so you can still manipulate them using systemd. The started state is idempotent, so if it was already started, it won't be marked as a change and you don't need to worry about running it over and over again. You can enable and start in the same task. This is the approach you should use:
tasks:
- name: Start and Enable Airflow
systemd:
name: airflow-worker
state: started
enabled: yes
daemon-reload: yes
A Handler would be better suited for a scenario where a configuration file is changed. The handler can listen for that and restart the service when it is triggered. The restarted state is never idempotent, so it should only be used in a handler scenario.
Upvotes: 9