xinghe0 发表于 2018-7-29 11:25:59

ansible自动部署 zabbix-agent 的模块

  ansible自动部署 zabbix-agent 模块的准备阶段
  ansible所在的服务端可以免密钥登录所被部署的机器称为客户端。
  免密钥的做法
  服务端 ssh-keygen一路回车生成密钥对
  ssh-copy-id 指定IP 将公钥发给指定的ip 即可
  ssh-copy-id 192.168.1.18
  下面红色是代表文件或目录黑色字体代表是内容
  使用了 roles 方法整体的目录结构是
  /etc/ansible/zabbix-agent.yml
  
  # pwd
  /etc/ansible
  /etc/ansibl/hosts
  # cat hosts
  192.168.1.145#centso 系统
  192.168.1.147#   centos 系统   加入 ansible 的客户端ip本次示例中是这几个 ip
  192.168.1.148#ubuntu 系统
  # cat zabbix-agent.yml
  ---
  - hosts: all
  roles:
  - zabbix-agent
  
  /etc/ansible/roles
  
  #ls
  zabbix-agent
  /etc/ansible/zabbix-agent
  
  # ls
  fileshandlerstaskstemplates
  
  /etc/ansible/zabbix-agent/files
  
  # ls files/
  zabbix
  
  /etc/ansible/zabbix-agent/handlers
  
  
  # ls handlers/
  main.yml
  
  /etc/ansible/zabbix-agent/tasks
  
  # ls tasks/
  files.ymlmain.ymlpackage.ymlservice.yml
  
  /etc/ansible/zabbix-agnet/templates
  
  # ls templates/
  zabbix_agentd.conf
  /etc/ansible/zabbix-agent/files/zabbix/conf.d
  
  
  # ls files/zabbix/conf.d/#zabbix-agent的配置文件 键值
  impression.confimsecret.confmystar.confresonance.conftagme.confuserReg.conf
  /etc/ansible/zabbix-agent/files/zabbix/scripts 要复制去客户端的文件
  # ls files/zabbix/scripts/下边是zabbix 可执行的脚本用来监控
  /etc/ansible/zabbix-agent/handlers/main.yml这个是开启zabbix-agent的
  
  # cat handlers/main.yml
  ---
  - name: start zabbix_agentd
  service: name=zabbix-agentd state=started
  /etc/ansible/zabbix-agent/tasks/files.yml 将 /etc/ansible/zabbix-agent/files/* 复制到客户端的配置
  # cat tasks/files.yml
  ---
  - name: copy centos template conf file
  template: src=zabbix_agentd.confdest=/etc/
  when: ansible_os_family == "RedHat" #判断系统
  - name: copy ubuntu template conf file
  template: src=zabbix_agentd.confdest=/etc/zabbix/
  when: ansible_os_family == "Debian"
  - name: crete conf.d and scripts
  file: path=` item ` state=directory owner=root group=root mode=0755
  with_items:
  - /etc/zabbix/scripts
  - /etc/zabbix/conf.d
  - /etc/zabbix/scripts1
  - name: copy the scripts/file
  copy: src=zabbix/scripts/ dest=/etc/zabbix/scripts/ mode=0755
  - name: copy the scripts1/file
  copy: src=zabbix/scripts1/ dest=/etc/zabbix/scripts1/ mode=0755
  - name: copy the conf.d/file
  copy: src=zabbix/conf.d/ dest=/etc/zabbix/conf.d/
  
  /etc/ansible/zabbix-agent/tasks/main.yml 主的配置 在同级目录下会先执行这个main
  
  # cat tasks/main.yml
  ---
  - include: 'package.yml'
  - include: 'files.yml'
  - include: 'service.yml'
  
  /etc/ansible/zabbix-agent/tasks/package.yml 安装zabbix-agent的配置
  
  # cat tasks/package.yml
  ---
  - name: useraddd the zabbix
  user: name=zabbix
  - name: install epel-release
  yum: name=epel-release.noarch state=latest
  when: ansible_os_family == "RedHat"
  - name: install epel-release andzabbix_agent_package
  yum: name=` item ` state=present
  with_items:
  - zabbix22-agent-2.2.18-1.el6.x86_64
  - zabbix22-2.2.18-1.el6.x86_64
  when: ansible_os_family == "RedHat"
  - name: apt-get install to ubuntu
  apt: name=` item ` state=present
  with_items:
  - zabbix-agent
  when: ansible_os_family == "Debian"
  
  /etc/ansible/zabbix-agent/tasks/service.yml   开启脚本 万无一失2重开启
  
  # cat tasks/service.yml
  ---
  - name: start zabbix-agentd
  service: name=zabbix-agentd state=started
  when: ansible_os_family == "RedHat"
  - name: start ubuntu zabbix-agent
  service: name=zabbix-agent state=started
  when: ansible_os_family == "Debian"
  /etc/ansible/zabbix-agent/templates/zabbix_agentd.conf配置文件的模板放在这里
  
  
  # cat templates/zabbix_agentd.conf
  PidFile=/var/run/zabbix/zabbix_agentd.pid
  LogFile=/var/log/zabbix/zabbix_agentd.log
  LogFileSize=100
  Server=192.168.1.100#zabbix 服务端所在的 ip
  ServerActive=192.168.1.100# zabbix-agent 主动模式 配置
  Hostname=` ansible_hostname `#这个是系统变量
  Include=/etc/zabbix/conf.d/
  UnsafeUserParameters=1
  # UserParameter自定义的 监控项
  具体执行
  # pwd
  /etc/ansible
  # ansible-playbook zabbix-agent.yml
  # ansible-playbook zabbix-agent.yml   -vvvv
  加-vvv可以看到更详细的过程其实是 没必要的
页: [1]
查看完整版本: ansible自动部署 zabbix-agent 的模块