Deploy Ansible Playbook using GitHub Action

Introduction

Ansible is an open-source IT automation tool that simplifies the deployment, configuration, and management of software applications and infrastructure

GitHub Actions is a powerful continuous integration and deployment (CI/CD) tool provided by GitHub. It allows developers to automate software workflows and build, test, and deploy code changes from within the GitHub platform.

Prerequisites

  • Basic knowledge of ansible and GitHub Action workflow
  • Up and running virtual or physicals machine.
  • GitHub account

In this post, We will show you how to deploy the Ansible playbook using GitHub Action workflow.

Step 1: Creating Any Ansible Playbook

We need to prepare any ansible-playbook and push in the GitHub main branch, For this tech article I am going to use the Apache ansible playbook.

- hosts: all
  become: yes
  tasks:

    - name: update
      apt: update_cache=yes   
   
    - name: install apache2
      apt: name=apache2 state=latest

    - name: enabled mod_rewrite
      apache2_module: name=rewrite state=present

    - name: enabled proxy
      apache2_module: name=proxy state=present

    - name: enabled proxy_http
      apache2_module: name=proxy_http state=present
    
    - name: enabled ssl
      apache2_module: name=ssl state=present
    
    - name: enabled socache_shmcb
      apache2_module: name=socache_shmcb state=present

    - name: enabled mpm_event
      apache2_module: name=mpm_event state=present


      notify:
        - restart apache2

  handlers:
    - name: restart apache2
      service: name=apache2 state=restarte 

Ansible-playbook yaml file named will be apache-playbook.yml that contain the Apache web server installation and module configuration with it.

Step 2: Creating Secrets Variables

We need to use the given 2 secrets valuables that will use in GitHub Action workflow.

ANSIBLE_INVENTORY

TYPE-IP-ADDRESS ansible_ssh_user=example

We need to update the IP address and Ansible’s client machine username, May be we will having different username of few machines and like by we also define the ssh’s password param by using this ansible_ssh_pass=xxxxxxx

SSH_PRIVATE_KEY

We need to copy and poste the pem file contain with SSH_PRIVATE_KEY in the Github Action’s secrets variables.

Step 3: Enabling Workflow

We need to click on Action button in GitHub’s repository account and Click on set up a workflow yourself button.

Copy and paste the following YAML configuration in order to deploy the Ansible playbook using GitHub Action.

name: Ansible Playbook Deployment 

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout 
      uses: actions/checkout@v3

    - name: Run Ansible playbook
      uses: dawidd6/action-ansible-playbook@v2.5.0
      with:
        playbook: apache-playbook.yml # path to your Ansible playbook
        directory: ./
        key: ${{ secrets.SSH_PRIVATE_KEY }} # the ssh private key for ansible to use to connect to the servers, stored as "ansible_ssh_private_key" in the GitHub secrets
        inventory: ${{ secrets.ansible_inventory }} # the ansible inventory to use, stored as "ansible_inventory" in the GitHub secrets

We you update the yours’s ansible playbook name and path only and we are good to save and commit this config, According to the branch config this will trigger the this workflow.

We should get GitHub Action job status like this and changes in ansible target machine.

Click here get more post got GitHub Action.

Conclusion

We have successfully deployed ansible playbook with GitHub Action , If you still have questions, please post them in the comments section below.

Author

Deploy Ansible Playbook using GitHub Action

29 thoughts on “Deploy Ansible Playbook using GitHub Action

Leave a Reply

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

Scroll to top