Forked from pgporada/gist:3e72eaf1ef71588113f06bb60ba4596c
Last active
May 6, 2019 04:54
-
-
Save YourFriendCaspian/7c4376bda77dc2b7029e593573aeccc0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat Makefile | |
.PHONY: all | |
.DEFAULT_GOAL := help | |
.ONE_SHELL: | |
SHELL = /bin/bash | |
FOLDER = $(shell basename "$$(pwd)" | cut -d'-' -f3) | |
help: ## Show the available CLI arguments | |
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' | |
@echo "[+] Example:" | |
@echo "[+]" | |
@echo "[+] *Running the playbook on AWS*" | |
@echo "[+] *---------------------------*" | |
@echo "[+] make install-python-requirements" | |
@echo "[+] make install-ansible-modules" | |
@echo "[+]" | |
@echo "[+] *Checking hosts before running anything*" | |
@echo "[+] *---------------------------*" | |
@echo "[+] ansible 'jenkins-master,&env,&tier' -m ping -i ansible/environments/aws/" | |
set-env: | |
@if [ -z $(ENVIRONMENT) ]; then\ | |
echo "ENVIRONMENT was not set"; exit 1;\ | |
else\ | |
echo "[+] ENVIRONMENT: $(ENVIRONMENT)";\ | |
fi | |
@if [ -z $(TIER) ]; then\ | |
echo "TIER was not set"; exit 1;\ | |
else\ | |
echo "[+] TIER: $(TIER)";\ | |
fi | |
set-role: | |
@if [ -z $(ROLE) ]; then\ | |
echo "ROLE was not set"; exit 1;\ | |
else\ | |
echo "[+] ROLE: $(ROLE)";\ | |
fi | |
set-hosts: | |
@if [ -z '$(HOSTS)' ]; then\ | |
echo "HOSTS was not set"; exit 1;\ | |
else\ | |
echo "[+] HOSTS: '$(HOSTS)'";\ | |
fi | |
get-versions: ## Gather version of the checked out roles by checking the VERSION file in a repository | |
@echo "$$(tput bold)Versions that you have checked out in your projects$$(tput sgr0)"; \ | |
for i in $$(grep src ansible/requirements.yml | awk '{print $$3}' | sed -e 's|[email protected]:greenlancer/||g' -e 's/.git//g'); do \ | |
pushd ~/work-repos/ansible/$$i > /dev/null; \ | |
printf "%-30s - %-8s - %-42s\n" $$i $$(cat VERSION) $$(git rev-parse --abbrev-ref HEAD); \ | |
popd > /dev/null; \ | |
done; \ | |
echo; \ | |
echo "$$(tput bold)You should verify your ansible/requirements.yml now$$(tput sgr0)" | |
install-python-requirements: ## Uses pip to install required python modules from requirements.txt | |
@echo "[+] Installing python modules via pip" | |
@pip install -r requirements.txt | |
install-ansible-modules: ## Uses ansible-galaxy to install roles from ansible/requirements.yml | |
@echo "[+] Installing ansible modules via galaxy" | |
@cd ansible && ansible-galaxy install -r requirements.yml --force | |
install-all-dependencies: install-python-requirements install-ansible-modules ## Install all the project pip/role dependencies | |
install-s3-secrets: set-env ## Gathers all of the secret files from S3 | |
@echo "[+] Getting role variables from S3" | |
aws s3 cp s3://glapp-configs/$(ENVIRONMENT)/$(FOLDER)/$(TIER)/all.yml ansible/environments/aws/group_vars/all.yml || (echo "[-] Failed getting role variables" ; exit 1;) | |
@echo "[+] Ensuring EC2 dynamic inventory ec2.py script is executable" | |
@chmod +x ansible/environments/aws/inventory/ec2.py || (echo "[-] Failed setting execute bit on ec2.py script" ; exit 1;) | |
deploy-aws-remoteconn: set-env set-role set-hosts ## Installs dependencies and uses the set HOSTS variable to ssh to the correct nodes | |
@echo "[+] Executing playbook" | |
@cd ansible ; ansible-playbook playbooks/deploy-aws-$(ROLE).yml -i environments/aws -e "cli_myhosts='$(HOSTS)'" -e cli_env=$(ENVIRONMENT) -e cli_tier=$(TIER) -e cli_role=$(ROLE) || (echo "[-] Ansible playbook fucked up" ; exit 1;) | |
deploy-aws-localconn: set-env set-role set-hosts ## Installs dependencies and uses the set HOSTS variable to ssh to the correct nodes | |
@echo "[+] Executing playbook": | |
@cd ansible ; ansible-playbook playbooks/deploy-aws-$(ROLE).yml --connection=local -i environments/aws -e "cli_myhosts='$(HOSTS)'" -e cli_env=$(ENVIRONMENT) -e cli_tier=$(TIER) -e cli_role=$(ROLE) || (echo "[-] Ansible playbook fucked up" ; exit 1;) | |
deploy-vagrant: ## Installs dependencies and runs the ansible/playbooks/deploy-vagrant.yml on the Vagrant VM | |
@echo "[+] Executing playbook" | |
@cd ansible ; ansible-playbook playbooks/deploy-vagrant.yml --connection=local -i environments/vagrant || (echo "[-] Ansible playbook fucked up" ; exit 1;) | |
deploy-docker: ## Installs dependencies and runs the ansible/playbooks/deploy-vagrant.yml on the Vagrant VM | |
@echo "[+] Executing playbook" | |
@cd ansible ; ansible-playbook playbooks/deploy-docker.yml -i environments/vagrant || (echo "[-] Ansible playbook fucked up" ; exit 1;) | |
- - - - | |
- - - - | |
- - - - | |
$ cat README.md | |
# Overview: Jenkins Playbook | |
This playbook will install and configure a Jenkins master server. The server will contain docker to run slave containers. | |
- - - - | |
# Setup | |
* **Failure to follow these instructions will result in a misconfigured application.** | |
* **Symptoms of a misconfiguration are seeing the Apache default index.html.** | |
- - - - | |
# Variable Definitions | |
- - - - | |
# Using this playbook | |
cd ansible-playbook-jenkins | |
make install-ansible-modules | |
ENVIRONMENT=mgmt TIER=ga make install-s3-secrets | |
cd ansible | |
ansible-playbook -i environments/aws/inventory/ec2.py -e cli_env=mgmt -e cli_tier=ga -e "cli_myhosts=tag_ROLES_jenkins_master:&tag_ENV_mgmt:&tag_TIER_ga" playbooks/deploy-aws-jenkins-master.yml --list-hosts | |
- - - - | |
# Hacking away at this playbook | |
If you need to add new variables to a configuration, you will need to do the following. | |
1. Update the AWS variables (if needed). Copy the variable file out of S3 to your local system | |
* These are comment out for your safety. `ENVIRONMENT=#mgmt TIER=#ga` | |
* `aws s3 cp s3://configs/mgmt/jenkins/ga/all.yml .` | |
1. Make your changes | |
1. Push the changed variable file back to S3 | |
* `ENVIRONMENT=#prod TIER=#ga` | |
* `aws s3 cp all.yml s3://configs/mgmt/jenkins/ga/` | |
1. Review the ansible change log | |
1. If there are errors, make your changes and run `vagrant provision` to re-provision the vagrant. | |
1. If everything looks good | |
$ git status | |
$ # git add * | |
$ git commit -m "MEANINGFUL MESSAGE SHOULD GO HERE. I'M COUNTING ON YOU." | |
$ # Tag it appropriately according to the versioning RFC. https://pgporada.atlassian.net/wiki/display/DEV/RFC+%230011%3A+GLAPP+Versioning | |
$ ./bumpver.sh | |
- - - - | |
# Starting new servers on Amazon | |
### User_data via Terraform | |
1. Based on the ENV and TIER tag set by Terraform, [user_data will be populated](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts) with the correct S3 bucket path(s). | |
1. Application config will be pulled down and placed in | |
* `/root/ansible-jenkins/ansible/environments/aws/group_vars/all.yml` | |
1. The dynamic inventory ec2.ini will be pulled down and placed in `/root/ansible-jenkins/ansible/environments/aws/inventory/ec2.ini` | |
1. The dynamic inventory plugin located at `/root/ansible-jenkins/ansible/environments/aws/inventory/ec2.py` will use the Terraform tags to dynamically build the inventory at runtime. | |
* The inventory is located at `/root/ansible-jenkins/ansible/environments/aws/inventory/hosts` | |
1. The playbook will be executed. | |
- - - - | |
# Theme Music | |
[Desmond Dekker - Perseverance](https://www.youtube.com/watch?v=FYNB7ExBDCo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment