How to remove http:// from an FQDN when called from Slack ChatOps


(Nick Maludy) #1

When invoking an ChatOps action-alias from Slack with a hostname parameter (and/or FQDN), Slack will automatically prepend https:// or http:// in front of the hostname.

This can cause issues when passing this hostname parameter to things like napalm or ansible where a hostname/FQDN is expected (and NOT a url).

To work around this we need to remove the preceding http[s]:// from the hostname/FQDN parmeter.

Here’s an example action-alias file:

---
name: "storage_info"
action_ref: "encore.storage_info"
pack: "encore"
description: "Retrieves storage info from a VM"
formats:
    - "storage info {{ server }}"
ack:
    format: "Roger that boss, mission is a go!"

And the corresponding action file:

---
description: "Retrieves storage info from a VM"
enabled: true
entry_point: workflows/storage_info.yaml
name: storage_info
pack: encore
parameters:
  server:
    type: string
    description: "Server to retrieve info from"
    required: true
  skip_notify:
    default:
      - "lvm_info"
      - "scsi_info"
runner_type: "mistral-v2"

To get this to work “right” we are able to modify the action metadata file, creating a new parameter that will contain the “cleaned”/“stripped” hostname/FQDN. This “clean” variable can then be used by our action/workflow without the http[s]:// in the front of it.

Here is the new action metadata file:

---
description: "Retrieves storage info from a VM"
enabled: true
entry_point: workflows/storage_info.yaml
name: storage_info
pack: encore
parameters:
  server:
    type: string
    description: "Server to retrieve info from"
    required: true
  server_clean:
    type: string
    immutable: true
    required: true
    default: "{{ server | regex_replace('[A-z]*://', '') }}"
  skip_notify:
    default:
      - "lvm_info"
      - "scsi_info"
runner_type: "mistral-v2"

Alternatively, assuming you’re using the original action metadata file,you can strip out this information using the same regex_replace technique within a workflow:

version: '2.0'

encore.storage_info:
  type: direct
  input:
    - server
    - run: null

  output:
    run: "{{ _.run }}"

  tasks:
    ipa_cache_clear:
      # Slack/ChatOps converts hostname.domain.tld into a URL: http://hostname.domain.tld
      # we need to reverse this and convert the URL back into a FQDN
      action: ansible.playbook
      input:
        playbook: /etc/ansible/playbooks/storage_info.yaml
        inventory_file: "{{ regex_replace(_.server, '[A-z]+://', '') }},"
      publish:
        run: "{{ task('ipa_cache_clear').result }}"