From 224b302e2d69dad90808e12e256992a596634d83 Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Sun, 24 Oct 2021 15:17:34 +0200 Subject: [PATCH] Implement virtual env for pip and ansible collections --- .gitignore | 3 +++ .vscode/settings.json | 3 ++- ansible.cfg | 6 ++++++ enter | 12 ++++++++++++ makefile | 14 ++++++++++++++ pip-requirements.txt | 27 +++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 1 deletion(-) create mode 100755 enter create mode 100644 pip-requirements.txt diff --git a/.gitignore b/.gitignore index 2459481..d26dcca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ +/ansible_collections credentials/** facts/** +/venv/** public_keys/** __pycache__/ !README.md @@ -10,3 +12,4 @@ __pycache__/ /*.yml !/site.yml !/hosts.yml +!/collection_requirements.yml diff --git a/.vscode/settings.json b/.vscode/settings.json index 298dd16..cd69ab7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,5 +11,6 @@ }, "files.exclude": { "playbooks/{credentials,filter_plugins,group_vars,helpers,host_vars,public_keys,roles}/": true - } + }, + "python.pythonPath": "/home/zocker/Repositories/ansible2/venv/bin/python", } diff --git a/ansible.cfg b/ansible.cfg index 72fa049..38e60cd 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -10,6 +10,12 @@ force_handlers = True # select custom inventory parser for setting up inventory inventory = ./hosts.py +# install & use ansible collections locally (similar to venv) instead of globally +# helps to prevent differences on developer machines to be disturbing +# collections will be automatically setup from the dependency list "collection-requirements.yml" using "make ansible_collections" +# requires dev's to documentate each external dependency inside the repository +collections_path = ./ # ansible then searches for the subdirectory "ansible_collections" for itself + # disable usage of cowsay for ansible-playbook's logging (increases readability drastically, only matters if cowsay is installed) nocows = True diff --git a/enter b/enter new file mode 100755 index 0000000..b294563 --- /dev/null +++ b/enter @@ -0,0 +1,12 @@ +#!/bin/echo You need to source this script! Use something like: source + +# (re-)create env if required (e.g. requirements.txt changed) +make setup + +# enable coloring on these tools +export ANSIBLE_FORCE_COLORS=1 +export PY_COLORS=1 + +# enter venv +. ./venv/bin/activate + diff --git a/makefile b/makefile index c5792f3..b4ff7e7 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,7 @@ playbooks_dir:=playbooks playbooks:=$(wildcard ${playbooks_dir}/*.yml) credentials_dir:=credentials credentials_file:=misc/credentials.tar.gpg +venv_dir:=venv # Default Target (must be first target) @@ -10,6 +11,19 @@ credentials_file:=misc/credentials.tar.gpg main: ansible-playbook site.yml +# Virtual Environment's Setup + +.PHONY: setup +setup: ansible_collections ${venv_dir} + +ansible_collections: collection-requirements.yml ${venv_dir} + mkdir --parent $@ + . ./${venv_dir}/bin/activate && ansible-galaxy install -r $< + +${venv_dir}: pip-requirements.txt + python3 -m venv $@ + . ./$@/bin/activate && python3 -m pip install -r $< + # Playbook Execution .PHONY: list diff --git a/pip-requirements.txt b/pip-requirements.txt new file mode 100644 index 0000000..049f1a6 --- /dev/null +++ b/pip-requirements.txt @@ -0,0 +1,27 @@ +#### Python / PiP Requirements #### + +# each group either sorted by alphabet or, if applicable, sorted by hierachy + + +### Main Runtime Dependencies ### + +# Ansible itself +ansible ~= 2.10.0 # pinned to 2.10 because upgrade may bring issues + + +### Test Frameworks ### + +ansible-lint # simple linter +yamllint # linter for YAML files in general + +## molecule ## +# role based test framework for Ansible + +molecule + +# enable docker for test environments, requires Docker to be installed on host and usuable without additional permissions +molecule-docker + +# allows using Vagrant (VMs) for creating test environments, requires Vagrant and any hypervisor (e.g. VirtualBox) to be installed +molecule-vagrant +python-vagrant # extra module required as not always installed with vagrant