1. 程式人生 > >Ansible playbook批量安裝zabbix agent

Ansible playbook批量安裝zabbix agent

str ESS active 環境 name .rpm 自己 state over

自動化工具大家都有自己的選擇,有人喜歡slatstack,有人喜歡puppet,我選擇ansible,原因有兩條,簡單的同時適合我的環境。沒有最好的工具只有最適合你自己的工具。使用ansible就是為了解決很多簡單而需要頻繁執行的任務,我現在的環境中的zabbix 監控的agent和插件就有這樣的特性,每次新機器上線需要在機器上部署zabbix agent,手工一臺臺的安裝實在是費時,同時也容易出現問題,當然解決這樣的問題最好的方式是自己做一套PXE的部署系統,打好自己的鏡像,但是目前我們的環境中還沒有做,所以就用ansible來代勞了。
通過ansible 的play 寫好安裝腳本之後推到各個新節點上,之後再通過zabbix 的discover功能實現zabbix server 自動添加監控節點。這樣就會省去當了的手動工作,也就相當於ansible+zabbix disable 配合實現了zabbix 節點添加全自動化的功能,別的不說了,先上代碼。

#server 是個變量,執行ansible-playbook 的時候需要加上 -e “server=xxx” 這個是指你在ansible host 定義的組

  • hosts: "{{ server }}"
    vars:
    zabbix_agent_ip: "{{ ansible_em1[‘ipv4‘][‘address‘] }}"
    zabbix_server_ip: xxx.xxx.xxx.xxx
    remote_user: root
    tasks:
    #當系統為 centos 7 的時候安裝centos 7 的rpm 包
  • name: Install zabbix rpm source when OS version eq 7
    shell: "rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm"
    when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
    #當系統為centos 6 的時候安裝centos 6的rpm 包
  • name: Install zabbix rpm source when OS version eq 6
    shell: "rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/zabbix-release-3.4-1.el6.noarch.rpm"
    when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
    安裝zabbix agent
  • name: Install zabbix agent
    shell: "yum -y install zabbix-agent"
    #通過變量替換默認配置中的zabbix server地址
  • name: modify zabbix server ip address
    shell: "sed -i ‘s#Server=127.0.0.1#Server=‘{{zabbix_server_ip}}‘#g‘ /etc/zabbix/zabbix_agentd.conf"
  • name: modify zabbix server active ip addr
    shell: "sed -i ‘s/ServerActive=127.0.0.1/ServerActive=‘{{zabbix_server_ip}}‘/g‘ /etc/zabbix/zabbix_agentd.conf"
    #開啟遠程執行命令功能,這個功能是zabbix
  • name: Enable remote command execution
    shell: "sed -i ‘s/# EnableRemoteCommands=0/EnableRemoteCommands=1‘/g /etc/zabbix/zabbix_agentd.conf"
  • name: Enable remote command logs
    shell: "sed -i ‘s/# LogRemoteCommands=0/LogRemoteCommands=1‘/g /etc/zabbix/zabbix_agentd.conf"
    #引用變量zabbix_agent_ip 設置agent hostname
  • name: modify zabbix agent hostname
    shell: "sed -i ‘s/Hostname=Zabbix server/Hostname=‘{{zabbix_agent_ip}}‘/g‘ /etc/zabbix/zabbix_agentd.conf"
    #啟動agent 同時加入開機啟動項
  • service: name=zabbix-agent enabled=yes state=started

Ansible playbook批量安裝zabbix agent