---

- name: 'apply setup to {{ host | default("all") }}'
  hosts: '{{ host | default("all") }}'
  serial: "{{ serial_number | default(5) }}"
  gather_facts: no
  become: no

  pre_tasks:
    - name: "Checking ansible version is at least 2.10.x"
      assert:
        that:
          - ansible_version.major >= 2
          - ansible_version.minor >= 10
        msg: "The ansible version has to be at least ({{ ansible_version.full }})"
      tags:
        - always

    - name: "Getting all firewalls from hetzner"
      uri:
        url: "https://api.hetzner.cloud/v1/firewalls"
        headers:
          accept: application/json
          authorization: Bearer {{ hetzner_authentication_ansible }}
        return_content: yes
      register: hetzner_firewalls_response
      delegate_to: 127.0.0.1
      tags:
        - update_networks

    - name: "Saving firewall entries as fact: hetzner_firewalls_response_json"
      set_fact: 
        hetzner_firewalls_response_json: "{{ hetzner_firewalls_response.json }}"
      tags:
        - update_networks

    - name: Parse firewall entries
      set_fact:  
        firewall_records: "{{ hetzner_firewalls_response_json.firewalls | json_query(jmesquery) }}"
      vars:
        jmesquery: '[*].{id: id, name: name}'
      tags:
        - update_networks

    - name: "Printing firewall entries"
      debug:
        msg: "{{ firewall_records }}"
      delegate_to: 127.0.0.1
      when:
       - debug
      tags:
        - update_networks

  roles:
    - role: hcloud
      when:
        - "'hcloud' in group_names"

    - role: hetzner_state
      vars:
        hetzner_state: 'started'
      when:
        - "'hcloud' in group_names"
  tasks:
    - name: "Create server in DO-cloud via include_tasks"
      include_role:
        name: sma_digitalocean
        tasks_from: _create_server
      vars:
        droplet:
          name: dev-blackbox-01
          tags:
            - service_blackbox
            - stage_dev
      when: "'digitalocean' in group_names"
