Ansible自动部署zabbix-agent监控

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

一、此处不是本节重点简要写一部分概括一下!!

1、在ansible自动部署zabbix-agent之前首先在管理机上安装ansible、

ssh-keygen -t rsa
ssh-copy-id root@192.168.71.21
ssh-copy-id root@192.168.71.22
ssh-copy-id root@192.168.71.23

2、并配置ssh免密登录(省事对接下来的操作)

3、添加主机组确定ansible接下来管理哪些机器实现自动部署

开始部署zabbix-agent角色

1、

2、准备zabbix-agent的配置模板

注意:此处适用Jinja模板因为zabbix监控被监控机器时每个机器都有自己的主机名,Jinja模板支持调用变量并且Jinja模板要使用template模块推送,建议把更改过得文件名字以.j2

cp /etc/zabbix/zabbix_agentd.conf  /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf
mv /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf.j2

 

vim /etc/ansible/roles/zabbix/templates/zabbix_agentd.conf.j2
一下需要更改的信息:

Server=192.168.71.10
ServerActive=192.168.71.10
Hostname={{ ansible_fqdn }}

3、开始编写剧本

[root@localhost roles]# cat /etc/ansible/roles/zabbix/tasks/main.yml 
---
# tasks file for zabbix

- name: yum install
  yum_repository:
      name: zabbix
      description: YUM zabbix
      file: zabbix
      baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/
      gpgcheck: no
      enabled: yes
- name: install zabbix-agent
  yum:
      name: zabbix-agent
      state: present
- name: copy
  template:
      src: zabbix_agentd.conf.j2
      dest: /etc/zabbix/zabbix_agentd.conf
  notify: server zabbix
- name: systemctl
  service:
      name: zabbix-agent
      state: started
[root@localhost roles]# cat /etc/ansible/roles/zabbix/handlers/main.yml 
---
# handlers file for zabbix

- name: server zabbix
  service:
      name: zabbix-agent
      state: restarted
[root@localhost roles]# cat /opt/11/zabbix.yaml 
- hosts: sqlserver
  user: root
  roles:
    - zabbix

4、剧本完成,执行剧本

​

[root@localhost roles]# ansible-playbook /opt/11/zabbix.yaml 

PLAY [sqlserver] ************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]

TASK [zabbix : yum install] *************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]

TASK [install zabbix-agent] *************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]

TASK [zabbix : copy] ********************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.21]
ok: [192.168.71.23]

TASK [zabbix : systemctl] ***************************************************************************************************************************************************************************************
ok: [192.168.71.22]
ok: [192.168.71.23]
ok: [192.168.71.21]

PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.71.21              : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.71.22              : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.71.23              : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[点击并拖拽以移动]
​

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值