Are you ready to dive into the world of automation? Ansible is a powerful tool that allows you to manage and configure systems with ease. This blog post will guide you through creating your first Ansible playbook, specifically designed to set up a web server (either Nginx or Apache). No prior experience is required!

What is Ansible?

Ansible is an open-source automation engine that automates software provisioning, configuration management, and application deployment. Unlike other tools, Ansible is agentless, meaning you don’t need to install any software on the target servers. It uses SSH to connect to your servers and execute tasks defined in a playbook.

Prerequisites

Before we start, make sure you have the following:

  • A control machine: This is the machine where you’ll install Ansible and run your playbooks. It can be your local computer or a dedicated server.
  • A target server: This is the server where you’ll install and configure the web server. You’ll need SSH access to this server from your control machine.
  • Python: Ansible requires Python to be installed on both the control machine and the target server. Most Linux distributions come with Python pre-installed.

Step 1: Installing Ansible

The installation process varies depending on your operating system. Here are the instructions for some common platforms:

Ubuntu/Debian:

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

CentOS/RHEL:

sudo yum install epel-release
sudo yum install ansible

macOS (using pip):

pip3 install ansible

After the installation, verify that Ansible is installed correctly by running:

ansible --version

This command should display the Ansible version and other relevant information.

Step 2: Creating an Inventory File

The inventory file tells Ansible which hosts it should manage. By default, Ansible looks for the inventory file at /etc/ansible/hosts. You can also specify a custom inventory file using the -i option when running a playbook.

Let’s create a simple inventory file. Open a new file named hosts (or any name you prefer) and add the following content:

[webservers]
webserver1 ansible_host=your_server_ip ansible_user=your_username ansible_ssh_private_key_file=~/.ssh/id_rsa

Replace the following placeholders:

  • your_server_ip: The IP address or hostname of your target server.
  • your_username: The username you’ll use to connect to the server via SSH.
  • ~/.ssh/id_rsa: The path to your SSH private key. If you’re using password-based authentication, you can omit ansible_ssh_private_key_file and Ansible will prompt you for the password. However, key-based authentication is highly recommended.

You can add multiple servers to the [webservers] group. For example:

[webservers]
webserver1 ansible_host=192.168.1.10 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
webserver2 ansible_host=192.168.1.11 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa

To verify that Ansible can connect to your server, run the following command:

ansible -i hosts webservers -m ping

If the connection is successful, you should see a pong response from each server.

Step 3: Writing Your First Playbook

A playbook is a YAML file that defines the tasks Ansible should execute on the target servers. Let’s create a playbook to install and start an Nginx or Apache web server.

Create a new file named webserver.yml (or any name you prefer) and add the following content:

---
- hosts: webservers
  become: true
  tasks:
    - name: Update apt cache (Debian/Ubuntu)
      apt:
        update_cache: yes
      when: ansible_os_family == "Debian"

    - name: Update yum cache (CentOS/RHEL)
      yum:
        update_cache: yes
      when: ansible_os_family == "RedHat"

    - name: Install Nginx
      apt:
        name: nginx
        state: present
      when: ansible_os_family == "Debian"

    - name: Install Apache
      yum:
        name: httpd
        state: present
      when: ansible_os_family == "RedHat"

    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes
      when: ansible_os_family == "Debian"

    - name: Start Apache
      service:
        name: httpd
        state: started
        enabled: yes
      when: ansible_os_family == "RedHat"

Let’s break down this playbook:

  • hosts: webservers: Specifies that this playbook should be executed on the servers in the webservers group defined in the inventory file.
  • become: true: Tells Ansible to use privilege escalation (sudo) to execute the tasks as the root user.
  • tasks: A list of tasks to be executed.
  • name: A descriptive name for each task.
  • apt: A module used to manage packages on Debian/Ubuntu systems.
    • update_cache: yes: Updates the apt package cache.
    • name: nginx: Specifies the package to be installed (Nginx in this case).
    • state: present: Ensures that the package is installed.
  • yum: A module used to manage packages on CentOS/RHEL systems.
    • update_cache: yes: Updates the yum package cache.
    • name: httpd: Specifies the package to be installed (Apache in this case).
    • state: present: Ensures that the package is installed.
  • service: A module used to manage services.
    • name: nginx: Specifies the service to be managed (Nginx in this case).
    • state: started: Ensures that the service is started.
    • enabled: yes: Ensures that the service is started automatically on boot.
  • when: a conditional statement which defines when a task needs to run.
    • ansible_os_family == "Debian": runs a task when the managed node’s OS family is Debian (i.e. Debian, Ubuntu, etc).
    • ansible_os_family == "RedHat": runs a task when the managed node’s OS family is RedHat (i.e. RedHat, CentOS, Fedora, etc).

This playbook uses conditional statements to install either Nginx or Apache depending on the operating system of the target server.

Step 4: Running the Playbook

To run the playbook, use the following command:

ansible-playbook -i hosts webserver.yml

Ansible will connect to the target server(s) and execute the tasks defined in the playbook. You’ll see output indicating the progress of each task.

If everything goes well, you should be able to access your web server by opening a web browser and navigating to the IP address of your target server.

Conclusion

Congratulations! You’ve successfully created and executed your first Ansible playbook. This is just the beginning of your automation journey. As you become more familiar with Ansible, you can create more complex playbooks to automate a wide range of tasks, from deploying applications to managing infrastructure. Explore the Ansible documentation and experiment with different modules to discover the full potential of this powerful tool.