Execution Time for ChatOps commands

November 7, 2017
by Eugen C. aka @armab

Did you know you can do something like this with StackStorm ChatOps?
ChatOps Command Execution time

Looks simple, but it’s a very useful thing to have in your ChatOps toolset, especially for potentially long-running commands.

This small feature was implemented a while ago, but we didn’t make much of a song and dance about it, so you might have missed it.

  • You can use execution.elapsed_seconds Jinja variable in ChatOps Alias template to get the action duration in seconds (123.4422).
  • Additionally, apply Jinja filter to_human_time_from_seconds and make it readable (2m3s)

Putting everything together in a Jinja expression:

{{ execution.elapsed_seconds | to_human_time_from_seconds }}

And here is a ChatOps Alias example with the Slack attachments API in place:

# Example from:
# https://stackstorm.com/2015/06/24/ansible-chatops-get-started-%F0%9F%9A%80/
name: chatops.ansible_package_update
action_ref: st2-chatops-aliases.update_package
description: Update package on remote hosts
  - display: "update <package> on <hosts>"
      - "update {{ package }} on {{ hosts }}"
      - "upgrade {{ package }} on {{ hosts }}"
  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 }}
    {% endif %}
    {{ execution.result.stdout }}
      color: "{% if execution.result.succeeded %}good{% else %}danger{% endif %}"
        - title: Updated nodes
          value: "{{ execution.result.stdout|regex_replace('(?!changed=1).', '')|wordcount }}"
          short: true
        - title: Executed in
          # THIS line
          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"

FYI: Since StackStorm v1.4 ChatOps integrates with the Slack attachments API, allowing you to produce nicely formatted message responses like you see in the screenshot.


If you have some cool ChatOps ideas – we’ll be glad to hear from you via Feature Request or meet us in StackStorm Public Slack.