Example showing how to run an ansible playbook using github actions
Setup GitHub secrets INVENTORY, KNOWN_HOSTS and SSH_KEY.
Important
Ensure that the tool you use to set the GitHub secret supports multiline content (VSCode seems not to support it). I recommend using the github.com website itself.
Afterwards every push to the repository or manual trigger of the workflow direktly (via GitHub.com -> Actions) should trigger the workflow and execute the playbook on the confiured hosts.
For more details see description below.
This repository contains following things:
- python requirements for running ansible
- simple ansible playbook
- github action workflow
See file requirements.txt
Install requirements locally using a venv:
python3 -m venv ./venv
source venv/bin/activate
pip install -r requirements.txt
See file ansible/ping.yaml
Uses the ansible.builtin.ping module to test connection to remote
See file .github/workflows/main.yml
The workflow contains the following jobs
- validate - Validates the playbook, using ansible-lint
- run-playbook
Runs asible-lint to ensure the code quality of the playbook.
The requirements.yml is required to resolve additional namespaces while linting.
Installs requirements.txt or test.requirements.txt if file exists in project-root.
Writes the inventory information from GitHub secret to file
The content should be a valid ansible inventory
ungrouped:
hosts:
my.domain.de:
ansible_user: the_user
Creates everything need for ssh. Therefor GitHub secret KNOWN_HOSTS and SSH_KEY must exist.
KNOWN_HOSTS Well known file ~/.ssh/known_hosts. The GitHub secret must be set to the required hosts.
my.domain.de ecdsa-sha2-nistp256 <key>
SSH_KEY The secret SSH_KEY must be set to a valid private key for the user that is used to run ansible on the target.
- generate ssh-key
ssh-keygen -o -a 1000 -t ed25519
-
add public key to the authorized_keys of the user on the target system.
-
set the private key as GitHub secret SSH_KEY. Should be something like:
-----BEGIN OPENSSH PRIVATE KEY-----
<LongPrivateKey>...==
-----END OPENSSH PRIVATE KEY-----
While looking for an example how to use github actions to run ansible notebooks I stumbled over the following article of @xNok (https://github.com/xNok) at medium: https://medium.com/faun/how-to-run-an-ansible-playbook-using-github-action-42430dec944 This repository contains the outcome of playing around with this example as a start.