Setting up the local development environment to work with Ansible.
You'll need virtualenv:
sudo apt-get install python3-virtualenv -yStart the environment:
python3 -m venv .venv
virtualenv .venv
source .venv/bin/activateInstall the dependencies:
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txtDepending on yor preference, the following provisioning options are available in this repository.
Launch the instance:
multipass launch 24.04 `
--name vm-ansible `
--cpus 4 `
--memory 8G `
--disk 50G `
--timeout 1800 `
--cloud-init .\multipass\cloud-init.yamlConnect via SSH:
multipass shell vm-ansibleCheck if *** System restart required ***. If so, sudo reboot.
Verify the cloud-init status:
cloud-init statusCheck logs if necessary:
/var/log/cloud-init-output.log
/var/log/cloud-init.logPrepare the directory with Vagrant:
mkdir -p vagrant-pull-node/ansible
cd vagrant-pull-node/ansible
vagrant init "ubuntu/jammy64"Set additional parameters as in the virtualbox/Vagrantfile. This will add the necessary packages.
Create and connect to the VM:
vagrant up
vagrant sshTip
In case of docker fetch errors, you may try docker system prune -a
For lightweight docker environment, set it up:
docker compose build
docker compose up -d
docker compose exec workstation /bin/bashThis section is executed from within the control node.
Tip
A fork will be required to create tokens in the account
Connect to the GitHub account and generate a new token. Prefer a fine-grained token, where only read-only permissions to the Contents to the repository is required.
Note
Preferring the https URL option to use GitHub tokens
The next ansible-pull command was adapted from this page.
For development purposes, create a /opt/ansible/pull.sh script:
Tip
Create link for ln -s /opt/ansible/pull.sh /home/vagrant/pull.sh
#!/bin/bash
token=1234
url="https://$token:x-oauth-basic@github.com/epomatti/ansible-pull-demo.git"
ansible-pull -U $url -d /opt/epomatti/ansible-pull-demoExecute the Ansible pull script:
bash pull.shThe local.yml file will create a cron job to pull new configuration every minute. The package flock will be used to prevent cron job overlap.
Check the crontab logs:
sudo tail -f /var/log/syslogThe script output will be redirected to this file:
sudo tail -f /var/log/ansible-pull.logTo make ad-hoc adjustments to the local configuration:
sudo systemctl stop cronCreate the key file, and add a secure password:
touch ~/.vault_key
chmod 600 ~/.vault_keyEncrypt files (they must be commited to the repository):
ansible-vault encrypt --vault-password-file ~/.vault_key secret.txtDecrypt the files locally:
ansible-vault decrypt --vault-password-file ~/.vault_key secret.txtWhen pulling, use the key:
ansibel pull --vault-password-fileTip
This is about test strategies for testing the application you are deploying
The primary tool for this should be Molecule.
bash local.shRunning the tests:
bash tests.sh