项目作者: tristanlt

项目描述 :
Zabinventory is an external Ansible inventory script. It request Zabbix API to retrieve hosts, hostgroups and hosts inventories.
高级语言: Python
项目地址: git://github.com/tristanlt/zabinventory.git
创建时间: 2018-10-31T13:52:21Z
项目社区:https://github.com/tristanlt/zabinventory

开源协议:GNU General Public License v3.0

下载


zabinventory : Zabbix Ansible inventory script

Zabinventory is an external Ansible inventory script. It request Zabbix API to retrieve hosts, hostgroups and hosts inventories.

It permit to use Zabbix Hostgroups with Ansible

  1. ansible-playbook playbooks/ntp.yaml zabbixdefinedgroup

Play with Zabbix inventory variables…

  1. ansible cercloud-mfs -m debug -a "var=[date_hw_expiry]" --limit br156-156

Installation

Zabinventory is a Python script. It requires some extra-modules.

  1. pip install pandas
  2. pip install ansible_vault
  3. pip install config_parser

Or more simply, use requirements.txt from repo :

  1. pip install -r requirements.txt

Create your inventory folder and place zabinventory inside it :

  1. inventory/
  2. ├── group_vars
  3. ├── all
  4. ├── hadoop-hdplops1
  5. ├── hadoop-hdplops1-hbase-regionserver
  6. └── IUEM-DC203-server
  7. └── zabinventory

If used, group_vars should be in the same directory as zabinventory.

Zabbix User creation

On the Zabbix user interface, create a user, this user will connect to the API to retrieve information. This user must be allowed to read all hostgroups that you want to configure with Ansible. It doesn’t need frontend access.

Be sure that Zabbix User is granted to read hostgroups you want to manage with Ansible.

Configuration

Zabinventory respect Ansible configuration location rules. It will try to find configuration in this order :

  • $ANSIBLE_CONFIG
  • ./ansible.cfg
  • ~/.ansible.cfg
  • /etc/ansible/ansible.cfg

Edit or create your ansible.cfg :

  1. [defaults]
  2. inventory = ./inventory/zabinventory
  3. [zabbix_api_params]
  4. zabbix_api_url = https://zabbix.server.tld/zabbix/api_jsonrpc.php
  5. zabbix_api_username = username
  6. zabbix_api_password = passw0rd

Test

Inside folder which contains ansible.cfg file, simply run zabinventory script :

  1. ./inventory/zabinventory

It should return complete list of monitored hosts, hostgroups and inventory variables (inside _meta).

(optional) Protect Zabbix credentials with Ansible Vault

It is a good idea to protect your Zabbix credentials with Ansible Vault.

Zabinventory can read one to all parameters from an encrypted file. Unfortunatly, Ansible inventory script wasn’t interactive, you must use a password file.

Inside your ansible configuration, define password file :

  1. [defaults]
  2. vault_password_file = ansible_vault_pass.txt

Next, you can create an encrypted configuration with :

  1. ansible-vault edit ./inventory/zabbix-api-params.yaml

Configure Zabinventory ( yaml !) :

  1. ---
  2. zabbix_api_url: https://zabbix.server.tld/zabbix/api_jsonrpc.php
  3. zabbix_api_username: username
  4. zabbix_api_password: passw0rd

Finally, edit ansible configuration, zabbix_api_params section, to point encrypted configuration :

  1. [zabbix_api_params]
  2. secured_yaml = ./inventory/zabbix-api-params.yaml

Contribute

Feel free to fork, submit issues, PR, offence or thanks.

Current state is “that works for us”, if Zabinventory work for you to, feel free to notify me on Twitter @tristanlt.