Skip to content

๐Ÿ› ๏ธ 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:

mkdir playbooks
mkdir -p collections/ansible_collections && cd collections/ansible_collections/

๐Ÿงฑ 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

collections:
  - name: community.general
  - name: zilux_ps_linux.mycol

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

๐Ÿ  HOME ๐Ÿ” Top