Skip to content

Instantly share code, notes, and snippets.

@armab

armab/0.vagrant.sh Secret

Last active Feb 21, 2017
Embed
What would you like to do?
DO-NOT-DELETE! Embed gist for StackStorm blog: "Ansible and ChatOps. Get Started!"
# replace with your token
export HUBOT_SLACK_TOKEN=xoxb-5187818172-I7wLh4oqzhAScwXZtPcHyxCu
git clone https://github.com/StackStorm/showcase-ansible-chatops.git
cd showcase-ansible-chatops
vagrant up
curl -sSL https://stackstorm.com/packages/install.sh | sudo bash -- --user=demo --password=demo
st2 pack install ansible
# Bot name
export HUBOT_NAME=stanley
export HUBOT_ALIAS='!'
# StackStorm API key
# Use: `st2 apikey create -k` to generate
# Replace with your key (!)
export ST2_API_KEY="123randomstring789"
# ST2 AUTH credentials
# Replace with your username/password (!)
export ST2_AUTH_USERNAME="demo"
export ST2_AUTH_PASSWORD="demo"
# Configure Hubot to use Slack
export HUBOT_ADAPTER="slack"
# Replace with your token (!)
export HUBOT_SLACK_TOKEN="xoxb-5187818172-I7wLh4oqzhAScwXZtPcHyxCu"
sudo service st2chatops restart
/invite @stanley
---
name: "chatops.ansible_local"
action_ref: "ansible.command_local"
description: "Run Ansible command on local machine"
formats:
- display: "ansible <command>"
representation:
- "ansible {{ args }}"
result:
format: |
Ansible command `{{ execution.parameters.args }}` result: {~}
{% if execution.result.stderr %}*Stdout:* {% endif %}
```{{ execution.result.stdout }}```
{% if execution.result.stderr %}*Stderr:* ```{{ execution.result.stderr }}```{% endif %}
extra:
slack:
color: "{% if execution.result.succeeded %}good{% else %}danger{% endif %}"
!ansible "uname -a"
/opt/stackstorm/virtualenvs/ansible/bin/ansible all --connection=local --args='uname -a' --inventory-file='127.0.0.1,'
!pack install https://github.com/armab/st2_chatops_aliases
---
name: server_status
description: Show server status by running ansible ping ad-hoc command
runner_type: local-shell-cmd
entry_point: ""
enabled: true
parameters:
sudo:
description: "Run command with sudo"
type: boolean
immutable: true
default: true
kwarg_op:
immutable: true
cmd:
description: "Command to run"
type: string
immutable: true
default: "/opt/stackstorm/virtualenvs/ansible/bin/ansible {{hosts}} --module-name=ping"
hosts:
description: "Ansible hosts to ping"
type: string
required: true
---
name: chatops.ansible_server_status
action_ref: st2_chatops_aliases.server_status
description: Show status for hosts (ansible ping module)
formats:
- display: "status <hosts>"
representation:
- "status {{ hosts }}"
- "ping {{ hosts }}"
result:
format: |
Here is your status for `{{ execution.parameters.hosts }}` host(s): {~}
```{{ execution.result.stdout }}```
extra:
slack:
color: "{% if execution.result.succeeded %}good{% else %}danger{% endif %}"
fields:
- title: Alive
value: "{{ execution.result.stdout|regex_replace('(?!SUCCESS).', '')|wordcount }}"
short: true
- title: Dead
value: "{{ execution.result.stdout|regex_replace('(?!UNREACHABLE).', '')|wordcount }}"
short: true
footer: "{{ execution.id }}"
footer_icon: "https://stackstorm.com/wp/wp-content/uploads/2015/01/favicon.png"
!pack install https://github.com/armab/st2_chatops_aliases
!status 'web'
---
name: service_restart
description: Restart service on remote hosts
runner_type: local-shell-cmd
entry_point: ""
enabled: true
parameters:
sudo:
description: "Run command with sudo"
type: boolean
immutable: true
default: true
kwarg_op:
immutable: true
cmd:
description: "Command to run"
type: string
immutable: true
default: "/opt/stackstorm/virtualenvs/ansible/bin/ansible {{hosts}} --become --module-name=service --args='name={{service_name}} state=restarted'"
hosts:
description: "Ansible hosts"
type: string
required: true
service_name:
description: "Service to restart"
type: string
required: true
---
name: chatops.ansible_service_restart
action_ref: st2_chatops_aliases.service_restart
description: Restart service on remote hosts
formats:
- display: "service restart <service_name> on <hosts>"
representation:
- "service restart {{ service_name }} on {{ hosts }}"
result:
format: |
Service restart `{{ execution.parameters.service_name }}` on `{{ execution.parameters.hosts }}` host(s): {~}
{% if execution.result.stderr %}
*Exit Status*: `{{ execution.result.return_code }}`
*Stderr:* ```{{ execution.result.stderr }}```
*Stdout:*
{% endif %}
```{{ execution.result.stdout }}```
extra:
slack:
color: "{% if execution.result.succeeded %}good{% else %}danger{% endif %}"
fields:
- title: Restarted
value: "{{ execution.result.stdout|regex_replace('(?!SUCCESS).', '')|wordcount }}"
short: true
- title: Failed
value: "{{ execution.result.stdout|regex_replace('(?!(FAILED|UNREACHABLE)!).', '')|wordcount }}"
short: true
footer: "{{ execution.id }}"
footer_icon: "https://stackstorm.com/wp/wp-content/uploads/2015/01/favicon.png"
!service restart "rabbitmq-server" on "mq"
!show mysql processlist
!show mysql processlist 'db'
---
name: mysql_processlist
description: Show MySQL processlist
runner_type: local-shell-cmd
entry_point: ""
enabled: true
parameters:
sudo:
immutable: true
default: true
kwarg_op:
immutable: true
cmd:
description: "Command to run"
type: string
immutable: true
default: "/opt/stackstorm/virtualenvs/ansible/bin/ansible {{ hosts }} --become --become-user=root -m shell -a \"mysql --execute='SHOW PROCESSLIST;' | expand -t 10\""
hosts:
description: "Ansible hosts"
type: string
default: db
---
name: chatops.mysql_processlist
action_ref: st2_chatops_aliases.mysql_processlist
description: Show MySQL processlist
formats:
- display: "show mysql processlist <hosts=db>"
representation:
- "show mysql processlist {{ hosts=db }}"
- "show mysql processlist on {{ hosts=db }}"
result:
format: |
{% if execution.status == 'succeeded' %}MySQL queries on `{{ execution.parameters.hosts }}`: ```{{ execution.result.stdout }}```{~}{% else %}
*Exit Code:* `{{ execution.result.return_code }}`
*Stderr:* ```{{ execution.result.stderr }}```
*Stdout:* ```{{ execution.result.stdout }}```
{% endif %}
!show mysql processlist
---
name: http_status_codes
description: Show sorted http status codes from nginx logs
runner_type: local-shell-cmd
entry_point: ""
enabled: true
parameters:
sudo:
immutable: true
default: true
kwarg_op:
immutable: true
cmd:
description: "Command to run"
type: string
immutable: true
default: "/opt/stackstorm/virtualenvs/ansible/bin/ansible {{hosts|replace('http://','')}} --become -m shell -a \"awk '{print \\$9}' /var/log/nginx/access.log|sort |uniq -c |sort -k1,1nr 2>/dev/null|column -t\""
hosts:
description: "Ansible hosts"
type: string
required: true
---
name: chatops.http_status_codes
action_ref: st2_chatops_aliases.http_status_codes
description: Show sorted http status codes from nginx on hosts
formats:
- display: "show nginx stats on <hosts>"
representation:
- "show nginx stats on {{ hosts }}"
result:
format: "```{{ execution.result.stdout }}```"
!show nginx stats on 'web'
---
name: update_package
description: Update package on remote hosts
runner_type: local-shell-cmd
entry_point: ""
enabled: true
parameters:
sudo:
immutable: true
default: true
kwarg_op:
immutable: true
timeout:
default: 6000
cmd:
description: "Command to run"
immutable: true
default: "/opt/stackstorm/virtualenvs/ansible/bin/ansible-playbook /opt/stackstorm/packs/${ST2_ACTION_PACK_NAME}/playbooks/update_package.yaml --extra-vars='hosts={{hosts|replace('http://','')}} package={{package}}'"
hosts:
description: "Ansible hosts"
type: string
required: true
package:
description: "Package to upgrade"
type: string
required: true
---
name: chatops.ansible_package_update
action_ref: st2_chatops_aliases.update_package
description: Update package on remote hosts
formats:
- display: "update <package> on <hosts>"
representation:
- "update {{ package }} on {{ hosts }}"
- "upgrade {{ package }} on {{ hosts }}"
result:
format: |
Update package `{{ execution.parameters.package }}` on `{{ execution.parameters.hosts }}` host(s): {~}
{% if execution.result.stderr %}
*Exit Status*: `{{ execution.result.return_code }}`
*Stderr:* ```{{ execution.result.stderr }}```
*Stdout:*
{% endif %}
```{{ execution.result.stdout }}```
extra:
slack:
color: "{% if execution.result.succeeded %}good{% else %}danger{% endif %}"
fields:
- title: Updated nodes
value: "{{ execution.result.stdout|regex_replace('(?!changed=1).', '')|wordcount }}"
short: true
- title: Executed in
value: ":timer_clock: {{ execution.elapsed_seconds | to_human_time_from_seconds }}"
short: true
footer: "{{ execution.id }}"
footer_icon: "https://stackstorm.com/wp/wp-content/uploads/2015/01/favicon.png"
!update 'bash' on 'all'
---
- name: Update package on remote hosts, run on 25% of servers at a time
hosts: "{{ hosts }}"
serial: "25%"
become: True
become_user: root
tasks:
- name: Check if Package is installed
command: dpkg-query -l {{ package }}
register: is_installed
failed_when: is_installed.rc > 1
changed_when: no
- name: Update Package only if installed
apt: name={{ package }}
state=latest
update_cache=yes
cache_valid_time=600
when: is_installed.rc == 0
---
name: cowsay
description: Draws a cow that says what you want
runner_type: local-shell-cmd
entry_point: ""
enabled: true
parameters:
sudo:
immutable: true
kwarg_op:
immutable: true
cmd:
description: "Command to run"
type: string
immutable: true
default: "/usr/games/cowsay {{message}}"
message:
description: "Message to say"
type: string
required: true
---
name: chatops.cowsay
action_ref: st2_chatops_aliases.cowsay
description: Draws a cow that says what you want
formats:
- display: "cowsay <message>"
representation:
- "cowsay {{ message }}"
ack:
enabled: false
result:
format: |
{% if execution.status == 'succeeded' %}Here is your cow: ```{{ execution.result.stdout }}``` {~}{% else %}
Sorry, no cows this time {~}
Exit Code: `{{ execution.result.return_code }}`
Stderr: ```{{ execution.result.stderr }}```
Hint: Make sure `cowsay` utility is installed.
{% endif %}
!cowsay 'Holy ChatOps Cow!'
sudo apt-get install cowsay
HUBOT_SLACK_TOKEN=xoxb-5187818172-I7wLh4oqzhAScwXZtPcHyxCu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.