2 # Ansible Boilerplate Collection: Makefile for inclusion into projects using it.
6 SOURCE_ROOT ?= $(CURDIR)
7 VENV_D = $(SOURCE_ROOT)/.venv
8 VENV_BIN = $(VENV_D)/bin
10 this_makefile_path:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
11 this_dir:=$(shell cd $(dir $(this_makefile_path));pwd)
13 check-ansible: $(VENV_BIN)/ansible-lint
14 # Check Ansible executable ...
15 "$(VENV_BIN)"/ansible --version
16 # Check YAML files ...
17 "$(VENV_BIN)"/yamllint .
18 # Run ansible-playbook syntax check, when a "site play" is found ...
19 ifneq ("$(wildcard playbooks/site.yml)","")
20 "$(VENV_BIN)"/ansible-playbook --syntax-check playbooks/site.yml
22 ifneq ("$(wildcard site.yml)","")
23 "$(VENV_BIN)"/ansible-playbook --syntax-check site.yml
25 # Run ansible-lint ...
26 "$(VENV_BIN)"/ansible-lint --offline --project-dir "$(SOURCE_ROOT)" --show-relpath
29 # Remove Python "virtual environment" ...
30 rm -fr "$(SOURCE_ROOT)"/.venv
31 # Remove Ansible Galaxy collections and roles
32 rm -fr "$(SOURCE_ROOT)"/ansible_galaxy/ansible_collections "$(SOURCE_ROOT)"/ansible_galaxy/ansible_roles
33 # Try to remove the "ansible_galaxy" directory (if empty), but ignore errors.
34 rmdir "$(SOURCE_ROOT)"/ansible_galaxy || true
35 # Remove cache directory, used by the Ansible "facts cache" for example ...
36 rm -fr "$(SOURCE_ROOT)"/.cache
37 # Clean up symlinked commands in dependant projects ...
38 for cmd in "$(SOURCE_ROOT)"/bin/a "$(SOURCE_ROOT)"/bin/ap "$(SOURCE_ROOT)"/bin/aps; do \
39 test -h "$$cmd" && rm -f "$$cmd" || true; \
42 $(VENV_BIN)/ansible $(VENV_BIN)/ansible-galaxy $(VENV_BIN)/ansible-lint venv: $(VENV_BIN)/pip
43 # Install/upgrade Python package manager
44 "$(VENV_BIN)"/pip install --upgrade pip wheel
45 touch "$(VENV_BIN)/pip" "$(VENV_BIN)/wheel"
46 # Install/upgrade Python dependencies ...
47 "$(VENV_BIN)"/pip install --upgrade --requirement $(this_dir)/requirements.txt
48 touch "$(VENV_BIN)/ansible" "$(VENV_BIN)/ansible-galaxy" "$(VENV_BIN)/ansible-lint"
51 # Create/upgrade Python "virtual environment"
52 "$(PYTHON)" -m venv "$(SOURCE_ROOT)"/.venv
56 ifneq ($(patsubst %..,,$(lastword $(SOURCE_ROOT))),)
57 # SOURCE_ROOT does not end in "..", so looks like this Makefile fragment is
58 # included in the top-level Makefile. So add some proprietary targets to the
59 # "common" toplevel targets:
61 all: $(VENV_BIN)/ansible $(VENV_BIN)/ansible-galaxy $(VENV_BIN)/ansible-lint
63 distclean: distclean-ansible
65 upgrade: $(SOURCE_ROOT)/bin/ansible-boilerplate
66 $(SOURCE_ROOT)/bin/ansible-boilerplate upgrade
68 .PHONY: check distclean