LoginSignup
3
4

More than 5 years have passed since last update.

ansibleをオンプレ運用に活用しよう

Last updated at Posted at 2018-03-29

全サーバに一気にコマンド叩きたいときにめっちゃ便利。

今回はこのシーン。zabbixに独自itemを追加したとき。これ、全サーバにitemを配置して zabbix-agent を再起動しないといけない。

業務ではubuntuだけ、とかcent6だけ、とか叩きたいシーンが多いのであらかじめ /etc/ansible/hosts にそういうグループを作っておく。これだけでいろんな調査がめっちゃはかどる。

ubuntu:
  hosts:
    host1:
    host2:
cent6:
  hosts:
    host3:
    host4:
cent7:
  hosts:
    host5:
    host6:

テストはこのコマンド

      ansible all --list-hosts            # 勝手にgroupのhostsが all にまとまってくれる
      ansible ubuntu --list-hosts

Childrenにするのがいいのか親にするのがいいのかはよくわかってない。

で、これを叩くと、一発でzabbix itemが動き始める!

zabbixにitem追加した時に叩くヤツ

全サーバ一気に更新&zabbix-agent再起動

ansible all  -a "bash -c 'cd /tools && git pull --ff-only && git push && update-zabbix-item'"
ansible cent6 -a "service zabbix-agent restart"
ansible "ubuntu cent7" -a "systemctl restart zabbix-agent"

update-zabbix-item は独自コマンドで、 /tools にいるitem用conf/shをzabbixが使うdirにln -s して chmod +x してくれるやつです。

もちろんリスタートするまでをshにしてしまってもいいんだけど、こうして対象サーバ群を便利に変えられるのがansibleのすごいところなので、しばらくこんな感じで使ってみようと思います。

3
4
4

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