概述

通过使用ansible下发shell脚本,一键安装zabbix-agent客户端。

开始安装

  1. 首先把下面的脚本,放在ansible控制台的/etc/ansible/shell/zabbix-agent.sh中。
    #!/bin/bash
    echo -e "请给出要安装的zabbix版本号,建议使用4.x的版本  \033[31musage:./zabbix-agent.sh 4.0|4.4|4.5 \033[0m"
    echo "例如要安装4.4版本,在命令行写上 ./zabbix-agent.sh 4.4"
    if [ -z $1 ];then
    exit
    fi
    VERSION=$1
    if [ -f /etc/yum.repos.d/zabbix.repo ];then
    rm -rf /etc/repos.d/zabbix.repo
    fi
    rpm -qa | grep zabbix-release && rpm -e zabbix-release
    rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/$VERSION/rhel/7/x86_64/zabbix-release-$VERSION-1.el7.noarch.rpm
    sed -i "s@zabbix/.*/rhel@zabbix/$VERSION/rhel@g" /etc/yum.repos.d/zabbix.repo
    sed -i 's@repo.zabbix.com@mirrors.aliyun.com/zabbix@g' /etc/yum.repos.d/zabbix.repo
    [ $? -eq 0 ] && echo "阿里云的zabbix源替换成功" || exit 1
    yum clean all
    yum makecache fast
    yum install zabbix-agent -y
    if [ -e /etc/zabbix/zabbix_agentd.conf ];then
    echo
    echo "##########begin modify zabbix_agentd.conf##########"
    sed -i 's/^Server=127.0.0.1/Server=172.16.100.19/g' /etc/zabbix/zabbix_agentd.conf
    sed -i 's/^ServerActive=127.0.0.1/ServerActive=172.16.100.19/g' /etc/zabbix/zabbix_agentd.conf
    sed -i '/Timeout=3$/a\Timeout=30' /etc/zabbix/zabbix_agentd.conf
    grep -v '^#' /etc/zabbix/zabbix_agentd.conf |grep -v '^$'
    echo "##########end modify zabbix_agentd.conf##########"
    echo
    else
    echo
    echo "##########zabbix_agentd.conf is not exit##########"
    echo
    exit 2
    fi
    systemctl start  zabbix-agent
    systemctl enable zabbix-agent 
  2. 执行命令#ansible zabbixagent -m script -a "/etc/ansible/shell/zabbix-agent.sh 4.4"