4
2

More than 5 years have passed since last update.

この記事は、Ansible Blogger Advent Calendar 2018 の4日目の記事です。

いつも使っているslack moduleを使いやすくするtips

サンプル

以下のレポジトリにサンプルを載せています。

ラップして使う

slack module に限らず、複数のパラメータが用意されています。
よく使うmoduleにはラッパーを用意して、使ったりするとプレイブックが少しだけ短く出来ます。

e.g.) library/slack/tasks/main.yml

library ディレクトリに各パラメータを書き、| default('') 記法でデフォルト値を用意してきます。

---

- name: Send notification message via Slack
  local_action:
    module: slack
    channel: "{{ channel | default('#ansible-notification') }}"
    color: "{{ color | default('normal') }}"
    icon_emoji: "{{ icon_emoji | default(':sunglasses:') }}"
    link_names: "{{ link_names | default(1) }}"
    msg: "{{ msg | default('message from ansible') }}"
    parse: "{{ parse | default('none') }}"
    token: "{{ lookup('env', 'SLACK_TOKEN') }}"
    username: "{{ username | default('Ansible') }}"

実行したい場合は以下のようにincludeして、デフォルトではなく変更したいパラメータをvarsで渡してあげます。(変数指定しなかったパラメータには、上記のデフォルト値が使われます。)

  tasks:
    - include: ./../library/slack/tasks/main.yml
      run_once: true
      vars:
        - channel: "#ansible-example"
        - color: "good"
        - msg: "hello"

ここからは、slack moduleに限らずのものです。

token の管理

クレデンシャルの管理には、ansible-vaultを使うのがてっとり早いかと思います。
上の例では、lookup('env', 'SLACK_TOKEN') で環境変数から取得しています。
(lookup('file', '{{ playbook_dir }}/.secret/slack_token')として、staticなファイルから読み込むことも可能です。)

一度だけ実行

複数ホストへのタスク実行時にも通知は1回にしたい場合
上の例ではタスクをrun_once: trueとして、1度だけ実行されるようにしています。
その他にも、delegate_to: localhostでansibleコマンドを実行元のホスト上でだけ実行するのでも同様です。

ドキュメントをみる

パラメータや例を確認したい場合、ansible-doc コマンドが便利です。
ブラウザを開かずに確認できます。

$ ansible-doc slack
# スニペットが見たい場合
$ ansible-doc -s slack
- name: Send Slack notifications
  slack:
      attachments:           # Define a list of attachments. This list mirrors the Slack JSON API. For more information, see also
                               in the (https://api.slack.com/docs/attachments).
      channel:               # Channel to send the message to. If absent, the message goes to the channel selected for the
                               `token'.
      color:                 # Allow text to use default colors - use the default of 'normal' to not send a custom color bar at
                               the start of the message
      icon_emoji:            # Emoji for the message sender. See Slack documentation for options. (if `icon_emoji' is set,
                               `icon_url' will not be used)
      icon_url:              # Url for the message sender's icon (default `https://www.ansible.com/favicon.ico')
      link_names:            # Automatically create links for channels and usernames in `msg'.
      msg:                   # Message to send. Note that the module does not handle escaping characters. Plain-text angle
                               brackets and ampersands should be converted to HTML entities (e.g.
                               & to &) before sending. See Slack's documentation
                               (https://api.slack.com/docs/message-formatting) for more.
      parse:                 # Setting for the message parser at Slack
      token:                 # (required) Slack integration token.  This authenticates you to the slack service. Prior to 1.8, a
                               token looked like `3Ffe373sfhRE6y42Fg3rvf4GlK'.  In 1.8 and above,
                               ansible adapts to the new slack API where tokens look like
                               `G922VJP24/D921DW937/3Ffe373sfhRE6y42Fg3rvf4GlK'.  If tokens are in
                               the new format then slack will ignore any value of domain.  If the
                               token is in the old format the domain is required.  Ansible has no
                               control of when slack will get rid of the old API.  When slack does
                               that the old format will stop working.  ** Please keep in mind the
                               tokens are not the API tokens but are the webhook tokens.  In slack
                               these are found in the webhook URL which are obtained under the
                               apps and integrations. The incoming webhooks can be added in that
                               area.  In some cases this may be locked by your Slack admin and you
                               must request access.  It is there that the incoming webhooks can be
                               added.  The key is on the end of the URL given to you in that
                               section.
      username:              # This is the sender of the message.
      validate_certs:        # If `no', SSL certificates will not be validated. This should only be used on personally controlled
                               sites using self-signed certificates.

終わり

slack module 限定ではなく、他のmoduleをlibraryしておくと便利だったり。
また ansible-doc でのパラメータ確認は、ansible使いには嬉しい機能なので抑えておきたいですね!

4
2
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
4
2