# ------------------ # 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 . 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=] [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=] # 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=] # 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=] [limit=] [args=] # 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=] [limit=] [args=] # 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=] # 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=] # Run an ansible console @env=$(env) ansible-console --inventory-file="$(env)" $(opts) group ?=all .PHONY: facts facts: ## make facts [group=all] [env=hosts] [args=] # 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