Ansible Playbook to start services
We have already seen how to run Ansible ad-hoc command now let’s focus on a playbook to start services on your servers, In the example below I have started a 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 an 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