Ansible自動化導入の運用


1.概要
Ansibleは、Python言語で開発された軽量レベルの自動化メンテナンスツールです.インストールの導入プロセスは簡単で、学習曲線は平坦です.ビジネスの関係上、1000個のZabbixエージェントをクラスタに導入する必要があるので、Ansibleを利用するのが最善の選択です.
2.実戦
1.Ansibleの取り付け
yum–y install ansibleイントラネットの場合、ansibleとその依存するrpmパッケージをyumソースに追加してインストールします.
2.ホストプロファイル
/etc/ansibleにホストを追加する、ホストプロファイルはhostsである、ansibleであってもよい.cfgで構成inventory={ホスト構成ファイルパス}を変更する特定のhostsフォーマット[zabbix-agent]#パケット名は、1つのファイル1つのパケットが望ましい.IP ansible_ssh_user='{アカウント名}'ansible_ssh_pass='{パスワード}'hostname={ホスト名、カスタマイズ可能}
3.インストールスクリプトの作成
Ansibleファイルディレクトリ:
├── ansible.cfg
├── hosts
└── roles
    ├── install_zabbix_agent
    │   ├── file
    │   │   ├── zabbix-agent-4.2.4-1.el7.x86_64.rpm
    │   │   └── zabbix_agentd.conf
    │   ├── handler
    │   │   └── main.yml
    │   └── tasks
    │       ├── install.yml
    │       ├── main.yml
    │       └── setport.yml
└── install_zabbix_agent.yml

install_zabbix_agent.yml
- hosts: zabbix-agent
  remote_user: root
  sudo: yes
  sudo_user: root
  gather_facts: true
  roles:
  - install_zabbix_agent

handlerの下のmain.yml
- name: restart zabbix-agent
  service: name=zabbix_agentd state=restarted

tasksの下のmain.yml
- import_tasks: install.yml
- import_tasks: setport.yml

install.yml
  - block:
      - name: "copy zabbix_agent to clients"
        cpoy:
          src=zabbix-agent-4.2.4-1.el7.x86_64.rpm
          dest=/tmp
      - name: "yum install zabbix_agent"
        yum:
          name: /tmp/zabbix-agent-4.2.4-1.el7.x86_64.rpm
          state: present
      - name: "copy zabbix_agentd.conf"
        copy:
          src=zabbix_agentd.conf
          dest=/etc/zabbix/zabbix_agentd.conf
      - name: disabled selinux
        shell: /usr/sbin/setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
      - name: "start zabbix, enable zabbix"
        service:
          name=zabbix-agent
          state=started
          enabled=yes
        notify:
    - restart zabbix-agent

setport.yml:
   - block:
      - name: mkdir log file
        shell: mkdir -p /var/log/zabbix
      - name: chmod for log
        shell: chmod -R 755 /var/log/zabbix
      - name: chown for log
        shell: chown -R zabbix. /var/log/zabbix
      - name: chmod for zabbix
        shell: chmod -R 755 /etc/zabbix
      - name: chown for zabbix
        shell: chown -R zabbix. /etc/zabbix
      - name: change log filepath
        shell: sed -i 's/LogFile=\/var\/log\/zabbix\/zabbix_agent.log/LogFile=\/var\/log\/zabbix\/{{hostname}}.log/g' /etc/zabbix/zabbix_agentd.conf
      - name: change server ip
        shell: sed -i 's/Server=127.0.0.1/Server=10.10.40.70/g' /etc/zabbix/zabbix_agentd.conf
      - name: change server active ip
        shell: sed -i 's/ServerActive=127.0.0.1/ServerActive=10.10.40.70/g' /etc/zabbix/zabbix_agentd.conf
      - name: change hostname
        shell: sed -i 's/Hostname=Zabbix Server/Hostname={{hostname}}/g' /etc/zabbix/zabbix_agentd.conf
        notify:
      - restart zabbix-agent