以前、zabbix-agentをstatic-libでbuildした記事を書いたが、
これをdeployするansibleのplaybookのsample。
ansibleはまだまだ勉強中です。
zabbix_agent staticライブラリ化 - Qiita
テスト環境
CentOS 6系(client,server共に)
ansible 2.0.1.0
playbook
前提
内容としては作成した運用ツール(今回はzabbix-agent)を、
fileや必要なdirectoryが存在しない場合に適宜作成と配置を行いたい。
configの配置場所とかはrpm版で動かしている物の内容を流用したので、
それに近い形になってるだけなので適宜書き換えてください。
*注:agent.confの中身のHostNameの部分については
ansibleのhostsと連携しないようになってるplaybookなのでそこのみ手動介入が必要です。
3/25追記:conf配置時にinventoryを引用してHostnameを設定するようにしました。
自身の環境ではhostsの名前とzabbixの設定の名前が管理上一致しない事もあったので外してました。
またcopyするファイルの元は、
/usr/local/ansible/master_files/zabbix/
配下に準備した。それらを各サーバに撒きにいく想定です。
事故防止になるかわかりませんが、
同じファイル名が存在している場合は内容に差分があっても書き換えない、
force=no
にしている点と
ファイルとdirectory作成時のパーミッションを指定しています。
mode=0755
playbookの中身 yml
---
# select to play host-group.
- hosts: hogehoge
# Not get Server informations.
gather_facts: no
tasks:
# zabbux ユーザの追加
- name: useradd zabbix
user: name=zabbix shell=/sbin/nologin state=present
# 配置に必要(自身が必要とする)ディレクトリ群を755で作成しオーナーをzabbixへ
- name: zabbix required dir checks and creates.#1
file: path={{ item }} state=directory mode=0755 owner=zabbix group=zabbix
with_items:
- /var/run/zabbix # pidの場所
- /var/log/zabbix # zabbix-agentのlogを吐く場所
# 配置に必要(自身が必要とする)ディレクトリ群を755で作成
- name: zabbix required dir checks and creates.#2
file: path={{ item }} state=directory mode=0755
with_items:
- /usr/local/zabbix/bin # zabbixのバイナリ置き場
- /etc/zabbix/zabbix_agentd.d # userparameter.confを置く場所
- /usr/lib/zabbix/externalscripts # LLDやuserparameterの中で使ったりするスクリプト置き場
- name : copy items conf
# ファイルが存在しない場合にファイルを配置
copy : src=/usr/local/ansible/master_files/zabbix/{{ item }} dest=/etc/zabbix/{{ item }} force=no
with_items :
- zabbix_agentd.conf
register : agent_conf
- name : agent.conf hostname changed
# 3/25追記
# ファイルを設置した場合に、confのhostnameをinventoryを使い、hostsの名前を引用し設定
lineinfile : dest=/etc/zabbix/{{ item }} backrefs=yes state=present regexp="^Hostname=" line="Hostname={{ inventory_hostname }}"
when : agent_conf|changed
# -C での実行時にerrorになるのでignoreを追加 2016/05/13
ignore_errors: True
with_items :
- zabbix_agentd.conf
- name : copy items bin
# ファイルが存在しない場合に755でファイルを配置
copy : src=/usr/local/ansible/master_files/zabbix/bin/{{ item }} dest=/usr/local/zabbix/bin/{{ item }} force=no mode=0755
with_items :
- zabbix_static_agentd
- zabbix_static_sender
- name : copy items userparameter.conf
# ファイルが存在しない場合にファイルを配置
copy : src=/usr/local/ansible/master_files/zabbix/{{ item }} dest=/etc/zabbix/zabbix_agentd.d/{{ item }} force=no
with_items :
- userparameter.conf
- name : copy items agent scripts
# ファイルが存在しない場合に755でファイルを配置
copy : src=/usr/local/ansible/master_files/zabbix/agentscript/{{ item }} dest=/usr/lib/zabbix/externalscripts/{{ item }} force=no mode=0755
with_items :
- original.agentscript
- name : copy items etc init file
# ファイルが存在しない場合に755でファイルを配置
copy : src=/usr/local/ansible/master_files/zabbix/init_script/{{ item }} dest=/etc/init.d/{{ item }} force=no mode=0755
with_items :
- zabbix-agent
実行
実行前に-C
でテストをしてみるのが良いです。
また、今回はrootでsshするパターンで記載してます。
# ansible-playbook -u root playbook/zabbix-deploy.yml -k
SSH password:
PLAY ***************************************************************************
TASK: [useradd zabbix] ********************************************************
changed: [hoge-host]
TASK: [zabbix required dir checks and creates.#1] *****************************
changed: [hoge-host] => (item=/var/run/zabbix)
changed: [hoge-host] => (item=/var/log/zabbix)
TASK: [zabbix required dir checks and creates.#2] *****************************
changed: [hoge-host] => (item=/usr/local/zabbix/bin)
changed: [hoge-host] => (item=/etc/zabbix/zabbix_agentd.d)
changed: [hoge-host] => (item=/usr/lib/zabbix/externalscripts)
TASK [copy items conf] *********************************************************
changed: [hoge-host] => (item=zabbix_agentd.conf)
TASK [agent.conf hostname changed] *********************************************
changed: [hoge-host] => (item=zabbix_agentd.conf)
TASK [copy items bin] **********************************************************
changed: [hoge-host] => (item=zabbix_static_agentd)
changed: [hoge-host] => (item=zabbix_static_sender)
TASK [copy items userparameter.conf] *******************************************
changed: [hoge-host] => (item=userparameter.conf)
TASK [copy items externalscripts] **************************************************
changed: [hoge-host] => (item=original.agentscript)
TASK [copy items etc] **********************************************************
changed: [hoge-host] => (item=zabbix-agent)
PLAY RECAP *********************************************************************
hoge-host : ok=9 changed=9 unreachable=0 failed=0
既にファイルが配置されてる、ディレクトリが存在する場合は
その部分はchangedにならずokのみで帰ってきます。
また、Hostname変更の部分で、confが配置されなかった場合(changedではない)は、
Skippedの動作になります。
failed等のハンドリング方法もあった気がするのですが、
手が回りましたら記事の方もアップデートします。
おまけ
ちなみに、startまで実施したい方は以下も突っ込むとagentの起動までいけるとおもいます。
ハンドラなどを駆使して取り込むと良いのかもしれません。
- hosts : zabbix-agent
gather_facts: no
tasks :
- name : start service
service : name={{ item }} state=started
with_items :
- zabbix-agent