Created
August 30, 2016 19:35
-
-
Save oakinogundeji/98479a7566f5ad8e333fb1b5ba610985 to your computer and use it in GitHub Desktop.
build dynamic ansible inventory from vagrant multi vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
"use strict"; | |
/** | |
* Run as follows: ansible-playbook -i ./vagrant_inventory.js pbook.yml, | |
* NB requires the 'vagrant hostsupdater plugin' | |
*/ | |
//============================================================================= | |
/** | |
* Get list of vagrant hosts | |
*/ | |
//============================================================================= | |
function get_vagrant_hosts() { | |
const | |
fs = require('fs'), | |
HOSTS_FILE_string = fs.readFileSync('/etc/hosts', 'utf8'), | |
HOSTS_FILE_array = HOSTS_FILE_string.split('\n'), | |
vagrant_match = /vagrant/i; | |
let vagrant_hosts = []; | |
HOSTS_FILE_array.forEach(registered_host => { | |
if(registered_host.search(vagrant_match) != -1) { | |
let registered_host_params = registered_host.split(' '); | |
const | |
registered_host_ip = registered_host_params[0], | |
registered_host_name = registered_host_params[2]; | |
vagrant_hosts.push([registered_host_ip, registered_host_name]); | |
} | |
}); | |
return vagrant_hosts; | |
} | |
//============================================================================= | |
/** | |
* Get ip address of running vagrant host | |
*/ | |
function get_ip_address(host, list) { | |
let ip; | |
list.forEach(host_stats => { | |
const | |
NAME = host_stats[1], | |
IP = host_stats[0]; | |
if(NAME == host) { | |
ip = IP; | |
} | |
}); | |
return ip; | |
} | |
//============================================================================= | |
/** | |
* Get list of running vagrant hosts | |
*/ | |
//============================================================================= | |
function main() { | |
const | |
cp = require('child_process'), | |
registered_hosts = get_vagrant_hosts(), | |
running_hosts = cp.execFile('vagrant', ['status', '--machine-readable'], | |
function (err, stdout, stderr) { | |
if(err) { | |
console.log('There was an error executing vagrant'); | |
console.error(err); | |
return process.exit(2); | |
} | |
else { | |
if(stdout) { | |
//============================================================================= | |
/** | |
* Build hosts and groups objects | |
*/ | |
//============================================================================= | |
let | |
hosts = [], | |
groups = {"web": {"hosts": []}, "lb": {"hosts": []}, "db": {"hosts": []}}, | |
main_output = {"infra": {"hosts": hosts}, "_meta": {"hostvars": {}}}, | |
vagrant_hosts_status_list = stdout.split('\n'); | |
vagrant_hosts_status_list.forEach(function (host_status) { | |
const host_status_params = host_status.split(','); | |
let | |
host_name = host_status_params[1], | |
host_status_key = host_status_params[2], | |
host_state = host_status_params[3]; | |
if(host_status_key == 'state' && host_state == 'running') { | |
hosts.push(host_name); | |
let | |
web_match = /^app\d{1,}$/i, | |
lb_match = /^lb\d{1,}$/i, | |
db_match = /^db\d{1,}$/i; | |
if(host_name.search(web_match) != -1) { | |
groups["web"]["hosts"].push(host_name); | |
let web_ip = get_ip_address(host_name, registered_hosts); | |
main_output["_meta"]["hostvars"][host_name] = {"ip": web_ip}; | |
} | |
else if(host_name.search(db_match) != -1) { | |
groups["db"]["hosts"].push(host_name); | |
let db_ip = get_ip_address(host_name, registered_hosts); | |
main_output["_meta"]["hostvars"][host_name] = {"ip": db_ip}; | |
} | |
else if(host_name.search(lb_match) != -1) { | |
groups["lb"]["hosts"].push(host_name); | |
let lb_ip = get_ip_address(host_name, registered_hosts); | |
main_output["_meta"]["hostvars"][host_name] = {"ip": lb_ip}; | |
} | |
} | |
}); | |
for(const grp in groups) { | |
main_output[grp] = groups[grp]; | |
} | |
process.stdout.write(JSON.stringify(main_output, null, 2) + '\n'); | |
process.exit(0); | |
} | |
else if(stderr) { | |
process.stdout.write(JSON.stringify(stderr, null, 2) + '\n'); | |
process.exit(1); | |
} | |
} | |
}); | |
} | |
//============================================================================= | |
if (require.main === module) { | |
main(); | |
} | |
//============================================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment