-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
135 lines (114 loc) · 5.09 KB
/
Makefile
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# ------------------
# ANSIBLE-MAKEFILE v0.15.0
# Run ansible commands with ease
# ------------------
#
# Copyright (C) 2017 Paul(r)B.r
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Additions made by Prodromos Kolyvakis:
# * Added: ``docker-test`` so as to test the ansible playbook on Docker.
# * Added: ``repo-update`` so as to update the repo.
SHELL:=/bin/bash
##
# VARIABLES
##
playbook ?= setup
roles_path ?= "roles/"
env ?= hosts
mkfile_dir ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
ifeq ("$(wildcard $(mkfile_dir)/aux_scripts/pass.sh)", "")
opts ?= $(args)
else # Handle vault password if any
ifeq ("$(shell $(mkfile_dir)/aux_scripts/pass.sh 2> /dev/null)", "")
opts ?= $(args)
else
opts ?= $(args) --vault-password-file=$(mkfile_dir)pass.sh
endif
endif
ifneq ("$(limit)", "")
opts := $(opts) --limit="$(limit)"
endif
ifneq ("$(tag)", "")
opts := $(opts) --tag="$(tag)"
endif
##
# TASKS
##
.PHONY: install
install: ## make install [roles_path=roles/] # Install roles dependencies
@ansible-galaxy install --roles-path="$(roles_path)" --role-file="requirements.yml" $(args)
.PHONY: fetch-inventory
fetch-inventory: ## make fetch-inventory [provider=<ec2|gce...>] [env=hosts] # Download dynamic inventory from Ansible's contrib
@wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/$(provider).py
@chmod +x $(provider).py
mv $(provider).py $(env)
.PHONY: inventory
inventory: ## make inventory [env=hosts] # Display the inventory as seen from Ansible
@env=$(env) ansible-inventory --graph -i $(env) $(opts)
.PHONY: lint
lint: ## make lint [playbook=setup] [env=hosts] [args=<ansible-playbook arguments>] # Check syntax of a playbook
@env=$(env) ansible-playbook --inventory-file="$(env)" --syntax-check $(opts) "$(playbook).yml"
.PHONY: debug
debug: mandatory-host-param ## make debug host=hostname [env=hosts] [args=<ansible arguments>] # Debug a host's variable
@env=$(env) ansible -i $(env) $(opts) -m setup $(host)
@env=$(env) ansible --inventory-file="$(env)" $(opts) --module-name="debug" --args="var=hostvars[inventory_hostname]" $(host)
.PHONY: dry-run
dry-run: ## make dry-run [playbook=setup] [env=hosts] [tag=<ansible tag>] [limit=<ansible host limit>] [args=<ansible-playbook arguments>] # Run a playbook in dry run mode
@env=$(env) ansible-playbook --inventory-file="$(env)" --diff --check $(opts) "$(playbook).yml"
.PHONY: run
run: ## make run [playbook=setup] [env=hosts] [tag=<ansible tag>] [limit=<ansible host limit>] [args=<ansible-playbook arguments>] # Run a playbook
@env=$(env) ansible-playbook --inventory-file="$(env)" --diff $(opts) "$(playbook).yml"
group ?=all
.PHONY: list
list: ## make list [group=all] [env=hosts] # List hosts inventory
@env=$(env) ansible --inventory-file="$(env)" $(group) --list-hosts
.PHONY: vault
vault: mandatory-file-param ## make vault file=/tmp/vault.yml [env=hosts] [args=<ansible-vault arguments>] # Edit or create a vaulted file
@[ -f "$(file)" ] && env=$(env) ansible-vault $(opts) edit "$(file)" || \
env=$(env) ansible-vault $(opts) create "$(file)"
.PHONY: console
console: ## make console [env=hosts] [args=<ansible-console arguments>] # Run an ansible console
@env=$(env) ansible-console --inventory-file="$(env)" $(opts)
group ?=all
.PHONY: facts
facts: ## make facts [group=all] [env=hosts] [args=<ansible arguments>] # Gather facts from your hosts
@env=$(env) ansible --module-name="setup" --inventory-file="$(env)" $(opts) --tree="out/" $(group)
.PHONY: cmdb
cmdb: ## make cmdb # Create HTML inventory report
@ansible-cmdb "out/" > list-servers.html
.PHONY: bootstrap
bootstrap: ## make bootstrap # Install ansible (Ubuntu only)
@apt-get install -y software-properties-common && \
apt-add-repository ppa:ansible/ansible && \
apt-get update && \
apt-get install -y ansible
.PHONY: docker-test
docker-test: ## make a test # Test on an Ubuntu docker instance.
@./aux_scripts/test.sh
.PHONY: repo-update
repo-update: ## make a repo update # Update the repo if no local changes are made.
@./aux_scripts/repo-update.sh
.PHONY: dotfiles-update
dotfiles-update: ## update only the dotfiles and link them accordingly.
@ansible-playbook -i inventory main.yml -b -K --tags "dotfiles,dotfiles-update"
.PHONY: mandatory-host-param mandatory-file-param
mandatory-host-param:
@[ ! -z $(host) ]
mandatory-file-param:
@[ ! -z $(file) ]
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.DEFAULT_GOAL := help