Use Ansible with GitHub Actions to Automate Playbook Runs

How to Use Ansible with GitHub Actions

Automation is key in today’s fast-paced tech environment. You’ve likely used Ansible to automate tasks if you’re managing infrastructure. But did you know you can also automate those Ansible playbook runs using GitHub Actions? You can create a seamless CI/CD pipeline by integrating Ansible with GitHub Actions. This combination reduces manual effort, minimizes errors, and ensures consistency across your environments.

In this article, I’ll explain how to use Ansible with GitHub actions to automate playbook runs.

Let’s explore each step!

Prerequisites

Before diving in, let’s make sure you have everything you need:

  • Ansible: Installed on your local machine.
  • GitHub Repository: Either create a new one or use an existing one.
  • GitHub Actions: Access within your repository.
  • A Remote Server: To apply the Ansible playbook.

Setting Up the GitHub Repository

1. Create a Git Repository

First, you need a GitHub repository. If you don’t have one, create it now. Here’s how:

  • Go to GitHub and log in.
  • Click on the “New” button to create a new repository.
  • Name your repository (e.g., ansible-github-actions).
  • Initialize with a README.md file and choose your preferred license.
  • Click “Create repository”.

2. Add your Ansible Playbook

Here’s a simple playbook that installs and configures Apache on a remote server:

---
- name: Install and Configure Apache
  hosts: webservers
  become: yes

  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present

    - name: Start and enable Apache
      service:
        name: apache2
        state: started
        enabled: true

Place this file in your repository’s root directory and name it site.yml.

3. Create an Inventory FIle

Also, create an inventory file to this repository and add the following content.

[webservers]
192.168.1.10
192.168.1.11

Note: Replaced 192.168.1.10 and 192.168.1.11 with the IP address of remote servers.

Creating a GitHub Actions Workflow

Now that your playbook is ready, it’s time to create a GitHub Actions workflow. This workflow will automatically run your Ansible playbook whenever there’s a code push.

1. Create a Workflow File

  • In your repository, create the directory .github/workflows/.
  • Inside, create a file named ansible.yml with the following content:
name: Run Ansible Playbook

on:
  push:
    branches:
      - main

jobs:
  ansible:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'

    - name: Install Ansible
      run: |
        python3 -m pip install --upgrade pip
        pip3 install ansible

    - name: Run Ansible Playbook
      run: ansible-playbook site.yml -i inventory

Here’s what each step does:

  • Checkout code: Pulls the latest code from your repository.
  • Set up Python: Sets up Python 3.x, which Ansible needs.
  • Install Ansible: Installs Ansible using pip.
  • Run Ansible Playbook: Executes the site.yml playbook.

This workflow triggers every time you push code to the main branch.

Real-World Use Case

Let’s look at a real-world scenario where a company automates its server configuration using Ansible and GitHub Actions.

Imagine you have a web application running on multiple servers. Each server needs Apache installed and configured. Instead of manually updating each server, you can automate this with GitHub Actions.

Here’s how the workflow would work:

  • Developer Pushes Code: A developer pushes a change to the main branch.
  • GitHub Actions Trigger: The workflow is triggered automatically.
  • Ansible Playbook Runs: The playbook installs and configures Apache on all servers listed in the inventory.

With this setup, you can manage your infrastructure as code. It’s reliable, repeatable, and reduces the risk of errors.

Conclusion

Automating Ansible playbook runs with GitHub Actions is a game-changer. It brings the power of automation to your CI/CD pipelines, making server management easier and more reliable. With the steps outlined in this article, you’re now equipped to set up this automation in your projects.

Explore more complex workflows as you grow comfortable with the basics. The possibilities are endless.

Happy automating!

FAQs

1. Can I use a private repository for this setup?

Yes, you can use a private repository. Just ensure you configure SSH keys or GitHub secrets for authentication when accessing remote servers.

2. How do I trigger the workflow manually?

You can trigger the workflow manually by going to the Actions tab in your repository and selecting the desired workflow run.

3. How can I troubleshoot issues with the playbook run?

You can check the logs generated by GitHub Actions for detailed error messages or warnings during the workflow execution.

4. Can I use GitHub Actions with other CI/CD tools?

Yes, GitHub Actions can integrate with other CI/CD tools like Jenkins, CircleCI, or TravisCI, depending on your project’s needs.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva