---
# install_vault.yml
- name: Install HashiCorp Vault
  hosts: vault_servers
  become: yes
  tasks:
    - name: Check if Vault is already installed
      command: which vault
      register: vault_check
      ignore_errors: yes
      changed_when: false

    - name: Install Vault using apt
      apt:
        name: vault
        state: present
        update_cache: yes
      when: vault_check.rc != 0

    - name: Create Vault data directory
      file:
        path: "{{ vault_data_dir | default('/opt/nomad/data/vault/config') }}"
        state: directory
        owner: root
        group: root
        mode: '0755'
        recurse: yes

    - name: Verify Vault installation
      command: vault --version
      register: vault_version
      changed_when: false

    - name: Display Vault version
      debug:
        var: vault_version.stdout