Skip to content

Ansible Environment

Decision: Use isolated minimal environments

Each collection uses an isolated minimal environment to minimize hidden dependencies.

Create Environment and Install Packages

Create python virtual environment, install Ansible and Molecule

python3 -m venv /var/venv/{REPO}  # Bare name (ansible_collection_srv).
source /var/venv/{REPO}/bin/activate
pip install --upgrade pip
pip install --upgrade setuptools
# Lock to specific version. See reference.
pip install ansible  # ansible==11.0  # ansible-core 2.18
pip install argcomplete
# Install older ansible-compat layer until issue is resolved.
# Reference:
# * https://github.com/ansible/ansible-lint/issues/4533
# pip install ansible-lint
# pip install molecule
pip uninstall -y ansible-compat ansible-lint molecule
pip install "ansible-compat==24.10.0" ansible-lint molecule
Reference:

Set alternative .ansible cache location

A recent change in Ansible now creates an .ansible directory in each directory ansible is run - building and copying all collections/roles recursively - potentially creating multi-GB PER directory. Leads to extremely slow and noisy yamllint and ansible-lint usage.

/etc/fstab

tmpfs  /tmp/ansible-cache  tmpfs  noatime,mode=1777  0 0

Mount new directory

mount -o remount -a

Symlink all directories to cache

find . -type d -name '.ansible' -exec rm -rf "{}" \; -exec ln -s /tmp/ansible-cache "{}" \;
Reference:

Set alternative container storage location (optional)

Developing ansible will thrash disk especially when running playbooks and testing with molecule. Relocate high-use directories to a disk that can handle high wear. Prefer to config change as this enables vanilla use in production.

Link ansible caching directories. (1)

  1. Consider moving and linking entire .cache directory.
# Delete or move existing cache data.
ln -s /mnt/cache/.ansible_async ${HOME}/.ansible_async
ln -s /mnt/cache/.ansible ${HOME}/.ansible
ln -s /mnt/cache/cache/ansible-compat ${HOME}/.cache/ansible-compat
ln -s /mnt/cache/cache/molecule ${HOME}/.cache/molecule