Sunday 17 July 2016

Web-UI to run Ansible Playbook

Web-Interface to run Ansible Playbook

Ansijet

All credits for Ansijet goes to Ramesh Nair who created Ansijet and Ankit Kulkarni for helping me to modify it.
Ansijet is a node.js server which exposes a simple web API which triggers playbook runs when a request is received. This is especially useful if you are unable to run Ansible playbooks directly from within your continuous integration environment or if you simply wish to trigger playbook runs based on other events within your system (may be on a click of a button).

Features:

  •     Trigger playbook runs from different sources, including from CI systems such as Drone.
  •     Run multiple playbooks in parallel, all in separate processes
  •     Fast, friendly web interface with accompanying REST API
  •     Highly asynchronous, scalable back-end
  •     Full console log capture and storage
  •     Sends notifications of job status through HipChat
The above are its current Feature, We modified it a bit and gave a facility to add a simple trigger endpoint for the playbook and make it run on a click of a button.

Pre-requisites:

  • Ansible 1.5+ - Follow the step here to install Ansible.
  • Node.js 0.11.2+ - Installation instructions: http://nodejs.org/.
  • MongoDB - Follow the step here to install MongoDB.

Setup Ansible Directory:

Ansijet stores its data in MongoDB. The default configuration expects to be able to connect to a MongoDB server running on 127.0.0.1 (i.e. localhost).

Setup your Ansible playbooks

Place your Ansible playbooks somewhere, e.g. /playbooks.

Ansijet expects your playbooks folder to have a certain structure:


<playbooks folder>/*.yml   <- your playbooks
<playbooks folder>/hosts   <- Ansible hosts file

Ensure that any roles needed by your playbooks can be found by the ansible-playbook binary. An easy way to ensure this is to store your roles within the same folder, i.e. at <playbooks folder>/roles/. Same for group_vars and host_vars folders.

Setup Ansijet

The feature to create a trigger and make the playbook run on a click of button is available in feature/deployment branch, so don't forget to check  the branch. Although I have set the default branch to feature/deployment so you don't need to worry.


$ git clone https://github.com/AjeetK/ansijet.git
$ cd ansijet
$ npm install -g gulp bower
$ npm install
$ bower install
$ npm run build

Now create ansijet/src/config/production.js:


"use strict";

module.exports = function(config) {
  /** Path to folder containg Ansible playbooks */
  config.playbooks = '/path/to/playbooks'

  /** Max no. of jobs to execute in parallel. Should match no. of CPU cores. */

  config.jobsInParallel = 1;
};

If you look inside ansijet/src/config/base.js you will see other configuration settings MongoDB, logging, etc. You may override these too within the config/production.js you created.

Run Ansijet

$ cd ansijet
$ NODE_ENV=production ./start-app.js

OR if it doesn't work on ubuntu try the following command


$ cd ansijet
$ NODE_ENV=production /usr/bin/nodejs --harmony ./start-app.js

If you visit http://localhost:3000 you should see the dashboard showing the Active Jobs (there should be none currently).

Creating a Trigger

When you visit the URL http://localhost:3000 you will see the the home-page with all the playbook listed.


Click  on any playbook for which you want to create a trigger. You will see the playbook and the a button to add a trigger.



After clicking on Add trigger button, you will see options to create a trigger. Give the trigger a Name, select the type (i.e. trigger to setup with Dron or Simple trigger to work on a click of a button). I have select "Simple" one.



Click on Next Step and than press Submit button. Now click on Deploy from Nav-bar and you will see a button for the trigger you have just created.


If you click on these button, say test-trigger-1, the corresponding playbook will get triggered and the logs will get stored in MongoDB. The logs can be seen from the Logs section in the nav-bar.



This is how you can use Ansijet to make it easy to run ansible-playbooks.



11 comments:

  1. bower minified#~2014.0.0-beta5.0SELF_SIGNED_CERT_IN_CHAIN Request to https://registry.bower.io/packages/minified failed: self signed certificate in certificate chain

    Iam having this error :bower install --allow-root

    ReplyDelete
    Replies
    1. You can set - Setting NODE_TLS_REJECT_UNAUTHORIZED=0
      Although not a good practice but you can try and get the things up and running

      Delete
    2. Thanks for the update. i am sure not clear with your answer: where do i need to change?
      and one more error i can not find the file path: NODE_ENV=production /usr/bin/nodejs --harmony ./start-app.js.
      The path:usr/bin/nodejs not available

      Delete
    3. Ignore above setting. Create .bowerrc in your project directory with the following content to turn off SSL:
      { "strict-ssl": false, "https-proxy": "" }

      For path issue, find where nodejs is installed. Try running whereis nodejs or which nodejs.
      It might be installed in /usr/local/bin/ or some other dir.

      Delete
    4. Error: connect ECONNREFUSED 127.0.0.1:27017
      at Object.exports._errnoException (util.js:1020:11)
      at exports._exceptionWithHostPort (util.js:1043:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)

      Delete
    5. This means your mongo process is not running. Mongo runs on port 27017 by default. So start your mongo process by sudo service mongod start or which ever upstart job you use. Or may be using the binary file

      Delete
  2. throw err;
    ^

    Error: Cannot find module '/home/ansijet/node_modules/mongoose/node_modules/mongodb/node_modules/bson/ext/start-app.js'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:383:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:496:3

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Try running following command:
      npm cache clean
      rm -rf node_modules
      npm install

      Delete
  3. please look at https://github.com/cloudfirst/PromAnsible.
    it is based on prometheus and ansible playbook to automate IT operations with Web UI

    ReplyDelete
  4. Hi, I have been trying this tutorial. But I have not found path directory for command NODE_ENV=production /use/bin/nodejs --harmony./start-app.js, instead I was running that on the nodejs installation file directory. I don't know what the problem is

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel