Browse Source

improve verify file and add simple filter capabilities

main
Markus Bergholz 1 month ago
parent
commit
8e5258527e
Signed by: m GPG Key ID: B45724801354B174
  1. 15
      CHANGELOG.md
  2. 13
      README.md
  3. 2
      galaxy.yml
  4. 18
      plugins/inventory/hetzner_dns.py

15
CHANGELOG.md

@ -1,3 +1,18 @@
# 1.6.0
* Add dynamic inventory for hetzner dns
```yml
plugin: markuman.hetzner_dns.hetzner_dns
zone_name: osuv.de
filters:
type:
- A
- CNAME
```
`ansible-inventory -i osuv.hetzner_dns.yml --list`
# 1.5.0
* fetch only requested `zone_name`

13
README.md

@ -185,6 +185,19 @@ Can be used with
`ansible-inventory -i osuv.hetzner_dns.yml --list` or
`ansible-playbook -i osuv.hetzner_dns.yml some_tasks.yml`
Supported filters are
```yml
plugin: markuman.hetzner_dns.hetzner_dns
zone_name: osuv.de
filters:
type:
- A
- CNAME
```
The default type filter is set to `['A', 'AAAA', 'CNAME']`
### SCM
| **host** | **category** |

2
galaxy.yml

@ -1,7 +1,7 @@
namespace: "markuman"
description: hetzner dns collection
name: "hetzner_dns"
version: "1.5.0"
version: "1.6.0"
readme: "README.md"
authors:
- "Markus Bergholz <markuman+spambelongstogoogle@gmail.com>"

18
plugins/inventory/hetzner_dns.py

@ -23,6 +23,12 @@ DOCUMENTATION = '''
description: api token. if not set, it is read from env HETZNER_DNS_TOKEN
required: False
type: string
filters:
description:
- A dictionary of filter value pairs.
type: dict
default: {}
required: False
'''
class InventoryModule(BaseInventoryPlugin):
@ -34,7 +40,12 @@ class InventoryModule(BaseInventoryPlugin):
self.api_token = self.get_option('api_token') or os.environ.get('HETZNER_DNS_TOKEN')
def verify_file(self, path):
return True
valid = False
if super(InventoryModule, self).verify_file(path):
# base class verifies that file exists and is readable by current user
if path.endswith(('hetzner_dns.yaml', 'hetzner_dns.yml')):
valid = True
return valid
def parse(self, inventory, loader, path, cache=False):
@ -51,9 +62,12 @@ class InventoryModule(BaseInventoryPlugin):
records = dns.get_record_info(zone_id).json()['records']
filters = self.get_option('filters')
filter_types = filters.get('type') or ['A', 'AAAA', 'CNAME']
#parse data and create inventory objects:
for item in records:
if item.get('type') in ['A', 'AAAA', 'CNAME']:
if item.get('type') in filter_types:
name = item.get('name') + '.' + zone_name
self.inventory.add_host(name)
self.inventory.set_variable(name, 'ansible_host', item.get('value'))

Loading…
Cancel
Save