Ansibleを使ってZabbixサーバ構築

はじめに

掲題の通りなんですが、Ansibleを使うのがこれで2回目という弱者なので、至らない点ばかりかと思いますがご容赦をば。

要件定義

  • 環境
    • AWS環境を想定
    • CentOS7(Zabbix-Server)
    • CentOS7(Zabbix-Agent)
    • Zabbix Ver4.0
  • 仕様
    • インベントリファイルにZabbixAgentのアドレスを記述し、ZabbixAgent接続用の秘密鍵を用意するだけでプレイブックを実行したらZabbix監視環境が構築出来るようにする。

構築

Zabbixサーバ側

AWS上にインスタンスを2つ用意したら構築していきます。
最終的なAnsibleの構成は次のようになります。

$ tree .
.
|-- ansible.cfg
|-- hosts
|-- README.md
|-- roles
|   |-- zabbix-agent
|   |   |-- files
|   |   |   `-- zabbix_agentd.conf
|   |   `-- tasks
|   |       `-- main.yml
|   `-- zabbix-server
|       |-- files
|       |   |-- CreateDB.sql
|       |   |-- my.cnf
|       |   |-- php.ini
|       |   |-- remi-php72.repo
|       |   |-- remi.repo
|       |   |-- zabbix.conf
|       |   `-- zabbix_server.conf
|       `-- tasks
|           `-- main.yml
|-- .ssh
|   |-- secret.pem
`-- start.yml

これを見ただけで素人がバレてしまったかもしれませんがご容赦をば。

Ansibleインストール

# yum -y update
# yum -y install epel-release
# yum -y install ansible

インベントリファイル

$ cd /etc/ansible
$ vi hosts
[zabbix-server]
localhost
[zabbix-server:vars]
DB_PASS=【DB用パスワード】
[zabbix-agents]
【ZabbixAgentのIPアドレス】
[zabbix-agents:vars]
ansible_port=22
ansible_user=centos
ansible_ssh_private_key_file=.ssh/secret.pem
  • DB_PASS:zabbixユーザがMySQLに接続する際のパスワード。英数字大文字小文字特殊文字を使用した8文字以上の強固な文字列にして下さい。
  • .ssh/secret.pem:エージェントにssh接続する用の鍵です。AWS接続に使用しているものか、新たに作成してください。但しファイル権限に注意してください。

プレイブック

$ cd roles/zabbix-server/tasks
$ vi main.yml
- name: download epel-release, remi, mysql repositorys
  yum:
    name: "{{ item.name }}"
    state: "{{ item.state }}"
  with_items:
      - { name: 'epel-release', state: 'latest' }
      - { name: 'http://rpms.famillecollet.com/enterprise/remi-release-7.rpm', state: 'present' }
      - { name: 'https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm', state: 'present' }

- name: copy remi.repo, remi-php72.repo
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
  with_items:
      - { src: 'remi.repo', dest: '/etc/yum.repos.d/remi.repo' }
      - { src: 'remi-php72.repo', dest: '/etc/yum.repos.d/remi-php72.repo' }

- name: import remi gpg-key
  rpm_key:
    state: present
    key: http://rpms.famillecollet.com/RPM-GPG-KEY-remi

- name: delete mariadb
  yum:
    name: mariadb-libs
    state: removed

- name: install mysql
  yum:
    name: "{{ item }}"
    state: present
  with_items:
      - mysql-community-devel
      - mysql-community-server
      - MySQL-python

- name: copy my.cnf
  copy:
    src: my.cnf
    dest: /etc/my.cnf
    mode: 0644

- name: enable mysql
  systemd:
    name: mysqld
    state: restarted
    enabled: yes

- name: get root password
  shell: "grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log | awk -F ' ' '{print $(NF)}'"
  register: root_password

- name: update expired root user password
  command: mysql --user root --password="{{ root_password.stdout }}" --connect-expired-password --execute='ALTER USER "root"@"localhost" IDENTIFIED BY "{{ DB_PASS }}";'

- name: create mysql client user
  mysql_user:
    name: zabbix
    password: "{{ DB_PASS }}"
    priv: "*.*:ALL,GRANT"
    host: localhost
    state: present
    login_user: root
    login_password: "{{ DB_PASS }}"

- name: download zabbix 4.0 repo
  yum:
    name: https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
    state: present

- name: install zabbix 4.0
  yum:
    name: "{{ item }}"
    state: present
  with_items:
      - zabbix-server-mysql
      - zabbix-web-mysql
      - zabbix-web-japanese
      - zabbix-agent
      - zabbix-get

- name: create db for zabbix
  shell: |
    mysql -u root -p"{{ DB_PASS }}" < /etc/ansible/roles/zabbix-server/files/CreateDB.sql
    cd /usr/share/doc/zabbix-server-mysql-4*
    zcat create.sql.gz | mysql -u zabbix -p"{{ DB_PASS }}" zabbix

- name: copy config files of zabbix
  copy: src="{{ item.src}}" dest="{{ item.dest }}" mode="{{ item.mode }}"
  with_items:
      - { src: 'zabbix_server.conf', dest: '/etc/zabbix/zabbix_server.conf', mode: '0640' }
      - { src: 'zabbix.conf', dest: '/etc/httpd/conf.d/zabbix.conf', mode: '0644' }
      - { src: 'php.ini', dest: '/etc/php.ini', mode: '0644' }

