Skip to content

Instantly share code, notes, and snippets.

@okovalov
Last active September 8, 2016 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okovalov/4f4081faf93025161ca90587621455b1 to your computer and use it in GitHub Desktop.
Save okovalov/4f4081faf93025161ca90587621455b1 to your computer and use it in GitHub Desktop.
Following these instructions you would get three virtual machines , whereas one would be a server (ansible machine)
from where ansible would install software into two remaining virtual machines (destination servers).
Prerequsets (installed on a host machine):
- virtual box
- vagrant
List of software which will be installed on the destination servers (basic list, some dependencies will be installed as well):
- git
- nginx
- php
- composer
- mysql
- nodejs
Insturctions:
=====
Destination Servers
=====
On your host machine
mkdir -p ~/Boxes/testServerOne
mkdir -p ~/Boxes/testServerTwo
vim ~/Boxes/testServerOne/Vagrantfile
paste there
```
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.40.11"
config.vm.host_name = 'server-provision-test01'
end
```
vim ~/Boxes/testServerTwo/Vagrantfile
paste there
```
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.40.12"
config.vm.host_name = 'server-provision-test02'
end
```
cd ~/Boxes/testServerOne
vagrant up
cd ~/Boxes/testServerTwo
vagrant up
=======
Ansible Box
=======
On your host machine
mkdir -p ~/Boxes/ansibleBox
cd ~/Boxes/ansibleBox
git clone https://github.com/scotch-io/scotch-box.git ./
vagrant up
vagrant ssh
Inside the ansible box
cd ~/.ssh/
ssh-keygen -t rsa
(please accept the default file name and leave a passphrase empty)
cd ~/
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.40.11
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.40.12
if you had it copied under same ip but for a different machine it would complain about the changed authentication
so please remove it from this box known hosts first
ssh-keygen -f "/home/vagrant/.ssh/known_hosts" -R 192.168.40.11
ssh-keygen -f "/home/vagrant/.ssh/known_hosts" -R 192.168.40.12
Check if you could ssh to both hosts by doing:
`ssh 192.168.40.11` - to ssh to the first server. you should see `vagrant@server-provision-test01:~$`
`exit` - to come back to your ansible box - you sould see `Connection to 192.168.40.11 closed. vagrant@scotchbox:~$`
`ssh 192.168.40.12` - to ssh to the first server. you should see `vagrant@server-provision-test02:~$`
`exit` - to come back to your ansible box - you sould see `Connection to 192.168.40.12 closed. vagrant@scotchbox:~$`
then (please make SURE you are inside your ansible box)
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible
sudo mv /etc/ansible/hosts /etc/ansible/hosts.orig
sudo vim /etc/ansible/hosts
and paste there
```
[web]
192.168.40.11
192.168.40.12
[local]
127.0.0.1
```
mkdir -p ~/Code/Ansible/roles
cd ~/Code/Ansible/roles
Checkout some repos (we use https here rahter the git because this box is obviously not authenticated at github)
git clone https://github.com/geerlingguy/ansible-role-composer.git
git clone https://github.com/geerlingguy/ansible-role-git.git
git clone https://github.com/geerlingguy/ansible-role-mysql.git
git clone https://github.com/geerlingguy/ansible-role-nginx.git
git clone https://github.com/geerlingguy/ansible-role-nodejs.git
git clone https://github.com/geerlingguy/ansible-role-php.git
(still inside of your ansible machine)
ls -la ~/Code/Ansible/roles/
you should have 6 folders there
For those 6 roles make some changes in files and folders
for role ansible-role-nodejs
file tasks/main.yml
vim ~/Code/Ansible/roles/ansible-role-nodejs/tasks/main.yml
replace
nodejs_install_npm_user: "{{ ansible_user }}"
with
nodejs_install_npm_user: "vagrant"
create a new directory
mkdir -p ~/Code/Ansible/roles/ansible-role-nodejs/vars
vim ~/Code/Ansible/roles/ansible-role-nodejs/vars/main.yml
and paste there
nodejs_version: "6.x"
nodejs_npm_global_packages:
- name: eslint
file tasks/setup-Debian.yml
vim ~/Code/Ansible/roles/ansible-role-nodejs/tasks/setup-Debian.yml
replace
- name: Add Nodesource apt key.
apt_key:
url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
state: present
with
- name: Add Nodesource apt key (due to an issue with apt_key we need to add it using this way)
shell: 'curl --silent https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add - '
register: node_key
and add a line
when: node_key|success
after
- name: Add NodeSource repositories for Node.js.
apt_repository:
repo: "{{ item }}"
state: present
with_items:
- "deb https://deb.nodesource.com/node_{{ nodejs_version }} {{ ansible_distribution_release }} main"
- "deb-src https://deb.nodesource.com/node_{{ nodejs_version }} {{ ansible_distribution_release }} main"
register: node_repo
for role ansible-role-php
file defaults/main.yml
vim ~/Code/Ansible/roles/ansible-role-php/defaults/main.yml
repalce
php_enable_php_fpm: false
with
php_enable_php_fpm: true
file tasks/setup-Debian.yml
vim ~/Code/Ansible/roles/ansible-role-php/tasks/setup-Debian.yml
at the beginning of the file add
- name: Add PHP7 Repository
apt_repository: repo='ppa:ondrej/php' state=present
register: ppaphp
after those two lines
- name: Update apt cache.
apt: update_cache=yes cache_valid_time=86400
add one more line
when: ppaphp|success
file vars/Debian.yml
vim ~/Code/Ansible/roles/ansible-role-php/vars/Debian.yml
replace
__php_webserver_daemon: "apache2"
with
__php_webserver_daemon: "nginx"
for role ansible-role-composer
file defaults/main.yml
vim ~/Code/Ansible/roles/ansible-role-composer/defaults/main.yml
replace
composer_home_owner: root
composer_home_group: root
with
composer_home_owner: vagrant
composer_home_group: vagrant
Create main playbook file
vim ~/Code/Ansible/server.yml
and paste there
```
---
- hosts: web
vars_files:
- roles/ansible-role-nodejs/vars/main.yml
roles:
- ansible-role-git
- ansible-role-nginx
- ansible-role-php
- ansible-role-composer
- ansible-role-mysql
- ansible-role-nodejs
```
check if your ansible hosts are reachable
ansible web -m ping
Run ansible playbook
cd ~/Code/Ansible/
ansible-playbook -s server.yml
PS - More about ansible is here - https://serversforhackers.com/an-ansible-tutorial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment