Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using Git with Ansible

Introduction

Git is a powerful version control system that allows you to track changes in your code and collaborate with others. Ansible is an open-source automation tool used for IT tasks such as configuration management, application deployment, and orchestration. Combining Git with Ansible allows you to manage your infrastructure as code efficiently, enabling version control, collaboration, and automated deployments.

Prerequisites

Before getting started, ensure you have the following installed on your system:

  • Git
  • Ansible
  • A GitHub, GitLab, or Bitbucket account (optional but recommended)

Setting Up a Git Repository

Start by creating a new Git repository for your Ansible project.

Navigate to your project directory and run the following commands:

mkdir my-ansible-project
cd my-ansible-project
git init
                

This initializes a new Git repository in your project directory.

Creating an Ansible Playbook

Create a simple Ansible playbook to test your setup.

Create a file named playbook.yml and add the following content:

---
- hosts: localhost
  tasks:
    - name: Ensure Nginx is installed
      apt:
        name: nginx
        state: present
                

Committing Changes to Git

Once you have created your playbook, you can commit your changes to Git.

Run the following commands:

git add playbook.yml
git commit -m "Add initial playbook"
                

This stages the playbook.yml file and commits it with a message.

Pushing to a Remote Repository

If you are using a remote repository (GitHub, GitLab, Bitbucket), push your changes to the remote repository.

First, add the remote repository:

git remote add origin https://github.com/your-username/my-ansible-project.git
                

Then push your changes:

git push -u origin master
                

Using Git with Ansible Vault

Ansible Vault allows you to keep sensitive data, such as passwords or keys, encrypted. You can use Git to version control these encrypted files.

Create a Vault file:

ansible-vault create secret.yml
                

This will prompt you to enter a password and open an editor to enter your secrets. Once saved, you can commit this encrypted file to Git:

git add secret.yml
git commit -m "Add encrypted secrets"
                

Using Git Hooks with Ansible

Git hooks are scripts that run automatically in response to certain events in a Git repository. You can use Git hooks to automate Ansible tasks.

Create a pre-commit hook to run an Ansible playbook before committing changes:

echo '#!/bin/sh' > .git/hooks/pre-commit
echo 'ansible-playbook playbook.yml' >> .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
                

This script runs the Ansible playbook before every commit.

Conclusion

Using Git with Ansible provides a powerful combination for managing your infrastructure as code. You can version control your Ansible playbooks, collaborate with others, and automate deployments efficiently. By integrating Git and Ansible, you can ensure your infrastructure code is reliable, maintainable, and scalable.