Last active
April 29, 2019 06:58
-
-
Save AgentO3/574b103d205d7a70c167 to your computer and use it in GitHub Desktop.
Hubot Ansible Script and Ansible Ops Playbooks
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
# Description: | |
# An attempt to expose Ansible commands through hubot | |
# | |
# Dependencies: | |
# "underscore": "~1.5.2" | |
# "js-yaml": "^3.2.7" | |
# | |
# | |
# Configuration: | |
# None | |
# | |
# Commands: | |
# hubot ops help | |
# | |
# Author: | |
# AgentO3 | |
exec = require('child_process').exec | |
_ = require('underscore') | |
yaml = require('js-yaml') | |
fs = require('fs') | |
ansiblePath = process.env.HUBOT_ANSIBLE_PATH | |
hubotUser = process.env.HUBOT_ANSIBLE_USER | |
module.exports = (robot) -> | |
robot.respond /ops (\S+)$/i, (msg) -> | |
processMsg(robot, msg) | |
robot.respond /ops (\S+) (.+)$/i, (msg) -> | |
processMsg(robot, msg) | |
processMsg = (robot, msg) -> | |
cmd = msg.match[1] | |
ops = msg.match[2] || "" | |
user_id = msg.envelope.user.id | |
dc = robot.brain.get("#{user_id}-dc") | |
switch cmd | |
when "help" then helpDocs(msg) | |
when "use" then processDc(robot, ops, user_id, msg) | |
when "update" then updateAnsible(msg) | |
when "branch" then checkOutBranch(msg) | |
else processAnsibleCmd(msg, cmd, ops, dc) | |
updateAnsible = (msg) -> | |
exec "cd #{ansiblePath}; git pull origin master", (error, stdout, stderr) -> | |
msg.send stderr | |
msg.send stdout | |
checkOutBranch = (msg) -> | |
branch = msg.match[2] | |
exec "cd #{ansiblePath}; git checkout #{branch}", (error, stdout, stderr) -> | |
msg.send stderr | |
msg.send stdout | |
helpDocs = (msg) -> | |
playbooks = yaml.safeLoad(fs.readFileSync("#{ansiblePath}/path/to/your/playbook.yml", 'utf8')) | |
docs = [] | |
for play in playbooks | |
parameters = [] | |
for k,v of play.vars | |
if k != "docs" | |
parameters.push """#{k} default: #{v}""" | |
docs.push """ | |
Command: #{play.tags[0]} | |
Description: #{play.vars.docs} | |
Hosts: #{play.hosts} | |
Parameters: | |
#{parameters.join("\n")} | |
""" | |
msg.send docs.join("\n") | |
processDc = (robot, ops, user_id, msg) -> | |
if ops != "" | |
robot.brain.set "#{user_id}-dc", ops | |
msg.send "You are now in #{ops}." | |
else | |
dc = robot.brain.get("#{user_id}-dc") | |
msg.send "You are in #{dc}." | |
processAnsibleCmd = (msg, cmd, ops, dc) -> | |
userFlag = "" | |
if hubotUser != undefined | |
userFlag = "-u #{hubotUser}" | |
if ops.indexOf("test=true") == -1 | |
ansibleCmd = "ansible-playbook -i #{dc} ops.yml -t #{cmd} -e '#{ops}' -D --vault-password-file=.password.txt #{userFlag}" | |
else | |
ansibleCmd = "ansible-playbook -i #{dc} ops.yml -t #{cmd} -e '#{ops}' -D -C --vault-password-file=.password.txt #{userFlag}" | |
msg.send """ | |
Running command | |
#{ansibleCmd} | |
""" | |
exec "cd #{ansiblePath}; #{ansibleCmd}", (error, stdout, stderr) -> | |
if stdout.length > 0 | |
for chunk in chunker(stdout) | |
msg.send chunk.join('\n') | |
sleep(1500) | |
if stderr.length > 0 | |
for chunk in chunker(stderr) | |
msg.send chunk.join('\n') | |
sleep(1500) | |
chunker = (input) -> | |
lines = input.split('\n') | |
chunks = [] | |
while lines.length > 0 | |
chunks.push lines.splice(0, 250) | |
chunks | |
# Attempt stagger the message so they appear in HipChat in the correct order | |
# however this doesn't work in HipChat but in using the cli adaptor it appears to | |
# behave as expected. Not sure why it doesn't do the same in HipChat. | |
sleep = (ms) -> | |
start = new Date().getTime() | |
continue while new Date().getTime() - start < ms |
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
--- | |
- hosts: "{{h}}" | |
tags: ["sf-app-log"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get logs from sf-app" | |
q: "{{env}}" | |
p: /var/log/sf-app/{{q}} | |
n: 20 | |
g: "" | |
tasks: | |
- shell: tail -n {{n}} {{p}}.log {% if g != "" %}| grep {{g}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["sf-app-conf"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get the sf-app configuration" | |
p: /var/www/sf-app/current/app/config/parameters.yml | |
tasks: | |
- shell: cat {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["utils-conf"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get the utils configuration files" | |
q: "*" | |
p: /etc/vividcortex | |
tasks: | |
- shell: cat {{p}}/{{q}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["crontab"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "List all files in cron.d" | |
p: /etc/cron.d | |
tasks: | |
- shell: ls {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["crontab-rm"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Remove file from cron.d" | |
q: "*" | |
p: /etc/cron.d | |
tasks: | |
- shell: rm -rf {{p}}/{{q}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["jenkins-state"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Change jenkins service state" | |
s: restarted | |
tasks: | |
- service: name=jenkins state={{s}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["mysql-show-status"] | |
gather_facts: no | |
sudo: yes | |
sudo_user: ansible | |
vars: | |
docs: "Run show status sql query" | |
tasks: | |
- shell: mysql -e "SHOW ENGINE INNODB STATUS\G" | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["mysql-conf"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get mysql config" | |
p: /etc/my.cnf | |
tasks: | |
- shell: cat {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["mysql-log"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get mysql logs" | |
p: /var/log/mysql/mysqld.log | |
n: 20 | |
g: "" | |
tasks: | |
- shell: tail -n {{n}} {{p}} {% if g != "" %}| grep {{g}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["mysql-backup"] | |
gather_facts: yes | |
sudo: yes | |
vars: | |
docs: "Run mysql backup." | |
tasks: | |
- file: path=/bku state=directory | |
- shell: mysqldump --all-databases > backup-{{ansible_date_time.epoch}}.sql chdir=/bku | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["mysql-backup-ls"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "List previous mysql backups" | |
p: /bku | |
tasks: | |
- shell: ls {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["mysql-backup-rm"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Remove a previous mysql backup" | |
p: /bku | |
q: "*" | |
tasks: | |
- shell: rm -rf {{p}}/{{q}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["consul-conf"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get consul config files" | |
q: "*" | |
p: /home/ansible/consul | |
tasks: | |
- shell: ls {{p}}/{{q}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["consul-conf-rm"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Remove a consul config file" | |
q: "*" | |
p: /home/ansible/consul/conf | |
tasks: | |
- shell: rm -rf {{p}}/{{q}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["nginx-log"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get nginx access log" | |
p: /var/log/nginx/access.log | |
n: 20 | |
g: "" | |
tasks: | |
- shell: tail -n {{n}} {{p}} {% if g != "" %}| grep {{g}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["nginx-config"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get nginx config" | |
p: /etc/nginx/conf.d/proxy.com.conf | |
tasks: | |
- shell: cat {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["nginx-state"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Set nginx service state" | |
s: restarted | |
tasks: | |
- service: name=nginx state={{s}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["haproxy-log"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get haproxy log" | |
p: /var/log/haproxy_1.log | |
n: 20 | |
g: "" | |
tasks: | |
- shell: tail -n {{n}} {{p}} {% if g != "" %}| grep {{g}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["haproxy-config"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get haproxy config" | |
p: /etc/haproxy/haproxy.cfg | |
tasks: | |
- shell: cat {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["haproxy-state"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Set haproxy service state" | |
s: restarted | |
tasks: | |
- service: name=haproxy state={{s}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "app-servers" | |
tags: ["httpd-log"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get httpd logs" | |
q: "*" | |
p: /var/log/httpd/{{q}}* | |
n: 20 | |
g: "" | |
tasks: | |
- shell: tail -n {{n}} {{p}} {% if g != "" %}| grep {{g}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "app-servers" | |
tags: ["httpd-log-list"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "List httpd logs" | |
p: /var/log/httpd | |
tasks: | |
- shell: ls {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["httpd-state"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Set httpd service state" | |
s: restarted | |
tasks: | |
- service: name=httpd state={{s}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "app-servers" | |
tags: ["httpd-vhost-list"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "List httpd vhosts" | |
p: /etc/httpd/conf.d | |
tasks: | |
- shell: ls {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "app-servers" | |
tags: ["httpd-vhost"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get httpd vhost config" | |
q: "*" | |
p: /etc/httpd/conf.d/{{q}}* | |
tasks: | |
- shell: cat {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["agent-log"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get agent logs" | |
q: "*" | |
p: /var/log/vividcortex/{{q}}.log | |
n: 20 | |
g: "" | |
tasks: | |
- shell: tail -n {{n}} {{p}} {% if g != "" %}| grep {{g}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["agent-config"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get agent config" | |
q: "*" | |
p: /etc/vividcortex/global.conf | |
tasks: | |
- shell: cat {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["agent-restart"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Restart agents" | |
p: /etc/init.d/vividcortex | |
tasks: | |
- shell: /etc/init.d/vividcortex restart | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["runit"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "List runit services" | |
p: /home/ansible/service | |
tasks: | |
- shell: ls {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["runit-rm"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Remove runit service" | |
q: null | |
p: /home/ansible/service/{{q}} | |
tasks: | |
- shell: rm -rf {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["runit-config"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get runit configuration" | |
q: "*" | |
p: /home/ansible/service/{{q}}/env/* | |
tasks: | |
- shell: tail {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["runit-log"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Get runit logs" | |
q: "*" | |
g: "" | |
p: /home/ansible/service/{{q}}/log/main/current | |
n: 20 | |
tasks: | |
- set_fact: | |
- shell: tail -n {{n}} {{p}} {% if g != "" %}| grep {{g}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["runit-restart"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Restart runit service" | |
q: "*" | |
p: /home/ansible/service/{{q}} | |
tasks: | |
- shell: sv restart {{p}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["os-ps"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "List running process on server" | |
q: "" | |
tasks: | |
- shell: ps aux {% if q != "" %}| grep {{q}}{% endif %} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["os-kill"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Kill running process on server" | |
i: "" | |
tasks: | |
- shell: kill {{i}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "health-check" | |
tags: ["kafka-sync"] | |
sudo: true | |
sudo_user: ansible | |
gather_facts: no | |
vars: | |
docs: "Run kafka-check sync" | |
p: /home/ansible/kafka-check/current | |
tasks: | |
- shell: "{{p}}/kafka-check sync -brokers={% for ser in groups['kafka-servers']%}{{ser}}:9092,{% endfor %}" | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "localhost" | |
tags: ["groups"] | |
gather_facts: no | |
vars: | |
docs: "List server groups in environment" | |
tasks: | |
- debug: var=groups | |
- hosts: "localhost" | |
tags: ["lookup"] | |
gather_facts: no | |
vars: | |
docs: "Lookup domain ip address" | |
q: "" | |
tasks: | |
- command: nslookup {{q}} | |
register: r | |
- debug: var=r.stdout_lines | |
- hosts: "{{h}}" | |
tags: ["facts"] | |
gather_facts: yes | |
vars: | |
docs: "Get server facts" | |
tasks: | |
- debug: var=hostvars[inventory_hostname] | |
- hosts: "build-server" | |
tags: ["build-clean"] | |
gather_facts: no | |
sudo: yes | |
vars: | |
docs: "Clean go project dependencies folder" | |
q: "" | |
p: /tmp/go_projects | |
tasks: | |
- fail: msg="You must specify a project to clean using q=<project_name>" | |
when: q == "" | |
- shell: rm -rf {{p}}/{{q}} | |
register: r | |
- debug: var=r.stdout_lines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment