Run Ansible Playbooks with Github Action

Spread the love

It is common for a senior DBA to set up CI/CD pipeline to deploy some software, or add some changes in the existing configuration. For this goal, I use Ansible/PowerShell DSC.

Some of the pre-requisites are the following –

  • Basic knowledge of Linux & shell scripting
  • Working knowledge of git
  • Level2 knowledge of Ansible to build your playbook
  • One or more git runners configured.

Following is the sample code of the Github Action workflow I use to run Ansible roles/playbook in my personal lab environment.

https://github.com/imajaydwivedi/Ansible-Learning/blob/dev/.github/workflows/ansible-role-runner.yml

The workflow accepts 2 input parameters-

  • Role folder to run
    • This folder is a folder directly inside the GitHub Repo. This folder is an Ansible Role.
    • To simplify management, I have kept the inventory file hosts.yml and the main playbook file site.yml that is needed to run the ansible role inside the role directory itself.
  • Whether to cleanup the files after workflow completes.
    • While building my ansible playbook CI/CD pipeline, I avoid cleanup to debug code on the git runner machine.

Within the workflow yml file <Repo>/.github/workflows/ansible-role-runner.yml, I have the following logic –

  • Using my desktop machine which is configured to be git runner named ‘ubuntu‘.
  • Use GitHub secrets, and build another var file defaults/creds.yml that is called in role main task file tasks/main.yml
  • Run the ansible playbook site.yml with role_name passed as environment variable.
  • Finally, clean up creds.yml or any other sensitive information from the git runner machine.

I hope this will help anyone trying to figure out how to build CI/CD pipeline using GitHub Actions and Ansible for various tasks like Linux configuration, postgres installation and configuration, etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.