This tutorial aims to provide a comprehensive guide on using Ansible for system configuration. By the end of this tutorial, you should be able to write and run Ansible playbooks to manage and configure your systems.
Ansible is a simple and agentless automation tool. You can install it using the default package manager for your system. For instance, on a Ubuntu system, the command would be:
sudo apt-get update
sudo apt-get install ansible
Playbooks are Ansible’s configuration, deployment, and orchestration language. They are expressed in YAML format and describe a policy to be enacted on your remote systems.
Example:
---
- name: Playbook to install nginx
hosts: webservers
tasks:
- name: ensure nginx is at the latest version
apt: name=nginx state=latest
The above playbook ensures that nginx is installed on all your web servers.
To run the playbook, you can use the ansible-playbook
command followed by the playbook file name.
ansible-playbook playbook.yml
Here is an example playbook that installs git on a server:
---
- name: Install git
hosts: servers
tasks:
- name: Ensure git is installed
apt:
name: git
state: present
This playbook ensures the nginx service is started:
---
- name: Start nginx
hosts: webservers
tasks:
- name: Ensure nginx is started
service:
name: nginx
state: started
In this tutorial, we learned how to use Ansible for system configuration. We understood what Ansible is, how to install it, and how to write and run Ansible playbooks. We also saw practical examples of Ansible playbooks.
To explore more about Ansible, you can visit the official Ansible documentation.
Hint: This is similar to the git installation playbook.
Hint: This is similar to the playbook for starting a service.
Hint: For this, you need to ensure the state is 'absent' instead of 'present'.