forked from ahmedfaragmostafa/Ansible-in-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2a841cb
Showing
24 changed files
with
626 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
.vault_pass.txt |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
.DEFAULT_GOAL := help | ||
|
||
help: | ||
@echo "" | ||
@echo "Available tasks:" | ||
@echo " encrypt-vars Encrypt your vars file" | ||
@echo " decrypt-vars Decrypt your vars file" | ||
@echo " apply Deploy your changes into your hosts" | ||
@echo " safe-apply Deploy your changes into your hosts using vault pass" | ||
@echo " code-deploy Pull latest changes " | ||
@echo " safe-code-deploy Pull latest changes with vault pass" | ||
@echo " update-config Update your env file" | ||
@echo " safe-update-config Update your env file with vault pass" | ||
|
||
@echo "" | ||
|
||
encrypt-vars: | ||
ansible-vault encrypt group_vars/vars.yml --vault-password-file .vault_pass.txt | ||
|
||
decrypt-vars: | ||
ansible-vault decrypt group_vars/vars.yml --vault-password-file .vault_pass.txt | ||
|
||
apply: | ||
ansible-playbook site.yml | ||
|
||
safe-apply: | ||
ansible-playbook site.yml --vault-password-file .vault_pass.txt | ||
|
||
|
||
code-deploy: | ||
ansible-playbook code-deploy.yml | ||
|
||
safe-code-deploy: | ||
ansible-playbook code-deploy.yml --vault-password-file .vault_pass.txt | ||
|
||
update-config: | ||
ansible-playbook code-deploy.yml --tags="env-file" | ||
|
||
safe-update-config: | ||
ansible-playbook code-deploy.yml --tags="env-file" --vault-password-file .vault_pass.txt | ||
|
||
|
||
|
||
.PHONY: help encrypt-vars decrypt-vars apply safe-apply |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
### Disclaimer | ||
This project will help you to deploy your Laravel project to your VPS without a headache | ||
it contains by default PHP - MySql - Redis - Nginx | ||
|
||
I suffered a lot while migrating my code base from VPS to another, I migrated my code base like 20 times between different servers | ||
then I had to automate the process to save my time | ||
|
||
currently I able to migrate from VPS to another in less than 5 minutes without any human interactions, | ||
i update my `hosts.ini`, run a single command `make apply` then I bring my coffee, watch Ansible. | ||
|
||
Pull my latest changes using a single command `make code-deploy` | ||
|
||
|
||
##Project structure | ||
``` | ||
├── README.MD | ||
├── MakeFile # file contains some aliases to get started quickly | ||
├── .vault_pass.txt # file contains your vault config pass | ||
├── ansible.cfg # contains ansible default config | ||
├── code-deploy.yml # playbook to pull latest changes and deploy code | ||
├── files # file contains sql_dump if you have one | ||
│ └── dump.sql | ||
├── group_vars # file to store all your settings [ssh-keys,php_version,..etc] | ||
│ └── vars.yml | ||
├── handlers # basic handlers to restart nginx || php-fpm | ||
│ └── main.yml | ||
├── hosts.ini # file that contains your host IP | ||
├── logs # this file contains error log details | ||
│ └── ansible-log.log | ||
├── roles | ||
│ ├── bootstrap-app | ||
│ │ └── tasks | ||
│ │ └── main.yml | ||
│ ├── code-deploy | ||
│ │ ├── tasks | ||
│ │ │ ├── config-files.yml | ||
│ │ │ └── main.yml | ||
│ │ └── templates | ||
│ │ └── env.conf | ||
│ ├── misc | ||
│ │ └── tasks | ||
│ │ └── main.yml | ||
│ ├── mysql | ||
│ │ └── tasks | ||
│ │ ├── config.yml | ||
│ │ └── main.yml | ||
│ ├── nginx | ||
│ │ ├── tasks | ||
│ │ │ └── main.yml | ||
│ │ └── templates | ||
│ │ └── nginx.conf | ||
│ ├── php | ||
│ │ └── tasks | ||
│ │ └── main.yml | ||
│ └── redis | ||
│ └── tasks | ||
│ └── main.yml | ||
├── scripts # some important scripts | ||
│ ├── install_composer.sh | ||
│ └── startup.sh | ||
└── site.yml # Main playbook to deploy all tasks | ||
``` | ||
|
||
## Quick start | ||
Before you start make sure you did these steps into your VPS | ||
you can add it as a startup script | ||
|
||
``` | ||
#!/bin/sh | ||
sudo add-apt-repository ppa:deadsnakes/ppa -y | ||
sudo apt-get update | ||
sudo apt-get install -y python2.7 python3 python-pip | ||
``` | ||
|
||
## Update ssh-keys | ||
you have to ad you server id_rsa.pub to your github access keys | ||
by login to your instance and | ||
``` | ||
ssh-keygen | ||
sudo chmod -R 644 .ssh/id_rsa | ||
cat .ssh/id_rsa.pub | ||
# add it to your git account | ||
``` | ||
|
||
### Update `hosts.ini` file | ||
``` | ||
[aws] | ||
#your server IP | ||
127.0.0.39 | ||
``` | ||
|
||
### Update `vars.yml` file | ||
you have to define you config inside `group_vars/vars.yml` | ||
|
||
``` | ||
#for example | ||
ansible_ssh_user: "ubuntu" | ||
current_user: "ubuntu" | ||
server_name: "your WebSiteName" | ||
repo_git_url: "you Repo" | ||
mysql_db: sql_db | ||
mysql_user: sql_user | ||
mysql_pass: "put_your_db_pass" | ||
``` | ||
|
||
by default, all these configs will be populated to `.env` | ||
have a look at `env.conf` file | ||
|
||
|
||
|
||
## Encrypt & Decrypt your config using vault | ||
``` | ||
#first create .vault_pass.txt to save your key | ||
touch .vault_pass.txt | ||
echo 'YOUR_CONFIG_PASS' > .vault_pass.txt | ||
# encrypt your config | ||
make encrypt-vars | ||
# decrypt your config | ||
make decrypt-vars | ||
``` | ||
|
||
## Note [optional step] | ||
If you encrypted your config don't forget to use `--vault-password-file .vault_pass.txt` | ||
i added both options if you use any commands that start with `safe-` | ||
if you don't use vault use commands that don't have `safe-` | ||
|
||
``` | ||
#with vault pass | ||
make safe-apply | ||
# without | ||
make apply | ||
``` | ||
|
||
## MakeFile | ||
run `make` inside project dir you will get following tasks:- | ||
|
||
``` | ||
Available tasks: | ||
encrypt-vars Encrypt your vars file | ||
decrypt-vars Decrypt your vars file | ||
apply Deploy your changes into your hosts | ||
safe-apply Deploy your changes into your hosts using vault pass | ||
code-deploy Pull latest changes | ||
safe-code-deploy Pull latest changes with vault pass | ||
update-config Update your env file | ||
safe-update-config Update your env file with vault pass | ||
``` | ||
|
||
|
||
## Apply your changes | ||
|
||
``` | ||
# RUN PLAY BOOK | ||
make apply | ||
#to pull latest changes | ||
make code-deploy | ||
# run specific tag | ||
make apply --tags="php" -vv | ||
#install specfic tag | ||
make apply --tags="php" -vv | ||
#update config files | ||
make update-config | ||
``` | ||
|
||
## TODO | ||
- [ ] Adding terraform | ||
- [ ] clone repo using local id_rsa | ||
- [ ] Ansible as provision in terraform | ||
- [ ] automate startup script | ||
- [ ] automate and provision instances and generate id_rsa key | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[defaults] | ||
hostfile = hosts.ini | ||
# configure log dir | ||
log_path= logs/ansible-log.log |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
- hosts: aws | ||
sudo: yes | ||
vars_files: | ||
- ./group_vars/vars.yml | ||
roles: | ||
- code-deploy | ||
#include service handlers | ||
handlers: | ||
- include: handlers/main.yml |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--- your sql dump if you have one ! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
##@ref https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#variables-and-vaults | ||
ansible_ssh_user: "ubuntu" | ||
current_user: "ubuntu" | ||
server_name: "app_name" | ||
|
||
repo_git_url: "app_github_url" | ||
ansible_ssh_private_key_file: "ssh_dir" | ||
php_version: 7.2 | ||
app_work_dir: /var/www/app_name/ | ||
#mysql config | ||
mysql_host: "mysql_host" | ||
mysql_db: app_name | ||
mysql_user: sql_user | ||
mysql_pass: sql_pass | ||
|
||
#other config | ||
cache_driver: file | ||
session_driver: file | ||
app_env: production | ||
app_debug: false | ||
app_key: "your_app_key" | ||
app_name: "app_name" | ||
app_url: "your_app_url" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
- name: Restart PHP-FPM | ||
service: | ||
name: php{{php_version}}-fpm | ||
state: restarted | ||
#### | ||
- name: Restart Nginx | ||
service: | ||
name: nginx | ||
state: restarted |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[aws] | ||
#your host static IP | ||
127.0.0.39 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
- name: Clone your repo | ||
git: | ||
repo: "{{repo_git_url}}" | ||
dest: "{{app_work_dir}}" | ||
version: master | ||
accept_hostkey: yes | ||
key_file: /home/{{current_user}}/.ssh/id_rsa | ||
tags: | ||
- clone-repo | ||
|
||
- name: Check for Composer | ||
stat: | ||
path: /usr/local/bin/composer | ||
register: composer_stat | ||
#### | ||
|
||
- name: Download Composer | ||
script: scripts/install_composer.sh | ||
when: not composer_stat.stat.exists | ||
|
||
#### | ||
- name: Move Composer Globally | ||
become: true | ||
command: mv composer.phar /usr/local/bin/composer | ||
when: not composer_stat.stat.exists | ||
### | ||
- name: Set permissions on Composer | ||
become: true | ||
file: | ||
path: /usr/local/bin/composer | ||
mode: "a+x" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
- name: Copy lara env file | ||
become: true | ||
template: | ||
src: templates/env.conf | ||
dest: "{{app_work_dir}}/.env" | ||
tags: | ||
- env-file |
Oops, something went wrong.