Skip to content

Commit

Permalink
Add ansible scripts from phansible.com
Browse files Browse the repository at this point in the history
markstory committed Nov 30, 2016
1 parent 335c244 commit 7142997
Showing 24 changed files with 384 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
##################################################
# Generated by phansible.com
##################################################

#If your Vagrant version is lower than 1.5, you can still use this provisioning
#by commenting or removing the line below and providing the config.vm.box_url parameter,
#if it's not already defined in this Vagrantfile. Keep in mind that you won't be able
#to use the Vagrant Cloud and other newer Vagrant features.
Vagrant.require_version ">= 1.5"

# Check to determine whether we're on a windows or linux/os-x host,
# later on we use this to launch ansible in the supported way
# source: https://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable? exe
}
end
return nil
end

Vagrant.configure("2") do |config|

config.vm.provider :virtualbox do |v|
v.name = "default"
v.customize [
"modifyvm", :id,
"--name", "default",
"--memory", 512,
"--natdnshostresolver1", "on",
"--cpus", 1,
]
end

config.vm.box = "ubuntu/trusty64"

config.vm.network :private_network, ip: "192.168.33.99"
config.ssh.forward_agent = true

# If ansible is in your path it will provision from your HOST machine
# If ansible is not found in the path it will be instaled in the VM and provisioned from there
if which('ansible-playbook')
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/playbook.yml"
ansible.inventory_path = "ansible/inventories/dev"
ansible.limit = 'all'
end
else
config.vm.provision :shell, path: "ansible/windows.sh", args: ["default"]
end

config.vm.synced_folder "./", "/vagrant", type: "nfs"
end
1 change: 1 addition & 0 deletions ansible/files/authorized_keys
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
2 changes: 2 additions & 0 deletions ansible/inventories/dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[phansible-web]
192.168.33.99
13 changes: 13 additions & 0 deletions ansible/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- hosts: all
sudo: true
vars_files:
- vars/all.yml
roles:
- server
- vagrant_local
- apache
- mysql
- php
- composer
- app
3 changes: 3 additions & 0 deletions ansible/roles/apache/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: restart apache
service: name=apache2 enabled=yes state=restarted
29 changes: 29 additions & 0 deletions ansible/roles/apache/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- name: Install Apache
sudo: yes
apt: pkg=apache2 state=latest

- name: Install Apache Modules
apache2_module: state=present name={{ item }}
notify: restart apache
with_items:
- rewrite
- vhost_alias
- headers
- expires
- filter

- shell: apache2 -v
register: apache_version

- name: Change default apache2.4 site
sudo: yes
template: src=vhost24.conf.tpl dest=/etc/apache2/sites-available/000-default.conf
notify: restart apache
when: apache_version.stdout.find('Apache/2.4.') != -1

- name: Change default apache2.2 site
sudo: yes
template: src=vhost22.conf.tpl dest=/etc/apache2/sites-available/default
notify: restart apache
when: apache_version.stdout.find('Apache/2.2.') != -1
14 changes: 14 additions & 0 deletions ansible/roles/apache/templates/vhost22.conf.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Default Apache virtualhost template

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot {{ apache.docroot }}
ServerName {{ apache.servername }}

<Directory {{ apache.docroot }}>
AllowOverride All
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
13 changes: 13 additions & 0 deletions ansible/roles/apache/templates/vhost24.conf.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Default Apache virtualhost template

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot {{ apache.docroot }}
ServerName {{ apache.servername }}

<Directory {{ apache.docroot }}>
AllowOverride All
Options -Indexes +FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
2 changes: 2 additions & 0 deletions ansible/roles/app/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# application tasks to be customized and to run after the main provision
2 changes: 2 additions & 0 deletions ansible/roles/composer/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- name: Install Composer
shell: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer creates=/usr/local/bin/composer
36 changes: 36 additions & 0 deletions ansible/roles/mysql/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# Retrieve the current hostname, because {{ ansible_hostname }} still contains the old name
- shell: hostname
register: current_hostname

- name: mysql | Install MySQL Packages
sudo: yes
apt: pkg={{ item }} state=latest
with_items:
- mysql-server
- mysql-client
- python-mysqldb

