---
# Sync and update Pacman
- name: Sync and update Pacman
  community.general.pacman:
    update_cache: yes
    upgrade: yes
  become: yes
  tags: update
# Install Pacman developer tools
- name: Ensure common developer tools installed
  community.general.pacman:
    name: 
    - vim
    - wine
    - winetricks
    - wine-mono
    - wine_gecko
    - iputils
    - dnsutils
    - iperf3
    - python
    - python-pip
    - grub
    - glances
    - htop
    - curl
    - wget
    - dconf-editor
    - unzip
    - avahi # bonjour service
  become: yes
# Ensure avahi service started
- name: Ensure avahi-daemon service running
  ansible.builtin.systemd:
    name: avahi-daemon.service
    state: started
    enabled: yes
  become: yes
# Install snap developer tools
- name: Ensure snap installed
  community.general.pacman:
    name: snapd
    state: present
  become: yes
- name: Ensure snapd service running
  ansible.builtin.systemd:
    name: snapd.socket
    state: started
    enabled: yes
  become: yes
- name: Link /var/lib/snapd/snap /snap
  ansible.builtin.file:
    src: /var/lib/snapd/snap
    dest: /snap
    owner: root
    group: root
    state: link
  become: yes
- name: Install snap packages
  community.general.snap:
    name: 
    - yq
    - code
    state: present
  become: yes
  retries: 6
  delay: 10
- name: Install classic snap packages
  community.general.snap:
    name: 
    - kubectl
    - helm
    state: present
    classic: yes
  become: yes
  retries: 6
  delay: 10
# AWS CLI
- name: Download awscli v2 installer - latest version
  unarchive:
    src: https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip
    dest: '{{ executable_temp_dir }}'
    remote_src: true
    creates: '{{ executable_temp_dir }}/aws'
    mode: 0755
  when: awscli_version is not defined
  tags: ["awscli", "awscliv2"]
- name: Download awscli v2 installer - specific version {{ awscli_version }}
  unarchive:
    src: 'https://awscli.amazonaws.com/awscli-exe-linux-x86_64-{{ awscli_version }}.zip'
    dest: "{{ executable_temp_dir }}"
    remote_src: true
    creates: '{{ executable_temp_dir }}/aws'
    mode: 0755
  when: awscli_version is defined
  tags: ["awscli", "awscliv2"]
- name: Run the installer for awscli v2
  command:
  args:
    cmd: '{{ executable_temp_dir }}/aws/install -i {{ awscli_install_dir }} -b /usr/local/bin'
    creates: /usr/local/bin/aws
  become: yes
  tags: ["awscli", "awscliv2"]
# Ansible
- name: Install ansible
  pip:
    name: ansible
    extra_args: --user
# Docker
- name: Ensure docker installed
  community.general.pacman:
    name:
      - docker
      - docker-compose
    state: present
  become: yes
- name: Ensure ducoterra in the "docker" group
  ansible.builtin.user:
    name: ducoterra
    groups: docker
    append: yes
  become: yes
# Vault CLI
- name: Download vault binary
  unarchive:
    src: https://releases.hashicorp.com/vault/{{vault_version}}/vault_{{vault_version}}_linux_amd64.zip
    dest: '{{ executable_temp_dir }}'
    remote_src: true
    creates: '{{ executable_temp_dir }}/vault'
    mode: 0755
- name: Move vault binary to /usr/local/bin
  ansible.builtin.copy:
    src: '{{ executable_temp_dir }}/vault'
    dest: /usr/local/bin/vault
    owner: root
    group: root
    mode: '0755'
  become: yes
# Terraform CLI
- name: Download vault binary
  unarchive:
    src: https://releases.hashicorp.com/terraform/{{tf_version}}/terraform_{{tf_version}}_linux_amd64.zip
    dest: '{{ executable_temp_dir }}'
    remote_src: true
    creates: '{{ executable_temp_dir }}/terraform'
    mode: 0755
- name: Move terraform binary to /usr/local/bin
  ansible.builtin.copy:
    src: '{{ executable_temp_dir }}/terraform'
    dest: /usr/local/bin/terraform
    owner: root
    group: root
    mode: '0755'
  become: yes
# Packer CLI
- name: Download packer binary
  unarchive:
    src: https://releases.hashicorp.com/packer/{{packer_version}}/packer_{{packer_version}}_linux_amd64.zip
    dest: '{{ executable_temp_dir }}'
    remote_src: true
    creates: '{{ executable_temp_dir }}/packer'
    mode: 0755
- name: Move packer binary to /usr/local/bin
  ansible.builtin.copy:
    src: '{{ executable_temp_dir }}/packer'
    dest: /usr/local/bin/packer
    owner: root
    group: root
    mode: '0755'
  become: yes
# Wireguard Configuration
- name: Ensure wireguard-tools installed
  community.general.pacman:
    name:  wireguard-tools
    state: present
  become: yes
- name: Ensure /home/ducoterra/.wireguard directory
  file:
    path: /home/ducoterra/.wireguard
    state: directory
- name: Link /home/ducoterra/.wireguard /etc/wireguard
  ansible.builtin.file:
    src: /home/ducoterra/.wireguard
    dest: /etc/wireguard
    state: link
  become: yes
