You create Ansible playbooks to handle or automates more complex management tasks. Playbooks consist of one or more plays, or groups of tasks, that operate on a set of defined hosts. Playbooks are expressed in YAML format.
Ansible hosts that are referenced in the playbook must be defined in the Ansible inventory file, which by default resides at
/etc/ansible/hosts. Each play must specify the hosts on which the tasks operate, the list of tasks to execute on each host, and any required variables or module parameters. For detailed information about creating Ansible playbooks, refer to the Ansible website at http://docs.ansible.com/playbooks.html
Creating a Playbook
To create a simple playbook to perform tasks on defined hosts/machine please follow below steps.
Step 1 – Create a new file with a descriptive playbook name that uses the
.yml file extension
Step 2 – Define play section with required opening line for the playbook and the hosts or groups of hosts which defined into inventory files.
--- # yml opening line - hosts: webservers # hosts or groups which defined into inventory file remote_user: yourname # The remote_user parameter was formerly called just user become: yes # Ansible allows you to ‘become’ another user with sudo privilege
To understand become please visit Become (Privilege Escalation)
Step 3 – Define a tasks section, and include one or more tasks with each task as a list item.
# Task section with list of tasks tasks: - name: ensure apache is at the latest version yum: name: httpd state: latest - name: write the apache config file template: src: /srv/httpd.j2 dest: /etc/httpd.conf notify: - restart apache - name: ensure apache is running service: name: httpd state: started
Executing the Playbook
To execute the playbook, execute the ansible-playbook command on the control machine, and provide the playbook path and any desired options.
For more options run