- name: mysql | Update root password for all root accounts
mysql_user: name=root host={{ item }} check_implicit_admin=yes password={{ mysql.root_password }} login_user=root login_password={{ mysql.root_password }}
with_items:
- "{{ current_hostname.stdout | lower }}"
- 127.0.0.1
- ::1
- localhost

- name: mysql | Create databases
mysql_db: name={{ mysql.database }} state=present login_user=root login_password={{ mysql.root_password }}

- name: mysql | Import dump
mysql_db: name={{ mysql.database }} state=import login_user=root login_password={{ mysql.root_password }} target=/vagrant/{{ mysql.dump }}
when: mysql.dump

- name: mysql | Ensure anonymous users are not in the database
mysql_user: name='' host={{ item }} state=absent login_user=root login_password={{ mysql.root_password }}
with_items:
- localhost
- "{{ current_hostname.stdout | lower }}"

- name: mysql | Create users
mysql_user: name={{ mysql.user }} password={{ mysql.password }} priv={{ mysql.database }}.*:ALL state=present login_user=root login_password={{ mysql.root_password }}
3 changes: 3 additions & 0 deletions ansible/roles/php/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: restart php5-fpm
service: name=php5-fpm enabled=yes state=restarted
19 changes: 19 additions & 0 deletions ansible/roles/php/tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- stat: path=/etc/php5/apache2/php.ini
register: modphp

- stat: path=/etc/php5/fpm/php.ini
register: phpfpm

- stat: path=/etc/php5/cli/php.ini
register: phpcli

- include: php-fpm.yml
when: phpfpm.stat.exists

- include: php-cli.yml
when: phpcli.stat.exists

- include: mod-php.yml
when: modphp.stat.exists

25 changes: 25 additions & 0 deletions ansible/roles/php/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
- name: Add ppa Repository
sudo: yes
apt_repository: repo=ppa:ondrej/{{ php.ppa }}

- name: Update apt
sudo: yes
apt: update_cache=yes

- name: Install php5
sudo: yes
apt: pkg=php5 state=latest

- name: Install php5-fpm
sudo: yes
apt: pkg=php5-fpm state=latest

- name: Install PHP Packages
sudo: yes
apt: pkg={{ item }} state=latest
with_items: php.packages
when: php.packages is defined

- include: configure.yml
- include: pecl.yml
10 changes: 10 additions & 0 deletions ansible/roles/php/tasks/mod-php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: ensure timezone is set in apache2 php.ini
lineinfile: dest=/etc/php5/apache2/php.ini
regexp='date.timezone ='
line='date.timezone = {{ server.timezone }}'

- name: enabling opcache
lineinfile: dest=/etc/php5/apache2/php.ini
regexp=';?opcache.enable=\d'
line='opcache.enable=1'
26 changes: 26 additions & 0 deletions ansible/roles/php/tasks/pecl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- name: Install
apt: pkg="php5-dev" state=present
when: php.pecl_packages is defined

- name: Install Package
shell: echo "\n\n\n\n\n\n\n\n\n" | pecl install {{ item }}
register: pecl_result
changed_when: "'already installed' not in pecl_result.stdout"
failed_when: "pecl_result.stderr or ('ERROR' in pecl_result.stdout)"
with_items: php.pecl_packages
when: php.pecl_packages is defined

- name: Create extension .ini file
template: >
src="extension.tpl"
dest="/etc/php5/mods-available/{{ item }}.ini"
owner="root"
group="root"
mode=0644
with_items: php.pecl_packages
when: php.pecl_packages is defined

- name: Enable extension
shell: php5enmod {{ item }}
with_items: php.pecl_packages
when: php.pecl_packages is defined
10 changes: 10 additions & 0 deletions ansible/roles/php/tasks/php-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: ensure timezone is set in cli php.ini
lineinfile: dest=/etc/php5/cli/php.ini
regexp='date.timezone ='
line='date.timezone = {{ server.timezone }}'

- name: enabling opcache cli
lineinfile: dest=/etc/php5/cli/php.ini
regexp=';?opcache.enable_cli=\d'
line='opcache.enable_cli=1'
19 changes: 19 additions & 0 deletions ansible/roles/php/tasks/php-fpm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- name: Set permissions on socket - owner
lineinfile: "dest=/etc/php5/fpm/pool.d/www.conf state=present regexp='^;?listen.owner' line='listen.owner = www-data'"

