DEV Community

Nedim Hadzimahmutovic
Nedim Hadzimahmutovic

Posted on

Monit to Slack notifications with Ansible

This post is a quick one where I share my Ansible playbook which installs Monit on the server, add space usage alerts with Slack notifications.

---
- hosts: all
  gather_facts: yes
  environment:
    PATH: /usr/local/rbenv/shims:{{ ansible_env.PATH }}
  roles:
  - role: zzet.rbenv
    tags: ['rbenv']
    rbenv_clean_up: false
    rbenv:
      env: system
      version: v1.2.0
      default_ruby: 3.0.4
      rubies:
        - version: 3.0.4
  - role: pgolm.monit
    tags: ['monit']
    vars:
      slack_webhook: "'https://hooks.slack.com/services/xxxx/xxxx/xxxx'"
      monit2slack_path: "/usr/local/rbenv/shims/monit2slack"
      monit_cycle: 120
      monit_webinterface_enabled: false
      monit_services:
      - name: disk_usage
        type: filesystem
        target: /
        rules:
          - 'if space usage > 90%
          then exec "{{monit2slack_path}} --webhook {{slack_webhook}} --status error"
          else if succeeded then exec "{{monit2slack_path}} --webhook {{slack_webhook}} --status ok"'
          - 'if inode usage > 90%
          then exec "{{monit2slack_path}} --webhook {{slack_webhook}} --status error"
          else if succeeded then exec "{{monit2slack_path}} --webhook {{slack_webhook}} --status ok"'
  tasks:
    - name: Update system gem
      command: gem update --system
      become: yes
      become_method: su
      become_user: root
      environment:
        PATH: "/usr/local/rbenv/bin:/usr/local/rbenv/shims:{{ ansible_env.PATH }}"

    - name: Install latest version of monit2slack
      gem:
        name: monit2slack
        state: latest
      environment:
        PATH: "/usr/local/rbenv/bin:/usr/local/rbenv/shims:{{ ansible_env.PATH }}"

    - name: Install monit2slack gem
      command: gem install monit2slack
      become: yes
      become_method: su
      become_user: root
      environment:
        PATH: "/usr/local/rbenv/bin:/usr/local/rbenv/shims:{{ ansible_env.PATH }}"

Enter fullscreen mode Exit fullscreen mode


`

Top comments (0)