17
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AnsibleからSlackに通知する

Posted at

Ansibleを使ってデプロイしたときに、Slackに『デプロイ完了』みたいにログるのが、ちょっと面倒になったので、AnsibleからSlackに通知するようにしてみました。

Ansible Slack Notification

slack - Send Slack notifications — Ansible Documentation

Ansible 1.6以上が必要なので、1.6未満の場合は、Ansibleをバージョンアップします。

AnsibleをVer1.6以上にバージョンアップ

$ ansible --version
ansible 1.7

Slack Incoming WebHooksの追加

下記、URLからチーム内の任意のチャンネルに対するIncoming WebHookを追加します。
Incoming WebHooks

Playbookの準備

デプロイ用のplaybookにSlack通知用の設定を追加します。

ansible/staging
[staging]
2xx.1xx.2xx.x1 ansible_ssh_user=sshuser

[staging:vars]
app_domain=example.com
app_root_path=/apps/example.com
app_user=appuser
app_group=admins

nginx_use_basic=true

### ここから追加
[local]
localhost ansible_connection=local

[local:vars]
app_url=http://example.com
ansible/deploy.yml
- hosts: staging
  sudo: true
  tasks:
    - include: roles/app/tasks/deploy.yml

# ここから追加
- hosts: local
  sudo: false
  tasks:
    - include: roles/app/tasks/notify_complete_deploy.yml
ansible/roles/app/tasks/deploy.yml
# (省略)

Slackに通知する

ansible/roles/app/tasks/notify_complete_deploy.yml
- name: Send notification message via Slack
  local_action:
    module: slack
    domain: yourteam.slack.com
    token: slackapitoken
    msg: "{{ app_url }} deploy completed"
    channel: "#channel"

デプロイ

デプロイ用のplaybookを実行します。

$ ansible-playbook -i ansible/staging ansible/deploy.yml

デプロイが正常に終了すれば、「http://example.com deploy completed」のメッセージがSlackに通知されます。

17
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?