# Ansible Playbooks - Complete Guide with Examples

Ansible is an open-source automation tool that simplifies the process of configuration management, application deployment, and task automation. It uses playbooks, which are YAML files, to define a series of tasks that can be executed on one or more managed nodes.

**<mark>Step 1: Test Connection to Hosts</mark>**

First, ensure you can connect to your hosts using Ansible:

```plaintext
ansible all -i inventory.ini -m ping
```

This command pings all hosts defined in your `inventory.ini` file to verify connectivity.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723835641260/eeccfd55-39c5-4a22-8819-bd91b18d7c87.png align="center")

**<mark>Step 2: Writing Your First Playbook - Installing Apache</mark>**

Let's start by writing a playbook to install and configure Apache on all managed nodes. Create a YAML file named `basic.yaml` with the following content:

```plaintext
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:
  - name: Install the latest version of Apache
    apt:
      name: apache2
      state: present
  - name: Start service apache2, if not started
    service:
      name: apache2
      state: started
  - name: Copy file with owner and permissions
    copy:
      src: /home/ubuntu/index.html
      dest: /var/www/html/index.html
      mode: '777'
```

This playbook performs three tasks:

1. Installs the Apache web server.
    
2. Ensures the Apache service is started.
    
3. Copies an HTML file to the Apache document root with the necessary permissions.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723837034867/a96f3b92-8c31-4ffb-bf60-a722ba7dbbc8.png align="center")

Run the playbook using the command:

```plaintext
 ansible-playbook -i inventory.ini basic.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723837114186/66ddb43b-b3a3-451a-a0a4-56b02c4ec1bb.png align="center")

After running the playbook, check each managed node (slave machine) by entering their IP addresses in a browser to ensure Apache is running and serving the HTML file.

**Slave Machine - 1**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723837322629/f5d7da0c-1621-4d58-adb4-c606a92c62a0.png align="center")

**Slave Machine - 2**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723837343119/2a315701-7f4e-4519-9c12-258a54484437.png align="center")

**Slave Machine - 3**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723837359518/01aeacc3-fab5-4aa5-8075-7bae0512384c.png align="center")

**<mark>Step 3: Installing Multiple Tools with Ansible</mark>**

Next, we'll write a playbook to install multiple tools, including PHP, MySQL, Unzip, and Apache. Create a file named `tools.yaml` with the following content:

```plaintext
- hosts: all
  remote_user: ubuntu
  become: yes
  tasks:
  - name: Install the latest version of tools
    apt:
      name: "{{ item }}"
      state: present
    loop: 
      - php
      - mysql-server
      - unzip
      - apache2
```

This playbook uses a loop to install multiple tools, ensuring that all necessary packages are present on the managed nodes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723838231532/73da1a86-07c3-4ac6-8e83-f92215e2515a.png align="center")

Run the playbook using the command:

```plaintext
ansible-playbook -i inventory.ini tools.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723838353448/e3a4985a-658a-4427-a981-60ec1b7a5c7f.png align="center")

#### **<mark>Step 4: Grouping Hosts and Installing Jenkins</mark>**

You’ve now set up host grouping in your `inventory.ini` file, where Jenkins will only be installed on one machine. Here’s how your inventory file might look:

```plaintext
[jenkins]
username@<ip_address>

[docker]
username@<ip_address>
username@<ip_address>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723840772582/3558169c-0aed-45cc-85b1-6ae16ca6625c.png align="center")

#### **<mark>Step 5: Installing and Configuring Jenkins</mark>**

Finally, let's create a playbook to install and configure Jenkins. Save the following content to a file named `jenkins.yaml`:

```plaintext
- hosts: jenkins
  remote_user: ubuntu
  become: yes
  tasks:
    - name: Update APT package manager repositories
      apt:
        update_cache: yes

    - name: Install Java (Jenkins dependency)
      apt:
        name: openjdk-11-jdk
        state: present

    - name: Install gnupg and curl (for adding the Jenkins repo key)
      apt:
        name:
          - gnupg
          - curl
        state: present

    - name: Add Jenkins repository key
      shell: curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | gpg --dearmor -o /usr/share/keyrings/jenkins-archive-keyring.gpg

    - name: Add Jenkins repository
      apt_repository:
        repo: "deb [signed-by=/usr/share/keyrings/jenkins-archive-keyring.gpg] https://pkg.jenkins.io/debian-stable binary/"
        state: present

    - name: Install Jenkins
      apt:
        name: jenkins
        state: present

    - name: Start Jenkins service
      systemd:
        name: jenkins
        state: started
        enabled: yes

    - name: Ensure Jenkins is accessible on port 8080
      ufw:
        rule: allow
        port: '8080'
        proto: tcp
```

This playbook:

1. Updates the APT package manager.
    
2. Installs Java, which is a dependency for Jenkins.
    
3. Installs necessary tools like `gnupg` and `curl` to add Jenkins repository keys.
    
4. Adds the Jenkins repository and installs Jenkins.
    
5. Starts the Jenkins service and ensures it's enabled on boot.
    
6. Configures the firewall to allow traffic on port 8080, which Jenkins uses.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723839773609/310c69c3-96d9-4f0c-b824-cd69353c70e3.png align="center")

Run the playbook using the command:

```plaintext
ansible-playbook -i inventory.ini jenkins.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723841626628/86f7f1e2-e09b-4fcc-bd36-f24bebbd21dd.png align="center")

After running the playbook, check Jenkins by entering the Jenkins machine’s IP address followed by `:8080` in a browser.

**Slave Machine - 1**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723841722513/3f06c2c3-1b3a-4e20-b38c-f1e21c1b2966.png align="center")

#### **Conclusion**

In this blog, we've covered the basics of creating Ansible playbooks to automate the installation and configuration of Apache, Jenkins, and other tools on your managed nodes. By grouping hosts and using playbooks effectively, you can simplify and standardize your infrastructure management.
