Ansible 發(fā)送 notification 到 HipChat

2018-07-25 10:49 更新

HipChat 是相當(dāng)老牌線上通訊服務(wù),官方也提供了不少整合方案。凍仁曾通過它整合 Ansible, Git, Jenkins, Redmine, Skype  Zabbix 1 等服務(wù),當(dāng)資訊集中在一處后,維運起來也會輕松許多。這里凍仁將介紹 Ansible 整合 HipChat 的部份。2

automate_with_ansible_practice-22.jpg

▲ 上圖為 Ansible 發(fā)送 notification 到 HipChat 的范例。

hipchat module 是什么?

hipchat module 是用來發(fā)送信息到 HipChat 上的通知模組 (notification modules),我們只需取得 Room notification Token 即可使用。

Ansible 怎么整合 HipChat?

  1. 撰寫發(fā)送 notification 到 HipChat 的 Playbook:請依步驟 2 取得的參數(shù)設(shè)定 hipchat_token 和 hipchat_room 等變數(shù)。

     $ vi send_notification_to_hipchat.yml
     ---
     - name: Send notification to HipChat
       hosts: localhost
    
       vars:
         hipchat_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
         hipchat_room: "automate-with-ansible"
    
       post_tasks:
         - name: send notification to hipchat
           hipchat:
             api: "https://api.hipchat.com/v2/"
             token: ""
             room: ""
             color: gray
             msg: "Send a notification with Ansible !"
    
     # vim: ft=ansible :
    

    由于 HipChat 在設(shè)計是綁定房間 (Room) 使用的,不像 Slack 可用一組設(shè)定發(fā)送訊息到不同的頻道,若有這類需求則可通過多個 Tasks 和 Tokens 進(jìn)行實作。

  2. 取得 HipChat 的 Room Notification Token。

    1. 登入 HipChat 后,開啟 web app。

      2016-12-17-ansible-hipchat-1.png

    2. 建立一間新的 Room。

      2016-12-17-ansible-hipchat-2.png

    3. 幫這個 Room 取個名字并簡單介紹一下用途。

      2016-12-17-ansible-hipchat-3.png

    4. 回到 HipChat 的管理界面,并找到 Group Admin (群組管理) 。

      2016-12-17-ansible-hipchat-4.png

    5. 在 Rooms 底下找到要整合 Ansible 的 Room。

      2016-12-17-ansible-hipchat-5.png

    6. 點選左側(cè)的 Tokens。

      2016-12-17-ansible-hipchat-6.png

    7. 在 Create New Token 輸入 Ansible 和選擇 Send Notification 的 Scopes,之后接下 Create。

      2016-12-17-ansible-hipchat-7.png

    8. Token 建立完畢后,即可在紅框里取得剛建立的 Token

      2016-12-17-ansible-hipchat-8.png

  3. 執(zhí)行 Playbook。

     $ ansible-playbook send_notification_to_hipchat.yml
    
     PLAY [Send notification to HipChat] ********************************
    
     TASK [setup] *******************************************************
     ok: [localhost]
    
     TASK [send notification to hipchat] ********************************
     changed: [localhost]
    
     PLAY RECAP *********************************************************
     localhost      : ok=2    changed=1    unreachable=0    failed=0
    
  4. 在 HipChat 接收從 Ansible 送出的 notification。

    2016-12-17-ansible-hipchat-9.png

  5. 現(xiàn)在我們可以在 HipChat 里接收來自 Ansible 的 notification 了!

后話

Ansible 與 HipChat 的組合在使用上會比 Slack 簡單許多,雖然 HipChat 不比 Slack 的高度客制化,但凍仁認(rèn)為它的門檻比較低,適合需求較單純的團(tuán)隊。

至于要選哪一家作為 ChatOps 的平臺?這就是另一門學(xué)問了。(笑)

相關(guān)連結(jié)

1. Zabbix 是企業(yè)級的 open source 監(jiān)控系統(tǒng),搭配 HipChat 使用后可以讓我們在極短的時間內(nèi)掌握服務(wù)器 (Server) 和網(wǎng)絡(luò)設(shè)備等狀況。 ?
2. 凍仁先前于高雄任職的公司有使用 HipChat,但后期只剩 IT 維運部門在用就是。 ?


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號