---
- name: Install Nomad by direct download from HashiCorp
  hosts: all
  become: yes
  vars:
    nomad_user: "nomad"
    nomad_group: "nomad"
    nomad_home: "/opt/nomad"
    nomad_data_dir: "/opt/nomad/data"
    nomad_config_dir: "/etc/nomad.d"
    nomad_datacenter: "dc1"
    nomad_region: "global"
    nomad_server_addresses:
      - "100.116.158.95:4647"  # semaphore server address

  tasks:
    - name: Create nomad user
      user:
        name: "{{ nomad_user }}"
        group: "{{ nomad_group }}"
        system: yes
        shell: /bin/false
        home: "{{ nomad_home }}"
        create_home: yes

    - name: Create nomad directories
      file:
        path: "{{ item }}"
        state: directory
        owner: "{{ nomad_user }}"
        group: "{{ nomad_group }}"
        mode: '0755'
      loop:
        - "{{ nomad_home }}"
        - "{{ nomad_data_dir }}"
        - "{{ nomad_config_dir }}"
        - /var/log/nomad

    - name: Install unzip package
      apt:
        name: unzip
        state: present
        update_cache: yes

    - name: Download Nomad binary
      get_url:
        url: "{{ nomad_url }}"
        dest: "/tmp/nomad_{{ nomad_version }}_linux_amd64.zip"
        mode: '0644'
        timeout: 300

    - name: Extract Nomad binary
      unarchive:
        src: "/tmp/nomad_{{ nomad_version }}_linux_amd64.zip"
        dest: /tmp
        remote_src: yes

    - name: Copy Nomad binary to /usr/local/bin
      copy:
        src: /tmp/nomad
        dest: /usr/local/bin/nomad
        mode: '0755'
        owner: root
        group: root
        remote_src: yes

    - name: Create Nomad client configuration
      template:
        src: templates/nomad-client.hcl.j2
        dest: "{{ nomad_config_dir }}/nomad.hcl"
        owner: "{{ nomad_user }}"
        group: "{{ nomad_group }}"
        mode: '0640'

    - name: Create Nomad systemd service
      copy:
        content: |
          [Unit]
          Description=Nomad
          Documentation=https://www.nomadproject.io/
          Requires=network-online.target
          After=network-online.target
          ConditionFileNotEmpty={{ nomad_config_dir }}/nomad.hcl

          [Service]
          Type=notify
          User={{ nomad_user }}
          Group={{ nomad_group }}
          ExecStart=/usr/local/bin/nomad agent -config={{ nomad_config_dir }}
          ExecReload=/bin/kill -HUP $MAINPID
          KillMode=process
          Restart=on-failure
          LimitNOFILE=65536

          [Install]
          WantedBy=multi-user.target
        dest: /etc/systemd/system/nomad.service
        mode: '0644'

    - name: Reload systemd daemon
      systemd:
        daemon_reload: yes

    - name: Enable and start Nomad service
      systemd:
        name: nomad
        enabled: yes
        state: started

    - name: Wait for Nomad to be ready
      wait_for:
        port: 4646
        host: localhost
        delay: 5
        timeout: 60

    - name: Verify Nomad installation
      command: /usr/local/bin/nomad version
      register: nomad_version_output

    - name: Display Nomad version
      debug:
        msg: "{{ nomad_version_output.stdout }}"

    - name: Clean up downloaded files
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - "/tmp/nomad_{{ nomad_version }}_linux_amd64.zip"
        - /tmp/nomad