-
Notifications
You must be signed in to change notification settings - Fork 11
/
Makefile
126 lines (106 loc) · 4.79 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
# ------------------
# ANSIBLE-MAKEFILE v0.16.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/>.
SHELL:=/bin/bash
##
# VARIABLES
##
playbook ?= setup
roles_path ?= "roles/"
env ?= hosts
mkfile_dir ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
ifeq ("$(wildcard $(mkfile_dir)pass.sh)", "")
opts ?= $(args)
else # Handle vault password if any
ifeq ("$(shell $(mkfile_dir)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 --ignore-errors --roles-path="$(roles_path)" --role-file="requirements.yml" $(opts)
.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: inventory-graph ## make inventory [env=hosts] # LEGACY replaced by inventory-graph
.PHONY: inventory-graph
inventory-graph: ## make inventory-graph [env=hosts] # Display the inventory as seen from Ansible
@env=$(env) ansible-inventory --graph -i $(env) $(opts)
.PHONY: inventory-list
inventory-list: ## make inventory-list [env=hosts] # Display the inventory as seen from Ansible
@env=$(env) ansible-inventory --list -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 edit $(opts) "$(file)" || \
env=$(env) ansible-vault create $(opts) "$(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: 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