ansible离线(二进制)批量安装zabbix-agent

目录tree

[root@ansible roles]# pwd
/etc/ansible/roles
[root@ansible roles]# tree 
.
└── zabbix
    └── tasks
        ├── files
        │   └── zabbix_agent-5.0.31-linux-3.0-amd64-static.tar.gz
        ├── templates
        │   └── zabbix_agentd.conf.j2
        └── zabbix.yaml

4 directories, 3 files

1. 定义主机清单

定义主机组和登录凭证(也可以设置凭证,使用免密登录)及zabbix Hostname参数

[zabbixagent]
192.168.52.173 Hostname=agent1
192.168.52.174 Hostname=agent2

[zabbixagent:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456

2. 定义template

zabbix_agentd.conf.j2文件内容

LogFile=/var/log/zabbix/zabbix_agentd.log
Server={{ Server }}
ListenPort=10050
ServerActive={{ ServerActive }}
Hostname={{ Hostname }}
AllowRoot=1
Include=/usr/local/etc/zabbix_agentd.conf.d/*.conf

3. 定义ansible-playbook

- hosts: zabbixagent
  remote_user: root
  gather_facts: false
  ignore_errors: yes
  vars:
    - Server: 10.1.1.1
    - ServerActive: 10.1.1.1
  tasks:
    - name: 创建zabbix用户组
      group:
        name: zabbix
        state: present
    - name: 创建zabbix用户
      user:
        name: zabbix
        group: zabbix
        state: present
    - name: 创建zabbix-agent目录
      file:
        name: "{{item.item1}}"
        state: directory
        owner: zabbix
        group: zabbix
        mode: "{{item.item2}}"
      with_items:
        - {item1: "/usr/local/zabbix_agent", item2: "0644"}
        - {item1: "/var/log/zabbix", item2: "0777"}
        - {item1: "/usr/local/etc/zabbix_agentd.conf.d", item2: "0644"}
    - name: 解压zabbix.tar包并分发文件
      unarchive:
        src: zabbix_agent-5.0.31-linux-3.0-amd64-static.tar.gz
        dest: /usr/local/zabbix_agent
        copy: yes
    - name: 停止zabbix-agent程序
      shell: ps -ef |grep zabbix_agentd|grep -v grep |awk '{print $2}' |xargs kill -9
    - name: 创建软连接
      file:
        src: "{{item.item1}}"
        dest: "{{item.item2}}"
        owner: zabbix
        group: zabbix
        state: link
      with_items:
        - {item1: "/usr/local/zabbix_agent/bin/zabbix_sender", item2: "/usr/local/bin/zabbix_sender"}
        - {item1: "/usr/local/zabbix_agent/bin/zabbix_get", item2: "/usr/local/bin/zabbix_get"}
        - {item1: "/usr/local/zabbix_agent/sbin/zabbix_agentd", item2: "/usr/local/sbin/zabbix_agentd"}
    - name: 分发配置文件
      template:
        src: zabbix_agentd.conf.j2
        dest: /usr/local/etc/zabbix_agentd.conf
    - name: 分发启动文件
      shell: cp /usr/local/sbin/zabbix_agentd /etc/init.d
    - name: 添加etc/init.d/zabbix_agentd执行权限
      shell: chmod +x /etc/init.d/zabbix_agentd && chmod +x /etc/rc.d/rc.local
    - name: 启动zabbix-agent
      shell: /etc/init.d/zabbix_agentd
    - name: 开机启动zabbix-agent
      shell: echo /etc/init.d/zabbix_agentd >> /etc/rc.d/rc.local
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值