Ansible + Vagrant + Expressjs

Source: Internet
Author: User
Tags install node virtual environment

According to one Daniel, Ansible is better than chef and puppet. Learn how to use ansible provisioning a vagrant managed virtual machine to start the EXPRESSJS server. GitHub Link: Https://github.com/kiwiwin/ansible-nodejs-demo


Create a EXPRESSJS project for a stay

$ Express Nodejs-ansible

$ CD Nodejs-ansible

$ NPM Install

Verify that the server can work

$ NPM Start

$ Curl http://localhost:3000

<! DOCTYPE html>


Ok, so we've built a code base for the express server of Hello World.


Create Ansible playbooks

The directory structure is as follows:



In Expressjs-playbook.yml we define two role, one is Nodejs and the other is server. Nodejs This role is mainly used to install some nodejs necessary environment, and server this role is mainly used for checkout source code, start the server and so on.


Nodejs role There is nothing to say, interested can look at the GitHub link given.


Server role is actually very simple. Here we use forever to manage our Nodejs app.

In the provision/server/tasks/server.yml.

1) We first use GIT to checkout our code: By default, Checkout is the head

-Name:checkout Demo Source code
Git:repo=git://github.com/kiwiwin/ansible-nodejs-demo.git
Dest=/nodejs-demo

2) then downgrade the checkout code to the NPM install, which installs the NPM package used under Package.json.

-Name:install node dependencies
Npm:path=/nodejs-demo


3) Use forever to get the running Nodejs app, and put the running list in a variable called RUNNING_APP (variable) for later use

-Name:get running app list with forever
Command:forever List
Register:running_app


4) from the Running_app to determine whether the Nodejs-demo is already running, if it is running, then restart

-Name:restart Server if server is already running
Command:forever restart/nodejs-demo/bin/www
When: "Running_app.stdout.find ('/nodejs-demo/bin/www ')! =-1"


5) Judging from the Running_app whether the Nodejs-demo is already running, if not, start
-Name:start Server
Command:forever start/nodejs-demo/bin/www
When: "Running_app.stdout.find ('/nodejs-demo/bin/www ') = =-1"



Create a Vagrantfile

We already have the code base of Expressjs and the playbooks of Ansible. The next step is to designate ansible to do provisioning in vagrantfile.

1) Initialize Vagrantfile

$ vagrant Init


2) Edit Vagrantfile

#-*-Mode:ruby-*-
# Vi:set Ft=ruby:

# vagrantfile Api/syntax version. Don ' t touch unless you know ' re doing!
Vagrantfile_api_version = "2"


Vagrant.configure (vagrantfile_api_version) do |config|
# every Vagrant virtual environment requires a box to build off of.
Config.vm.box = "Precise64"

# The URL from where the ' config.vm.box ' box would be fetched if it
# doesn ' t already exist on the user ' s system.
Config.vm.box_url = "Http://files.vagrantup.com/precise64.box"

Config.vm.define "Expressjs" do |cfg|
Cfg.vm.host_name = "EXPRESSJS-SERVER.VM"
# Forward Port 5432 on the gust to 5432 on the host
Cfg.vm.network:forwarded_port, guest:3000, host:3333
#The DB server would be is at IP address 192.168.33.33
Cfg.vm.network:p rivate_network, IP: "192.168.33.33"


Cfg.vm.provisioning:ansible do |ansible|
Ansible.playbook = "Provision/expressjs-playbook.yml"
Ansible.verbose = ' V '
End
End
End


Some of the more important points above are:

A) We have 3000 of this port in the virtual machine forward to the 3333 port on this machine, so you can access the localhost:3000 server in the virtual machine via localhost:3333.

b) Specify playbook to provison/expressjs-playbook.yml as provision


3) Verification

$vagrant up

(if prompted to do vagrant provision, then directly input vagrant provision on the line)

$curl http://localhost:3333

<! DOCTYPE html>


The results are exactly the same as the local.



Resources:

Https://servercheck.in/blog/start-nodejs-app-with-forever-and-ansible

Http://cnodejs.org/topic/5021c2cff767cc9a51e684e3


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.