---
- name: Create Prometheus User and Group
  hosts: all,!otsrvcibernetica  # Specify the hosts where you want to create the user and group
  become: yes  # Run tasks with elevated privileges (sudo)

  tasks:
    - name: Check if prometheus group exists
      ansible.builtin.group:
        name: prometheus
      register: prometheus_group_check
      ignore_errors: yes  # Ignore errors if the group doesn't exist

    - name: Create prometheus group if it doesn't exist
      ansible.builtin.group:
        name: prometheus
        state: present
      when: prometheus_group_check.failed  # Create group only if it doesn't exist

    - name: Check if prometheus user exists
      ansible.builtin.user:
        name: prometheus
      register: prometheus_user_check
      ignore_errors: yes  # Ignore errors if the user doesn't exist

    - name: Create prometheus user if it doesn't exist
      ansible.builtin.user:
        name: prometheus
        group: prometheus
        shell: /bin/bash
        home: /home/prometheus
        createhome: yes
        state: present
      when: prometheus_user_check.failed  # Create user only if it doesn't exist

    - name: Ensure prometheus user is part of the prometheus group
      ansible.builtin.user:
        name: prometheus
        groups: prometheus
        append: yes
      # when: "'prometheus' not in prometheus_user_check.groups"  # Add user to group if not already a member
