Introduction.
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It simplifies managing IT infrastructure by defining tasks in YAML-based playbooks. With Ansible, you can automate repetitive tasks across multiple systems, ensuring consistency and efficiency. It works through SSH and doesn’t require agent installation, making it lightweight and easy to use. Playbooks are the heart of Ansible automation, defining a series of tasks to execute on specified hosts. Ansible’s declarative language ensures that tasks are executed in the desired state. It supports various modules for tasks like package installation, service management, and file manipulation. By using Ansible, teams can achieve faster, error-free, and repeatable deployments. It integrates well with cloud platforms and DevOps pipelines. Ansible is popular for its simplicity, scalability, and flexibility, making it a key tool for modern system administration.

Step 1: Install Ansible
- Update the package index: Open your terminal and update your package list to ensure that you have the latest version of the software.
sudo apt update

Install Ansible: You can install Ansible by using the following command:
sudo apt install ansible

Verify Installation: Check that Ansible has been successfully installed:
ansible --version

Step 2: Set Up the Inventory File
Ansible requires an inventory file that lists the hosts it will manage. By default, Ansible uses /etc/ansible/hosts
, but you can create a custom inventory file if needed.
- Create an inventory file: Create a new inventory file to specify the servers you want to manage.
sudo nano /etc/ansible/hosts

Example hosts
file:
[web_servers]
192.168.1.100
192.168.1.101
[db_servers]
192.168.1.200

Check your inventory: You can verify the hosts in your inventory file with the following command:
ansible-inventory --list -i /etc/ansible/hosts

Step 3: Write Your First Ansible Playbook
- Create a playbook file: Playbooks are written in YAML format. Create a new playbook file, for example
first-playbook.yml
:
nano first-playbook.yml

Write the playbook: Here’s an example playbook that installs nginx
on the web servers.
---
- name: Install nginx on web servers
hosts: web_servers # This specifies the group of servers defined in your inventory file
become: yes # Run tasks with sudo
tasks:
- name: Install nginx package
apt:
name: nginx
state: present
update_cache: yes # Update apt cache before installation
- name: Ensure nginx is running
service:
name: nginx
state: started
enabled: yes # Ensure nginx starts on boot

name
: Describes what the playbook does.hosts
: Specifies which hosts this playbook applies to. In this case, it applies to theweb_servers
group.become: yes
: Indicates that the tasks will be run with elevated privileges (sudo).tasks
: A list of tasks to be executed on the remote host. In this example, it installsnginx
and starts the service.
Step 4: Run the Playbook
- Execute the playbook: To execute the playbook, run the
ansible-playbook
command. If you’re using the default inventory file located at/etc/ansible/hosts
, you can run:
ansible-playbook -i /etc/ansible/hosts first-playbook.yml

-i /etc/ansible/hosts
: This specifies the inventory file to use.first-playbook.yml
: This is the playbook you want to execute.
Watch the output: Ansible will connect to the hosts specified in the inventory file and execute the tasks defined in the playbook. The output will look something like this:
PLAY [Install nginx on web servers] ***
TASK [Install nginx package] ***
ok: [192.168.1.100] => (item=None) => {
"changed": false,
"name": "nginx",
"state": "present"
}
TASK [Ensure nginx is running] ***
ok: [192.168.1.100] => (item=None) => {
"changed": false,
"name": "nginx",
"state": "started"
}
PLAY RECAP ***
192.168.1.100 : ok=2 changed=0 unreachable=0 failed=0
Step 5: Verify the Changes
After the playbook finishes running, you can verify the changes:
- Check if nginx is installed: On the target machine (e.g., 192.168.1.100), you can check the status of
nginx
:
systemctl status nginx

Check the web server: Open a web browser and navigate to the server’s IP address (e.g., http://192.168.1.100). You should see the default nginx welcome page.
Conclusion.
Congratulations! You’ve written your first Ansible playbook to install and manage Apache on Ubuntu. Playbooks are powerful, and you can expand them by adding more tasks, roles, and configurations to automate even more complex workflows.
Ansible playbooks allow you to define automation tasks that are easy to read and maintain. As you continue learning, you can explore more advanced topics like roles, handlers, and templates to make your playbooks even more flexible and reusable.
Add a Comment