# Ansible playbook to delete a AAAA record from IPAM
#
# Example usage: ansible-playbook delete-ipam-aaaa.yml --extra-vars \
#  '{"fqdn":"ansibletest.sandbox.illinois.edu","ipv6addr":"2001:db8::17"}'
#
# Copyright (c) 2018 Board of Trustees University of Illinois
---
- import_playbook: ipam-provider.yml

- name: Delete IPAM AAAA record
  hosts: localhost
  gather_facts: false
  vars:

    view: default
    fqdn:
    ipv6addr:

  tasks:

    - assert: { that: "ansible_version.full is version('2.8', '>=')", quiet: true }

    - name: check required variables
      fail: msg="Required variable '{{ item }}' not set"
      when: not lookup('vars', item)
      loop:
        - fqdn
        - ipv6addr

    - name: get existing record if any
      set_fact:
        # may return list or single object
        existing_record: "{{ lookup('nios', 'record:aaaa', filter={'view': view, 'name': fqdn, 'ipv6addr': ipv6addr},
                                    return_fields=['view', 'name', 'ipv6addr', 'ttl', 'use_ttl', 'extattrs', 'comment'],
                                    provider=ipam_provider) }}"

    - debug: var=existing_record
      when: DEBUG is defined

    - name: delete AAAA record
      when: existing_record
      nios_aaaa_record:
        view: "{{ view }}"
        name: "{{ fqdn }}"
        ipv6addr: "{{ ipv6addr }}"
        state: absent
        provider: "{{ ipam_provider }}"
