--- - name: Playbook hosts: webservers become: yes become_user: root tasks: - name: ensure apache is at the latest version yum: name: httpd state: latest - name: ensure apache is running service: name: httpd state: started
On the Second task with service module, we are making sure that the service named
httpd is started and running using the
state: started Ansible would not restart the service if it is already started and running.
Ansible playbook can be executed with
ansible-playbook command. like you have
ansible command to execute ad hoc command. This is dedicated for ansible playbooks
Let us see how to execute the preceding playbook and install apache on the
Note*: a host group is a group of hosts and servers mentioned in the ansible inventory file.
➜ Ansible-Examples git:(master) ✗ cat ansible_hosts
Here is the customized Ansible inventory file with two hosts grouped as
Here the host group name is
webservers and it is mentioned in the
hosts: directive on the playbook
Given below is the command syntax or sample to run an ansible playbook.
➜ ansible-playbook sampleplaybook.yml -i ansible_hosts
If you have mentioned all the host groups in your default inventory file
/etc/ansible/hosts then you do not have use
-i argument. this is only when you have a customized inventory file like I do.