Формирование списка хостов для Ansible из Zabbix


/etc/ansible/get_hosts.py:

=== cut ===
from pyzabbix import ZabbixAPI
import logging
import codecs

logging.getLogger().setLevel(logging.DEBUG)
z_api = ZabbixAPI("http://zabbix.YOURDOMAIN.ru/")
z_api.login('USERNAME', 'PASSWORD')

with codecs.open('hosts_zabbix','w','utf-8') as config:
for group in z_api.hostgroup.get(output="extend"):
gid = (group['groupid'])
print("---------------- %s ------------" %group['name'])
config.write(u'[%s]\n' % group['name'])
for host in z_api.host.get(output="extend", selectInterfaces=True, groupids=[gid]):
hid = (host['hostid'])
for hi in z_api.hostinterface.get(hostids=[hid]):
print("name:%s interface:%s" % (host['name'],hi['ip']))
config.write(u'%s fullname=%s\n' % (hi['ip'],host['name']))
=== cut ===