主要完成通过playbook自动生成zabbix_server,agent,这里没有完全实现自动化,这里机器的获取还是需要人为手工填写,如果感兴趣想通过自动获取需要部署的机器可以通过namp扫描工具awk命令截取,这里不过多描述。我用两台机器做的测试,老版本的模块用法会有差异,如果无法执行可以通过官网去查看https://ansible-tran.readthedocs.io/en/latest/,参考文档https://www.cnblogs.com/LyShark/p/10886486.html

ansible 2.4.2

zabbix 3.4.15

  • 安装nmap扫描工具

    yum install nmap -y
    #通过ping探测172.16.9.0网段中存活机器
    nmap -sP 172.16.9.0/24

完成机器间的免密登录

  • 生成公钥对

    ssh-keygen -t rsa
  • 设置ansible hosts配置

    [test]
    172.16.9.141 ansible_ssh_user="root" ansible_ssh_pass="root"
    172.16.9.142 ansible_ssh_user="root" ansible_ssh_pass="root" [zabbix_server]
    172.16.9.141
    [zabbix_client]
    172.16.9.142
  • 批量推送公钥到集群节点

    - hosts: test
    user: root
    tasks:
    - name: ssh-copy
    authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
    tags:
    - sshkey

    报错:

    FAILED! => {"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."}

    解决:

    /etc/ansible/ansible.cfg

    修改host_key_checking(默认是check的)

  • playbook构建zabbix_server(官方建议一个play写一个独立的任务,测试不过多计较了)

    # 初始化,关闭防火墙和SELINUX
    - hosts: zabbix_server
    tasks:
    - name: off selinux
    shell: setenforce 0
    - name: seline modify enforcing
    lineinfile:
    dest: /etc/selinux/config
    regexp: '^SELINUX='
    line: 'SELINUX=disabled'
    - name: seline firealld
    shell: systemctl stop firewalld && systemctl disable firewalld
    # 安装部署LAMP环境,通过YUM模块快速安装
    - hosts: zabbix_server
    tasks:
    - name: install LAMP
    yum: name={{item}} state=installed
    with_items:
    - httpd
    - httpd-devel
    - mariadb
    - mariadb-server
    - php
    - php-mysql
    - name: start httpd
    shell: systemctl restart httpd
    - name: start mariadb
    shell: systemctl restart mariadb
    # 下载YUM源,更新EOEL源,安装Zabbix
    - hosts: zabbix_server
    tasks:
    - name: clear YUM
    shell: rm -rf /etc/yum.repos.d/*
    - name: install YUM EPEL
    get_url: 'url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
    - name: yum install EPEL -y
    yum: name=epel-release state=installed
    - name: install zabbix.repo
    shell: rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
    - name: install zabbix
    yum: name={{item}} state=installed
    with_items:
    - zabbix-server-mysql
    - zabbix-web-mysql
    - zabbix-agent
    - name: start zabbix-server
    shell: systemctl restart zabbix-server
    - name: start zabbix-agent
    shell: systemctl restart zabbix-agent
    # 安装配置数据库权限,导入zabbix数据库
    - hosts: zabbix_server
    tasks:
    - name: set mariadb password
    shell: mysqladmin -u root password 'ansible'
    - name: create zabbix master databases
    shell: mysql -uroot -pansible -e 'create database zabbix character set utf8 collate utf8_bin;'
    - name: set zabbix master databases grant
    shell: mysql -uroot -pansible -e 'grant all privileges on zabbix.* to zabbix@localhost identified by "zabbix";'
    - name: import zabbix initial data SQL shell
    shell: zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix
    # 修改并拷贝配置文件,给予权限
    - hosts: zabbix_server
    tasks:
    - name: edit zabbix dbhost
    lineinfile:
    dest: /etc/zabbix/zabbix_server.conf
    regexp: '# DBHost=localhost'
    line: 'DBHost=localhost'
    - name: edit zabbix dbpasswd
    lineinfile:
    dest: /etc/zabbix/zabbix_server.conf
    regexp: '# DBPassword='
    line: 'DBPassword=zabbix'
    - name: cp zabbix web
    shell: cp -a /usr/share/zabbix/* /var/www/html/
    - name: chmod web
    shell: chmod 755 -R /var/www/html/*
    - name: chown web
    shell: chown apache.apache -R /var/www/html/* - name: set php
    shell: echo "date.timezone = Asia/Shanghai" >> /etc/php.ini
    - name: set php
    shell: echo "max_execution_time = 300" >> /etc/php.ini
    - name: set php
    shell: echo "max_input_time = 300" >> /etc/php.ini
    - name: set php
    shell: echo "post_max_size = 32M" >> /etc/php.ini
    - name: set php
    shell: echo "memory_limit = 128M" >> /etc/php.ini
    - name: set php
    shell: echo "mbstring.func_overload = 0" >> /etc/php.ini - name: start http mysql zabbix
    shell: systemctl restart httpd ; systemctl restart mariadb
    - name: start http mysql zabbix
    shell: systemctl restart zabbix-server ; systemctl restart zabbix-agent
    - name: enabled http mysql zabbix
    shell: systemctl enable httpd ; systemctl enable mariadb
    - name: start http mysql zabbix
    shell: systemctl enable zabbix-server ; systemctl enable zabbix-agent
    • playbook构建zabbixclient
    ---
    # 初始化,关闭防火墙和SELINUX
    - hosts: zabbix_client
    tasks:
    - name: off selinux
    shell: setenforce 0
    - name: seline modify enforcing
    lineinfile:
    dest: /etc/selinux/config
    regexp: '^SELINUX='
    line: 'SELINUX=disabled'
    - name: seline firealld
    shell: systemctl stop firewalld && systemctl disable firewalld
    # 安装zabbix_client
    - hosts: zabbix_client
    vars:
    zabbix_server_ip: 172.16.9.141
    zabbix_agent_ip: 172.16.9.142 tasks:
    - name: install zabbix_client
    shell: rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.15-1.el7.x86_64.rpm
    - name: Install zabbix agent
    shell: yum -y install zabbix-agent
    - 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
    - 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
    - name: modify zabbix agent hostname
    shell: sed -i 's/Hostname=Zabbix server/Hostname='{{zabbix_agent_ip}}'/g' /etc/zabbix/zabbix_agentd.conf
    - name: enable zabbix-agent
    shell: systemctl start zabbix-agent ;systemctl enable zabbix-agent

再来几个检查语法结构,主机是否生效的命令

ansible-playbook install_zabbix_server.yaml --syntax-check
ansible-playbook install_zabbix_server.yaml --list-task
ansible-playbook install_zabbix_server.yaml --list-hosts

ansible-playbook安装zabbix_server,agent监控的更多相关文章

  1. 写Ansible playbook添加zabbix被监控的对象

    本主题达到的效果是能通过编写Ansible Playbook,创建zabbix主机组,把被监控的对象加入到zabbix监控系统中,同时链接到对象的模板. 1.准备工作 在zabbix服务器上面,我们需 ...

  2. ansible playbook 安装docker

    1.新增host配置到/etc/ansible/hosts文件中 [docker] 192.168.43.95 2.配置无密码登录 # 配置ssh,默认rsa加密,保存目录(公钥)~/.ssh/id_ ...

  3. Vagrant Ansible Playbook 安装一群虚拟机

    https://docs.ansible.com/ https://favoorr.github.io/2017/01/06/vagrant-virtualbox-vagrantfile-config ...

  4. ansible自动安装rabbitmq

    ansible playbook 安装rabbitmq单机版,以下脚本在CentOS6.7服务器测试通过. 需要配置本机的yum源,用于安装socat软件. rabbitmq版本和Erlang版本需要 ...

  5. Centos下zabbix部署(二)agent安装并设置监控

    1.配置zabbix源 rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch ...

  6. 使用Ubuntu系统编译安装Zabbix企业级监控系统

    使用Ubuntu系统编译安装Zabbix企业级监控系统   作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Ubuntu系统部署笔记:https://www.cnblogs.com/ ...

  7. Centos7 使用 Ansible 批量安装中文字体

    需求背景 Centos7 下 Java 生成图片水印时中文乱码,原因是没有安装中文字体. 安装中文字体 以下是基于 Centos7 手动安装中文字体的详细步骤.当测试或者生产环境服务器比较多的时候,建 ...

  8. CentOS 6.5安装部署Zabbix监控系统

    CentOS 6.5安装部署Zabbix监控系统 先说一点废话,我没有用centos7做实验,讲真,centos 7我也不常用,喜欢新版本的同学其实可以尝试下,注意一点的就是centos 6.5只支持 ...

  9. ansible离线安装

    目录 1. ansible离线安装 2. ansible配置文件 3. ansible常用的命令: 1. ansible离线安装 最近要在内网部署一台ansible服务器,只能手动离线安装ansibl ...

随机推荐

  1. centos7 中没有service iptables save指令来保存防火墙规则

    解决方法: systemctl stop firewalld  关闭防火墙yum install iptables-services 安装 iptables 服务systemctl enable ip ...

  2. 【cf补题记录】Codeforces Round #607 (Div. 2)

    比赛传送门 这里推荐一位dalao的博客-- https://www.cnblogs.com/KisekiPurin2019/ A:字符串 B:贪心 A // https://codeforces.c ...

  3. 【Activiti学习之五】BPMN事件

    环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.事件定义1.定时器事件(1)timeDate:指定时间触发<timerEven ...

  4. kafka作为elk缓存使用

    ELK集群在大规模的日志收集中面临着数据量大,收集不及时,或宕机的风险,可以选择单节点的redis,但是相比redis,kafka集群高可用的特性,更优,下面来配置kafka集群配置elk作为缓存的方 ...

  5. Docker学习-从无知到有知的学习过程

    Docker学习 最近被别人提到的docker吸引到了注意力,所以打算先快速的了解一下docker到底是个上面东西. 之所以我写下这个文档呢,是为了记录对docker一无所知我是如何进行学习一门新技术 ...

  6. Docker 常用命令速查手册

    记录一下docker的日常使用命令,本文主要针对linux + mac操作系统而言,window是否适用不太确定,谨慎使用 1. docker进程 docker进程启动.停止.重启,常见的三种case ...

  7. Zhe Second Language——

    You got things to do. Place to go. People to see. Futures to make. 我们还有事要做,有地儿要去,有人要见,有梦要实现. Remembe ...

  8. 045 用户登录功能01----JWT和后台代码

    (1)有状态登录概述 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session. 例如登录:用户登录后,我们把登录者 ...

  9. 《C++ 习题与解析》笔记

    目录 ####Chapter-1 C++语言概述(错题) ####Chapter-2 类和对象 ####Chapter-3 引用 ####Chapter-4 友元函数 #### Chapter-5 运 ...

  10. mysql 中的日期格式。date_format( ) 转换格式

    date_format( ) 转换格式 : 格式 描述 %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) %e 月的天,数值(0-31) ...