๐ ๏ธ Ansible Development Environment Setup
A step-by-step guide to setting up an Ansible collection with testing tools like ansible-test, molecule, and ansible-lint.
๐ Python Setup
Install Python 3.12 and configure alternatives:
sudo dnf install python3.12 python3.12-pip python3.12-setuptools
sudo alternatives --install /usr/bin/python python /usr/bin/python3.12 60
sudo alternatives --install /usr/bin/python python /usr/bin/python3.9 70
sudo alternatives --config python
python --version
๐ Project Initialization
export ANSIBLE_CONFIG=~/somepath/ansible.cfg
mkdir project_NewCollection && cd project_NewCollection
python -m venv .venv && source .venv/bin/activate
pip install ansible-dev-tools ansible-lint molecule
pip install --upgrade pip
Create directories:
๐งฑ Create Collection & Role
ansible-galaxy collection init zilux_ps_linux.mycol
cd zilux_ps_linux/mycol
ansible-galaxy role init roles/myrole
cd roles/myrole
molecule init scenario default
๐งช Molecule Configuration
molecule.yml
---
driver:
name: default
platforms:
- name: instance
dependency:
name: galaxy
provisioner:
name: ansible
config_options:
defaults:
inventory: ./inventory
stdout_callback: yaml
roles_path: ../../roles
deprecation_warnings: False
scenario:
name: default
test_sequence:
- destroy
- dependency
- create
- prepare
- converge
- verify
- destroy
๐งช Molecule Commands
molecule --debug converge
molecule converge -- --vault-password-file vault_pass.txt --extra-vars molecule_test_host=$(molecule_test_host)
molecule test --report
molecule test -h
molecule matrix -s default test
molecule syntax
๐ฆ prepare.yml Example
---
- name: Install required collections
hosts: localhost
gather_facts: false
tasks:
- name: Install collections
ansible.builtin.command:
cmd: ansible-galaxy collection install -r requirements.yml
๐ฆ requirements.yml Example
Install with:
ansible-galaxy collection install -r requirements.yml
ansible-galaxy role install -r requirements.yml
๐งโ๐ป Git Configuration
.gitconfig
[user]
name = Harry
email = XXX@gmail.com
[init]
defaultBranch = main
[format]
pretty = %h %ad | %s%d [%an]
[log]
date = short
[alias]
st = status -s
ll = log --pretty=format:"%h %s" --graph
lll = log --pretty=format:"%h - %an, %ar : %s"
[color]
ui = true
branch = true
diff = true
status = true
[credential]
helper = cache --timeout=36000
[core]
editor = vim
๐ซ .gitignore Example
.venv/
__pycache__/
*.pyc
.ansible/
.cache/
collections/ansible_collections/.cache/
collections/ansible_collections/*/*/.python-version
.devcontainer/
.vscode/
molecule/default/.molecule/
*.log
*.retry
.coverage
๐ณ๏ธ Podman Setup
๐ Podman Installation Guide
dnf install container-tools
dnf install podman-docker
podman pull registry.access.redhat.com/ubi9/ubi
podman run --rm --name=myubi registry.access.redhat.com/ubi9/ubi cat /etc/os-release