Master Ansible: How to Write Your First Playbook on Ubuntu.

Master Ansible: How to Write Your First Playbook on Ubuntu.

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.

Screenshot2025 03 09182401 ezgif.com optipng

Step 1: Install Ansible

  1. 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
Screenshot2025 03 09182724 ezgif.com optipng

Install Ansible: You can install Ansible by using the following command:

sudo apt install ansible
Screenshot2025 03 09183707 ezgif.com optipng 1

Verify Installation: Check that Ansible has been successfully installed:

ansible --version
Screenshot2025 03 09183837 ezgif.com optipng

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.

  1. Create an inventory file: Create a new inventory file to specify the servers you want to manage.
sudo nano /etc/ansible/hosts
Screenshot2025 03 09183916 ezgif.com optipng 1

Example hosts file:

[web_servers]
192.168.1.100
192.168.1.101

[db_servers]
192.168.1.200
Screenshot2025 03 09185017 ezgif.com optipng

Check your inventory: You can verify the hosts in your inventory file with the following command:

ansible-inventory --list -i /etc/ansible/hosts
Screenshot2025 03 09185236 ezgif.com optipng

Step 3: Write Your First Ansible Playbook

  1. Create a playbook file: Playbooks are written in YAML format. Create a new playbook file, for example first-playbook.yml:
nano first-playbook.yml
Screenshot2025 03 09185340 ezgif.com optipng

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
Screenshot2025 03 09185414 ezgif.com optipng
  • name: Describes what the playbook does.
  • hosts: Specifies which hosts this playbook applies to. In this case, it applies to the web_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 installs nginx and starts the service.

Step 4: Run the Playbook

  1. 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
Screenshot2025 03 09185604 ezgif.com optipng
  • -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:

  1. 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
Screenshot2025 03 09185546 ezgif.com optipng

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.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *