前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ansible-playbook调用zabbix-api自动添加主机

ansible-playbook调用zabbix-api自动添加主机

作者头像
shaonbean
发布2019-05-26 09:23:37
1.1K0
发布2019-05-26 09:23:37
举报
文章被收录于专栏:运维前线运维前线

版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。 https://cloud.tencent.com/developer/article/1434637

ansible-playbook调用zabbix-api自动添加主机

  1. playbook-example
代码语言:javascript
复制
# example.yml
---
- hosts: "{{ host }}"
  vars:
    zabbix_host_groups: "{{ host }}"

    zabbix_url: "http://zabbix.example.com"
    zabbix_api_user: Admin
    zabbix_api_pass: admin
    zabbix_api_create_hostgroup: True
    zabbix_api_create_hosts: True
    zabbix_create_hostgroup: present  # or absent
    zabbix_create_host: present       # or absent
    zabbix_host_status: enabled  
    zabbix_agent_hostname: "{{ ansible_hostname }}"
    zabbix_proxy: null
    zabbix_inventory_mode: disabled
    zabbix_useuip: 1
    zabbix_agent_service: zabbix-agent
    dns_name: null

    zabbix_link_templates:
      - Template OS Linux Active
                                                                                                              
    zabbix_agent_interfaces:
      - type: 1
        main: 1
        useip: "{{ zabbix_useuip }}"
        ip: 0.0.0.0
        dns: ""
        port: 0

  tasks:
  - name: "Make sure the zabbix-agent service is running"
    service:
      name: "{{ zabbix_agent_service }}"
      state: started
      enabled: yes
    tags:
      - service
  
  - name: "Create hostgroups"
    local_action:
      module: zabbix_group
      server_url: "{{ zabbix_url }}"
      login_user: "{{ zabbix_api_user }}"
      login_password: "{{ zabbix_api_pass }}"
      host_group: "{{ zabbix_host_groups }}"
      state: "{{ zabbix_create_hostgroup }}"
    when:
      - zabbix_api_create_hostgroup
    tags:
      - api
  
  - name: "Create a new host or update an existing host's info"
    local_action:
      module: zabbix_host
      server_url: "{{ zabbix_url }}"
      login_user: "{{ zabbix_api_user }}"
      login_password: "{{ zabbix_api_pass }}"
      host_name: "{{ zabbix_agent_hostname }}"
      host_groups: "{{ zabbix_host_groups }}"
      link_templates: "{{ zabbix_link_templates }}"
      status: "{{ zabbix_host_status }}"
      state: "{{ zabbix_create_host }}"
      proxy: "{{ zabbix_proxy }}"
      inventory_mode: "{{ zabbix_inventory_mode }}"
      interfaces: "{{ zabbix_agent_interfaces }}"
      visible_name: "{{ zabbix_host_groups }}_{{ inventory_hostname }}"
    when:
      - zabbix_api_create_hosts
    changed_when: false
    tags:
      - api
  • 执行playbook
代码语言:javascript
复制
ansible-playbook -i hosts auto_add_hosts_to_zabbix_server.yml -e host=appname

注意:笔者的zabbix-agent是主动模式,如果被动模式需更改: zabbix_agent_interfaces:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年09月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ansible-playbook调用zabbix-api自动添加主机
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档