- name: Set permissions on socket - group
lineinfile: "dest=/etc/php5/fpm/pool.d/www.conf state=present regexp='^;?listen.group' line='listen.group = www-data'"

- name: Set permissions on socket - mode
lineinfile: "dest=/etc/php5/fpm/pool.d/www.conf state=present regexp='^;?listen.mode' line='listen.mode = 0660'"
notify: restart php5-fpm

- name: ensure timezone is set in fpm php.ini
lineinfile: dest=/etc/php5/fpm/php.ini
regexp='date.timezone ='
line='date.timezone = {{ server.timezone }}'
- name: enabling opcache
lineinfile: dest=/etc/php5/fpm/php.ini
regexp=';?opcache.enable=\d'
line='opcache.enable=1'
2 changes: 2 additions & 0 deletions ansible/roles/php/templates/extension.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
; Configuration for php PECL {{ item }} extension
extension={{ item }}.so
31 changes: 31 additions & 0 deletions ansible/roles/server/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Update apt
sudo: yes
apt: update_cache=yes

- name: Install System Packages
sudo: yes
apt: pkg={{ item }} state=latest
with_items:
- curl
- wget
- python-software-properties

- name: Install Extra Packages
sudo: yes
apt: pkg={{ item }} state=latest
with_items: server.packages
when: server.packages is defined

- name: Configure the timezone
sudo: yes
template: src=timezone.tpl dest=/etc/timezone

- name: More Configure the timezone
sudo: yes
file: src=/usr/share/zoneinfo/{{server.timezone}} dest=/etc/localtime state=link force=yes backup=yes

- name: Set default system language pack
shell: locale-gen {{server.locale}}
sudo: yes

1 change: 1 addition & 0 deletions ansible/roles/server/templates/timezone.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{server.timezone}}
11 changes: 11 additions & 0 deletions ansible/roles/vagrant_local/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Set the hostname in /etc/hostname
shell: echo {{ vagrant_local.vm.hostname }} > /etc/hostname
when: vagrant_local.vm.hostname is defined

- name: Set the hostname
shell: hostname {{ vagrant_local.vm.hostname }}
when: vagrant_local.vm.hostname is defined

- name: Update /etc/hosts
lineinfile: dest=/etc/hosts regexp='^127\.0\.0\.1' line='127.0.0.1 localhost {{ vagrant_local.vm.hostname }}' owner=root group=root mode=0644
25 changes: 25 additions & 0 deletions ansible/vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
server:
install: '1'
timezone: UTC
locale: en_US.UTF-8
vagrant_local:
install: '1'
vm: { base_box: trusty64, hostname: default, ip: 192.168.33.99, memory: '512', sharedfolder: ./, useVagrantCloud: '1', syncType: nfs }
apache:
install: '1'
docroot: /vagrant
servername: bookmarker
mysql:
install: '1'
root_password: ''
database: my_app
user: my_app
password: secret
dump: ''
php:
install: '1'
ppa: php5-5.6
packages: [php5-cli, php5-intl, php5-mcrypt, php5-mysql]
composer:
install: '1'
31 changes: 31 additions & 0 deletions ansible/windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# Update Repositories
sudo apt-get update

# Determine Ubuntu Version
. /etc/lsb-release

# Decide on package to install for `add-apt-repository` command
#
# USE_COMMON=1 when using a distribution over 12.04
# USE_COMMON=0 when using a distribution at 12.04 or older
USE_COMMON=$(echo "$DISTRIB_RELEASE > 12.04" | bc)

if [ "$USE_COMMON" -eq "1" ];
then
sudo apt-get install -y software-properties-common
else
sudo apt-get install -y python-software-properties
fi

# Add Ansible Repository & Install Ansible
sudo add-apt-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

# Setup Ansible for Local Use and Run
cp /vagrant/ansible/inventories/dev /etc/ansible/hosts -f
chmod 666 /etc/ansible/hosts
cat /vagrant/ansible/files/authorized_keys >> /home/vagrant/.ssh/authorized_keys
sudo ansible-playbook /vagrant/ansible/playbook.yml -e hostname=$1 --connection=local

0 comments on commit 7142997

Please sign in to comment.