- name: Start zabbix_server, zabbix_agent, httpd service start
  systemd:
    name: "{{ item.name }}"
    state: "{{ item.state }}"
    enabled: "{{ item.enabled }}"
  with_items:
      - { name: 'zabbix-server', state: 'started', enabled: 'yes' }
      - { name: 'httpd', state: 'started', enabled: 'yes' }
      - { name: 'zabbix-agent', state: 'started', enabled: 'yes' }

解説

  • 3~10行目:EPEL, REMI, MySQLのリポジトリを用意する。
  • 12~18行目:Remiリポジトリの有効化。予め変更されたコンフィグをローカルにコピーしておき、playbook実行時に上書きする。
$ cd /etc/ansible/roles/zabbix-server/files
$ cp /etc/yum.repos.d/remi.repo ./
$ vi remi.repo
[remi]
enabled=0
 ↓
enabled=1
$ cp /etc/yum.repos.d/remi-php72.repo ./
$ vi remi-php72.repo
[remi-php72]
enabled=0
 ↓
enabled=1
  • 25~37行目:標準のMariaDBを消してMySQL Ver8.0をインストール。MySQL-pythonは初回ログイン等の対話処理に必要。
  • 39~43行目:デフォルトの認証プラグインを変更。
$ cp /etc/my.cnf ./
$ vi my.cnf
#default-authentication-plugin=mysql_native_password
 ↓
default-authentication-plugin=mysql_native_password
  • 51~56行目:ログから初期パスワードを取得しrootユーザでログインした後rootユーザのパスワードを更新。
  • 58~66行目:zabbixユーザ(zabbxi用)を作成。
  • 68~82行目:zabbixのリポジトリを登録しzabbixをインストール。
  • 84~88行目:DB作成。専用モジュールが無いようなのでshellモジュールで対応。shellモジュールの場合リダイレクトでファイルを読む際絶対パスでの指定が必要。
$ vi CreateDB.sql
CREATE DATABASE zabbix character set utf8 collate utf8_bin;
GRANT ALL ON zabbix.* TO zabbix@localhost;
quit;

90~95行目:zabbixのパラメータ詳細は割愛。アドレス、ホスト名、タイムゾーン等を各々設定。

$ cp /etc/zabbix/zabbix_server.conf ./
$ cp /etc/httpd/conf.d/zabbix.conf ./
$ cp /etc/php.ini ./
各々設定

念のため参考程度にパラメータを記載。

$ grep -v -e "^#" -e "^\s*$" zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBName=zabbix
DBUser=zabbix
DBPassword=【パスワード】
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
AllowRoot=1
$ grep -v -e "^#" -e "^\s*$" zabbix.conf
Alias /zabbix /usr/share/zabbix
<Directory "/usr/share/zabbix">
    Options FollowSymLinks
    AllowOverride None
    Require all granted
    <IfModule mod_php7.c>
        php_value max_execution_time 300
        php_value memory_limit 128M
        php_value post_max_size 16M
        php_value upload_max_filesize 2M
        php_value max_input_time 300
        php_value max_input_vars 10000
        php_value always_populate_raw_post_data -1
        php_value date.timezone Asia/Tokyo
    </IfModule>
</Directory>
<Directory "/usr/share/zabbix/conf">
    Require all denied
</Directory>
<Directory "/usr/share/zabbix/app">
    Require all denied
</Directory>
<Directory "/usr/share/zabbix/include">
    Require all denied
</Directory>
<Directory "/usr/share/zabbix/local">
    Require all denied
</Directory>

あとはサービスを起動する。

Zabbixエージェント側

プレイブック

- name: packages update
  yum: name=*
  state=latest

- name: download zabbix-agent 4.0 repo
  yum:
    name: http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.14-1.el7.x86_64.rpm
    state: present

- name: install zabbix-agent
  yum:
    name: zabbix-agent
    state: present

- name: copy zabbix_agentd.conf
  copy:
    src: zabbix_agentd.conf
    dest: /etc/zabbix/zabbix_agentd.conf

- name: start zabbix-agent
  systemd:
    name: zabbix-agent
    state: started
    enabled: yes

解説

  • 1~2行目:接続したら全パッケージをyumアップデート。
  • 4~12行目:Zabbix-Agent Ver4.0のリポジトリを登録、インストール。
  • 14~17行目:サーバ側同様コンフィグをコピーして各々設定。
$ cd roles/zabbix-agent/files
$ cp /etc/zabbix/zabbix_agentd.conf ./
軽く説明すると......
Server=【ZabbixサーバIP】
ServerActive=【ZabbixサーバIP】←ログ解析するなら設定
Hostname=【Zabbixで設定したZabbixエージェントホスト名】

実行

$ vi start.yml
- hosts: zabbix-agents
  become: yes
  roles:
    - zabbix-agent

- hosts: zabbix-server
  connection: local
  roles:
    - zabbix-server

$ ansible-playbook -i hosts start.yml

以上で完了です。

最後に

申し訳ございません。GitHubで公開はしておりませんので悪しからず。
最後までお付き合いいただきありがとうございました。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA