5
1

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.

Prometheusの設定変更・チェック・ReloadまでAnsibleで一発でやる

Posted at

Prometheusの設定変更、Alertルールの変更した際にそれ専用のPlaybookを実行するとか面倒すぎる
同じplaybookで一発で反映・チェック・Reloadまでできるようにしてみた

まず、systemdのprometheus.serviceでReloadできるようにしておく

[Unit]
Description=Prometheus - Monitoring system and time series database
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
After=network-online.target

[Service]
User=prometheus
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target

Ansibleは

  • validateでpromtoolを使ってcheckする
  • changedならnotifyでreloadを実行
  • tagを追加して単発実行できるようにしておく
  • handlerにreloadを作成

と修正

task
- name: rule files of Prometheus
  copy:
    src:  "{{ rule }}"
    dest: "{{ root_path }}/prometheus/rules.yml"
    validate: "{{ root_path }}/prometheus/promtool check rules %s"
  notify: Reload Prometheus
  with_items:
    - files/rules.yml
  loop_control:
    loop_var: rule
  tags: rules

- name: Configure Prometheus
  template:
    src:  templates/prometheus.yml.j2
    dest: "{{ root_path }}/prometheus/prometheus.yml"
    validate: "{{ root_path }}/prometheus/promtool check config %s"
  notify: Reload Prometheus
  tags: config
handler
- name: Reload Prometheus
  service:
    name: prometheus
    state: reloaded

ruleを反映させるには --tag rules 、config fileの場合は --tag config を指定するだけ
playbookも1つで済むし、rulesやconfigが間違っててPrometheus起動できないっていう事故も無くせるので

5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?