Skip to content

Instantly share code, notes, and snippets.

@b0bbywan
Created February 22, 2016 15:57
Show Gist options
  • Save b0bbywan/e82bbffe701894415516 to your computer and use it in GitHub Desktop.
Save b0bbywan/e82bbffe701894415516 to your computer and use it in GitHub Desktop.
A hubot script for executing ansible playbooks
# Description:
# A hubot script for launching ansible playbooks
#
# Commands:
# hubot update <environment> [only <tags>] [skip <tags>] [with <key>:<value>] - Execute the ansible playbook on the given environment
#
# Dependencies:
# "node-ansible": "^0.5.2"
#
# Author:
# Mathieu Réquillart
Ansible = require('node-ansible')
settings = {
'path': '/home/ansible/metod-deploy/',
'prod': {
'inventory': 'ec2.py',
'playbook' : 'prod',
'hosts' : 'tag_Name_metod_web_instance'
},
'preprod': {
'inventory': 'ec2.py',
'playbook' : 'preprod',
'hosts' : 'tag_Name_metod_preprod_server'
},
'yourself': {
'inventory': 'ec2.py',
'playbook' : 'inframanager',
'hosts' : 'tag_Name_metod_infra_manager'
},
}
module.exports = (robot) ->
robot.respond /update (prod|preprod|yourself)( only (\w*(,\w*)*))?( skip (\w*(,\w*)*))?( with (\w*:.*(,\w*:.*)*))?/i, (msg) ->
target = msg.match[1]
invfile = "inventory/" + settings[target]['inventory']
playbook = settings[target]['playbook']
cwd = settings['path']
# We use a buffer to comply with slack rate limits
# see https://api.slack.com/docs/rate-limits
# replace regexp to display custom smileys for visual feedback
buffer = []
handleTimeOut = null
bufferInterval = 1000
emptyBuffer = ->
if buffer.length > 0
msg.send buffer.join('\n')
.replace(new RegExp(/ok: /g), ":check: ok: ")
.replace(new RegExp(/failed: /g), ":failed: failed: ")
.replace(new RegExp(/skipping: /g), ":skip: skipping: ")
.replace(new RegExp(/changed: /g), ":changed: changed: ")
.replace(new RegExp(/fatal: /g), ":fatal: fatal: ")
buffer = []
handleTimeOut = setTimeout(emptyBuffer, bufferInterval)
else
handleTimeOut = null
return
if msg.match[2]
tags = msg.match[3].trim().split ","
if msg.match[5]
skip_tags = msg.match[6].trim().split ","
if msg.match[8]
vars = msg.match[9].trim().split ","
varsJsonString = {}
i = 0
while i < vars.length
eltArray = vars[i].trim().split(':')
varsJsonString[eltArray[0]] = eltArray[1]
i++
varsJsonObj = JSON.parse JSON.stringify(varsJsonString)
playbook = (new (Ansible.Playbook)).inventory(invfile).playbook(playbook)
message = "updating: #{target}"
if (tags?)
playbook = playbook.tags(tags)
message = message + " limiting to #{tags}"
if (skip_tags?)
playbook = playbook.skipTags(skip_tags)
message = message + " without #{skip_tags}"
if (vars?)
playbook = playbook.variables(varsJsonObj)
message = message + " replacing #{vars}"
msg.send message
playbook.on 'stdout', (data) ->
buffer.push data.toString()
if handleTimeOut == null
handleTimeOut = setTimeout(emptyBuffer, bufferInterval)
return
playbook.on 'stderr', (data) ->
buffer.push data.toString()
if handleTimeOut == null
handleTimeOut = setTimeout(emptyBuffer, bufferInterval)
return
playbook.exec cwd: cwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment