- name: Set user configuration from environment
  block:
    - name: Get admin username from environment
      ansible.builtin.set_fact:
        user_name: "{{ lookup('env', 'ADMIN_USER_NAME') }}"
        user_home: "/home/{{ lookup('env', 'ADMIN_USER_NAME') }}"
      delegate_to: localhost
      run_once: true

- name: User Creation and Sudo Configuration
  block:
    - name: Create new user and set shell for '{{ user_name }}'
      ansible.builtin.user:
        name: "{{ user_name }}"
        state: present
        shell: /usr/bin/bash

    - name: Add to sudo group new user '{{ user_name }}'
      ansible.builtin.user:
        name: "{{ user_name }}"
        groups: [sudo]
        append: true

    - name: Configure sudoers (NOPASSWD) for '{{ user_name }}'
      ansible.builtin.lineinfile:
        path: /etc/sudoers.d/010_sudo_user
        line: "{{ user_name }} ALL=(ALL) NOPASSWD: ALL"
        create: true
        mode: "0440"
        validate: "/usr/sbin/visudo -cf %s"
