Ansible Playbook to start services
We have already seen how to run Ansible Adhoc Command, now let’s focus on a playbook to start any service on your servers, In the example below I have started crond service but you can use it to suit your needs.
#Ansible Playbook to start service
-
name: Start Services
hosts: webservers
become: true
tasks:
- name: Start Services
service:
name: crond
state: started
If you have multiple host groups then replace hosts: webservers with below
hosts:
- webserversUSA
- webserversASIA
- webserversEurope
You may have a situation where you want to start multiple services, In that case, you can use ansible loop the example playbook is as below.
#Ansible Playbook to start service
-
name: Start Services
hosts: webservers
become: true
tasks:
- name: Start Services
service:
name: '{{ item }}'
state: started
loop:
- httpd
- crond
- and-so-on