Git Product home page Git Product logo

van's Introduction

Vagrant_Ansible_Nginx

This is a repository for experimenting the combination of Vagrant, Ansible and Nginx.

Design

Design

Prerequisite

For this project, it is required to install Vagrant locally either on a mac/windows OS. As for Ansible, it is not required to install the tool because it will be all executed inside the VM images.

How to use

First clone or download the repository and go the the directory where the following command can be executed to get this setup running.

Vagrant up
This command creates and configures guest machines according to your Vagrantfile.

Vagrant provision
Runs any configured provisioners against the running Vagrant managed machine.

Vagrant destroy
This command stops the running machine Vagrant is managing and destroys all resources that were created during the machine creation process. After running this command, your computer should be left at a clean state, as if you never created the guest machine in the first place.

Once the machines are up and playbook is fully played, go checkout the URL: http://192.168.88.10. If you refresh it few times, it balance between application server 192.168.88.11 and 192.168.88.12.

Vagrantfile

The syntax of the vagrantfile is based on Ruby. It is simple setup for two application servers thats serves the loadbalancer. The application servers can be scaled from two to three simply by changing the values: (1..2).each do |i| into (1..3).each do |i|.

To understand the Ruby each Iterator go to Tutorialspoint.

Vagrant.configure("2") do |config|
        config.vm.define "loadbalancer" do |loadbalancer|
          loadbalancer.vm.box = "puppetlabs/ubuntu-14.04-64-nocm"
          loadbalancer.vm.network :private_network, ip: "192.168.88.10"

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

        config.vm.provision :ansible_local do |ansible|
          ansible.playbook = "./ansible/playbook.yml"
        end
    end

(1..2).each do |i|
        config.vm.define "application_#{i}" do |application|
          application.vm.box = "puppetlabs/ubuntu-14.04-64-nocm"
          application.vm.network :private_network, ip: "192.168.88.1#{i}"

          application.vm.provider :virtualbox do |v|
            v.name = "application_#{i}"
            v.customize [
                "modifyvm", :id,
                "--name", "application_#{i}",
                "--memory", 512,
                "--natdnshostresolver1", "on",
                "--cpus", 1,
            ]
            end

        config.vm.provision :ansible_local do |ansible|
          ansible.playbook = "./ansible/playbook.yml"
        end
    end
  end
end

As a reminder, ansible is used within the VM images, which means the Vagrant Ansible Local provisioner allows it to provision the guest using Ansible playbooks by executing ansible-playbook directly on the guest machine.

Playbook

Remember to scale up the application server in the playbook if the vagrantfile is also scaled up by a numbers.

---
- hosts: loadbalancer
  become: true
  vars_files:
    - vars/all.yml
  roles:
    - server
    - nginx_proxy

- hosts: application_1
  become: true
  vars_files:
    - vars/all.yml
  roles:
    - server
    - nginx
    - php

- hosts: application_2
  become: true
  vars_files:
    - vars/all.yml
  roles:
    - server
    - nginx
    - php
    - test

Roles

These roles are a further level of abstraction that can be useful for organizing playbooks. As you add more and more functionality and flexibility to your playbooks, they can become difficult to maintain as a single file. Roles allow you to create very minimal playbooks that then look to a directory structure to determine the actual configuration steps they need to perform.

For this project the roles were created (with tasks, handlers and templates) to serve the loadbalancer and the application servers.

  • Server

Includes update_cache to ensures the indexes are synchronized with the sources list and installation curl, wget and vim for test/troubleshooting purposes.

  • Nginx_proxy

Includes installation of Nginx (latest version) and round-robin template. This also include restaring handler for nginx. See more options at Loadbalancing types with Nginx.

  • Nginx

Includes the installation of the latest Nginx and its template for setting up the config with restarting handler for Nginx.

  • Php

Includes the installaton of php php5-fpm, php5-mcrypt, php5-gd and php5-curl and a restarting handler for php5-fpm.

  • Test

Testing connectivity with ports 80 to the loadbalancer and the application servers.

Improvements

The goal is to improve the current design on the following points:

  • Explanatories on every line of code with a hash-mark for understandability
  • Add inventories for replicating deployment environments
  • Add a debug handler with block module
  • Add a report module
  • Add more test module
  • Add tags for organizing and testing purposes

License

MIT License. See the LICENSE file file for details.

van's People

Contributors

akmabrouk avatar coolstoten avatar trhli avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.