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

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

  1. playbook-example
# 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
ansible-playbook -i hosts auto_add_hosts_to_zabbix_server.yml -e host=appname

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值