美文网首页@IT·互联网
使用Ansible脚本批量快速安装zabbix-agent

使用Ansible脚本批量快速安装zabbix-agent

作者: 阿乐_822e | 来源:发表于2021-11-03 16:14 被阅读0次

Ansible服务的安装这里不再赘述。安装完成后,配置好/etc/ansible/hosts文件及ansible服务器到各台机器的免密登录。再登录ansible服务器。

  • 创建安装目录并准备好安装软件
$ mkdir -p /usr/local/setup/zbx_agent_setup/zabbix-agent
# agent安装文件请自行下载
$ tar zxvf zabbix_agent-4.4.7-linux-3.0-amod64-static.tar.gz  -C /usr/local/setup/zbx_agent_setup/zabbix-agent
# 修改一些配置
$ vi zabbix-agent/conf/zabbix_agentd.conf  
# 修改“Server="与"ServerActive="两处为zabbix服务器的IP
  • 准备好zabbix-agent的服务文件
$ cd /usr/local/setup/zbx_agent_setup/
$ vi zabbix_agentd   # 添加如下内容:
#!/bin/sh
#chkconfig:2345 80 90
#description:auto_run


# Source function library.
. /etc/rc.d/init.d/functions

if [ -x /usr/local/zabbix-agent/sbin/zabbix_agentd ]; then
     exec=/usr/local/zabbix-agent/sbin/zabbix_agentd 
else
     exit 5
fi

prog=${exec##*/}
conf=/usr/local/zabbix-agent/conf/zabbix_agentd.conf
pidfile=$(grep -e "^PidFile=.*$" $conf | cut -d= -f2)
timeout=10

lockfile=/var/lock/subsys/zabbix-agent

start()
{
     echo -n $"Starting Zabbix agent: "
     daemon $exec -c $conf
     rv=$?
     echo
     [ $rv -eq 0 ] && touch $lockfile
     return $rv
}

stop()
{
     echo -n $"Shutting down Zabbix agent: "
     killproc -p $pidfile -d $timeout $prog
     rv=$?
     echo
     [ $rv -eq 0 ] && rm -f $lockfile
     return $rv
}

restart()
{
     stop
     start
}

case "$1" in
     start|stop|restart)
         $1
         ;;
     force-reload)
         restart
         ;;
     status)
         status -p $pidfile $prog
         ;;
     try-restart|condrestart)
         if status $prog >/dev/null ; then
             restart
         fi
         ;;
     reload)
         action $"Service ${0##*/} does not support the reload action: " /bin/false
         exit 3
         ;;
     *)
         echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
         exit 2
         ;;
esac

  • 准备ansible-playbook文件
$ vi zbx-install.yml    
---
- hosts: All
  vars:
        hostname : "{{ ansible_hostname }}"
        source_dir: "/usr/local/setup/zbx_agent_setup"
        desc_dir: "/usr/local"

  tasks:

      - name: Create group "zabbix" 
        group: name=zabbix

      - name: Create user "zabbix" 
        user: name=zabbix

      - name: set zabbix's password
        command: echo "zabbix" > passwd zabbix 
      
      - name: copy to direct
        copy: src={{ source_dir }}/zabbix-agent  dest={{ desc_dir }} owner=zabbix group=zabbix mode=0755

      - name: Get remote hostname
        set_fact: hostname={{ hostname }}

      - name: Configure hostname in the zabbix-agentd.conf file
        lineinfile: path={{ desc_dir }}/zabbix-agent/conf/zabbix_agentd.conf regexp="Hostname=Zabbix server" line=Hostname={{ hostname }}

      - name: Registy zabbix-agentd to system service
        copy: src={{ source_dir }}/zabbix_agentd  dest=/etc/init.d/  mode=0755

      - name: start zabbix-agentd   
        service: name=zabbix_agentd state=started enabled=yes
      
      - name: add zabbix-agentd  to chkconfig    
        command: chkconfig --add zabbix_agentd    

      - name: set chkconfig for zabbix-agentd   
        command: chkconfig zabbix_agentd on        

...

  • 总结一下目录结构,两个文件与一个安装目录
$ pwd 
/usr/local/setup/zbx_agent_setup/
$ ll
总用量 8
drwxr-xr-x 5 zabbix zabbix   88 10月 29 01:24 zabbix-agent
-rwxr-xr-x 1 root   root   1323 10月 29 03:39 zabbix_agentd
-rwxr-xr-x 1 root   root   1460 10月 29 03:35 zbx-install.yml
  • 开始安装
$ ansible-playbook zbx-install.yml

相关文章

网友评论

    本文标题:使用Ansible脚本批量快速安装zabbix-agent

    本文链接:https://www.haomeiwen.com/subject/ahhczltx.html