aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 91211352d8f53a4f1f846c6da2c61529c55e83c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# ANSIBLE-MAKEFILE

SHELL:=/usr/bin/env bash

##
# VARIABLES
##
playbook         ?= setup
roles_path       ?= "roles/"
env_path         ?= "inventories/"
group            ?= all
venv_path  	 := "${HOME}/.venv/vps/"

mkfile_dir       ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
ifeq ("$(wildcard $(mkfile_dir)/vaultid)", "")
	opts     ?= $(args)
else # Handle vault password if any
	opts     ?= $(args) --vault-password-file=$(mkfile_dir)/vaultid
endif

ifneq ("$(limit)", "")
	opts     := $(opts) --limit="$(limit)"
endif

ifneq ("$(tags)", "")
	opts     := $(opts) --tags="$(tags)"
endif

ifneq ("$(skip)", "")
	opts     := $(opts) --skip-tags="$(skip)"
endif

##
# TASKS
##
.PHONY: install
install: ## make install # Install ansible, ansible-lint, and required collections. Reveal vaultid file.
	@test -d ${venv_path} || (python3 -m venv ${venv_path} --system-site-packages)
	@source ${venv_path}/bin/activate && pip install -r requirements.txt
	@source ${venv_path}/bin/activate && ansible-galaxy install -r requirements.yml
	@source ${venv_path}/bin/activate && ansible-galaxy collection install --ignore-errors -r requirements.yml
	@[ -f "vaultid" ] && source ${venv_path}/bin/activate && ansible-vault decrypt --vault-id vaultid --output hosts hosts.vault || echo "Nothing to decrypt with. Bye."

.PHONY: lint
lint: mandatory-file-param ## make lint file=playbooks/template_playbook.yaml # Run ansible-lint on a playbook or tasks file.
	@[ -f "$(file)" ] && ANSIBLE_VAULT_PASSWORD_FILE=vaultid ansible-lint -c ansible-lint.yaml "$(file)"

.PHONY: syntax
syntax: mandatory-file-param ## make syntax file=playbooks/template_playbook.yaml [args=<ansible-playbook arguments>] # Check syntax of a playbook.
	@[ -f "$(file)" ] && ansible-playbook --inventory-file="hosts" --syntax-check $(opts) "$(file)"

.PHONY: dry-run
dry-run: mandatory-file-param ## make dry-run file=playbooks/template_playbook.yaml [tags=<ansible tags>] [limit=<ansible host limit>] [args=<ansible-playbook arguments>] # Run a playbook in dry run mode.
	@[ -f "$(file)" ] && source ${venv_path}/bin/activate && ansible-playbook --inventory-file="hosts" --diff --check $(opts) "$(file)"

.PHONY: run
run: mandatory-file-param ## make run file=playbooks/template_playbook.yaml [tags=<ansible tags>] [limit=<ansible host limit>] [args=<ansible-playbook arguments>] # Run a playbook.
	@[ -f "$(file)" ] && source ${venv_path}/bin/activate && ansible-playbook --inventory-file="hosts" --diff $(opts) "$(file)"

.PHONY: vault-view
vault-view: ## make vault-view [group=all] [args=<ansible-vault arguments>] # View a vaulted file.
	@[ -f "group_vars/${group}/vault" ] && source ${venv_path}/bin/activate  && ansible-vault view $(opts) "group_vars/${group}/vault"

.PHONY: vault-edit
vault-edit: ## make vault-edit [group=all] [args=<ansible-vault arguments>] # Edit a vaulted file.
	@[ -f "group_vars/${group}/vault" ] && source ${venv_path}/bin/activate  && ansible-vault edit $(opts) "group_vars/${group}/vault"

.PHONY: facts
facts: ## make facts [group=all] [args=<ansible arguments>] # Gather facts from your hosts.
	@source ${venv_path}/bin/activate && ansible --module-name="setup" --inventory-file="hosts" $(opts) --tree="out/" $(group)

.PHONY: mandatory-host-param mandatory-file-param
mandatory-host-param:
	@[ ! -z $(host) ]
mandatory-file-param:
	@[ ! -z $(file) ]

.PHONY: help
help: ## make help # Print this help message.
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.DEFAULT_GOAL := help