
GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.
Before you can use pip to install Ansible you will need to setup Python in the runner VM. Check out my article FreeKB - GitHub Actions - Install Python in a runner VM.
You will also need to install Ansible in the runner VM. Check out my article FreeKB - GitHub Actions - Install Ansible in a runner VM.
Let's say your repo contains the following structure, where you have a galaxy.yml file and a YAML file that contains a task.
├── galaxy.ymy
├── main.yml
The plain ole run command can be used to run the pip commands in the runner VM to install Ansible in the runner VM, to copy the galaxy.yml and main.yml files into the build/src directory in the runner VM, and to then run the ansible-galaxy collection build command to build the collection. Last but not least, the collection TAR file is SCP from the runner VM to a local server.
name: GitHub Action
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
push:
branches:
- main
jobs:
restart-docker-container:
runs-on: ubuntu-latest
steps:
- name: Checking out the repository code
uses: actions/checkout@v4
- name: setup python version 3.12 in the runner VM
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: use pip to install ansible in the runner VM
run: |
python3 -m pip install --upgrade pip
pip install --upgrade ansible
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: copy galaxy.yml and the roles directory to build/src in the runner VM
run: |
mkdir --parents build/src
cp galaxy.yml build/src
cp main.yml build/src
- name: build the Ansible collection (this will create a TAR archive)
run: ansible-galaxy collection build build/src
- name: append SSH key connection to target server to id_rsa
run: |
install -m 600 -D /dev/null ~/.ssh/id_rsa
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
- name: Get version from galaxy.yml file
id: collection_version
run: echo "version=$(grep version homegrown/collections/galaxy.yml | awk -F':' '{ print $2 }' | sed 's|\s*||g')" >> $GITHUB_OUTPUT
- name: SCP command
run: scp -o StrictHostKeyChecking=no "homegrown-collections-${{ steps.collection_version.outputs.version }}.tar.gz" john.doe@server1.example.com:/tmp
Did you find this article helpful?
If so, consider buying me a coffee over at