---
-
  name: "Setting up Bridged Networking"
  when: 'ansible_virtualization_type == "virtualbox"'
  become: true
  block:
    -
      name: "Setting up Bridged Networking"
      when: networks[0] is defined
      become: true
      block:
        -
          name: "Extracting MAC address of bridged adapter"
          ansible.builtin.shell: lshw -xml -c net | xmllint --xpath "string(//node[@id='network']/serial|//node[@id='network:1']/serial)" -
          args:
            executable: /bin/bash
          register: macaddress

        -
          name: "Placing Netplan into Working directory"
          ansible.builtin.template:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: '0700'
          loop:
            - { src: 'interface.yaml.j2', dest: '/etc/netplan/public_adapter.yaml' }

        -
          name: "Setting Networking"
          ansible.builtin.command: "{{ item }}"
          with_items:
            - "netplan apply"

        -
          name: "Waiting for 3 seconds for IP to set"
          ansible.builtin.pause:
            seconds: 3

        -
          name: "Grabbing External IP address"
          ansible.builtin.shell: >
            lshw -xml -c net |
            xmllint --xpath
            "string(//node[@id='network:1']/configuration/setting[@id='ip']/@value|//node[@id='network']/configuration/setting[@id='ip']/@value)" -
          args:
            executable: /bin/bash
          register: netoutput

        -
          name: "Creating file ipaddress.yml with contents of: {{ netoutput.stdout }}"
          ansible.builtin.copy:
            content: "{{ netoutput.stdout }}"
            dest: "{{ completed_dir }}/ipaddress.yml"
            mode: '0644'
    -
      name: "Setting up Bridged Networking"
      when: networks[0] is not defined
      become: true
      block:
        -
          name: "Placing 127.0.0.1 in file ipaddress.yml as no IP was configured in Hosts.yml into {{ completed_dir }}"
          ansible.builtin.copy:
            content: "127.0.0.1"
            dest: "{{ completed_dir }}/ipaddress.yml"
            mode: '0644'

        -
          name: "Grabbing local address for scripts"
          become: true
          ansible.builtin.shell: "cat {{ completed_dir }}/ipaddress.yml"
          args:
            executable: /bin/bash
          register: netoutput
