-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    - 
      name: "Install Kayak and TFTP"
      pkg5: 
        name: 
          - kayak
          - kayak-kernel
          - tftp
          - isc-dhcp

    -
      name: "Checking if Image has  been downloaded"
      register: file_exists
      ansible.builtin.stat:
        path: "/var/kayak/kayak/{{ omnios_source_file }}"
        get_md5: false

    - 
      name: "Downloading PXE Boot Installer Image"
      ansible.builtin.get_url: 
        dest: "/var/kayak/kayak/{{ omnios_source_file }}"
        mode: "0644"
        url: "{{ omnios_source_url }}"
      when: not file_exists.stat.exists 

    - 
      name: "Enable and start Kayak"
      ansible.builtin.service: 
        name: kayak
        state: started

    - 
      name: "Enable and start TFTP"
      ansible.builtin.service: 
        name: tftp/udp6
        state: started

    - 
      name: "Convert Mac Address to extended format"
      ansible.builtin.set_fact: 
        modified_macaddr: "{{ '{}{}:{}{}:{}{}:{}{}:{}{}:{}{}'.format(*pxe_client_mac) }}"

    - 
      become: true
      name: "Setting DHCP Settings"
      ansible.builtin.template: 
        dest: "{{item.dest}}"
        mode: "0755"
        src: "{{item.src}}"
      with_items: 
        - 
          dest: /etc/dhcpd.conf
          src: dhcpd.conf

    - 
      name: "Enable and start DHCP"
      ansible.builtin.service: 
        name: "dhcp:ipv4"
        state: started

    - 
      become: true
      name: "Setting Boot Configurations for Kayak and TFTP"
      ansible.builtin.template: 
        dest: "{{item.dest}}"
        mode: "0755"
        src: "{{item.src}}"
      with_items: 
        - 
          dest: /tftpboot/boot/loader.conf.local
          src: loader.conf.local
        - 
          dest: /tftpboot/boot/grub/menu.lst
          src: menu.lst

    -
      name: "Create the XML Storage Directory"
      ansible.builtin.file: 
        path: /var/kayak/kayak/xml
        state: directory

    -
      name: "Create the XML Storage Directory"
      ansible.builtin.file: 
        path: "{{ item }}"
        state: directory
      loop:
        - /opt/promimic
        - /opt/promimic/scripts

    - 
      become: true
      name: "Setting up XML2AI"
      ansible.builtin.template: 
        dest: "{{item.dest}}"
        mode: a+x
        src: "{{item.src}}"
      with_items: 
        - 
          dest: /opt/promimic/scripts/xml2ai
          src: xml2ai

    - 
      become: true
      name: "Generating the XML from {{ pxe_profile }}hosttemplate.xml"
      ansible.builtin.template: 
        dest: "{{item.dest}}"
        mode: "0644"
        src: "{{item.src}}"
      with_items: 
        - 
          dest: "/var/kayak/kayak/xml/{{ pxe_client_hostname }}.xml"
          src: "{{ pxe_profile }}hosttemplate.xml"


    - 
      become: true
      name: "Placing /var/kayak/kayak/xml/machineschema.dtd"
      ansible.builtin.template: 
        dest: "{{item.dest}}"
        mode: "0644"
        src: "{{item.src}}"
      with_items: 
        - 
          dest: "/var/kayak/kayak/xml/machineschema.dtd"
          src: "machineschema.dtd"

    -
      name: "Adding Path to System Profile"
      ansible.builtin.lineinfile:
        mode: '0644'
        path: "/etc/profile"
        line: "PATH=$PATH:"
        insertbefore: EOF
      loop:
        - "PATH=$PATH:{{ profile_path }}"
        - "export PATH"

    - 
      become: true
      name: "Generating Kayak Configurations with  XML2AI with {{  pxe_client_hostname }}.xml"
      ansible.builtin.shell: 
        cmd: "pfexec python3 {{ pxe_script_generator }} < /var/kayak/kayak/xml/{{  pxe_client_hostname }}.xml"
