---
- hosts: drupalvm
  gather_facts: no

  vars_files:
    - vars.yml

  pre_tasks:
    # See: https://github.com/geerlingguy/drupal-vm/issues/1245
    - name: Install Python if it's not available.
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
      register: output
      changed_when: output.stdout != ""
    - action: setup

  tasks:
    - name: Create admin user account.
      user:
        name: "{{ admin_user }}"
        createhome: yes
        home: "/home/{{ admin_user }}"
        generate_ssh_key: yes
        ssh_key_comment: "ansible-{{ inventory_hostname }}"
        password: "{{ admin_password }}"
        groups: "{{ admin_group }}"
        shell: /bin/bash

    - name: Add local SSH public key to admin account authorized_keys.
      authorized_key:
        user: "{{ admin_user }}"
        key: "{{ lookup('file', admin_pubkey) }}"
        manage_dir: yes
      when: admin_copy_ssh_pubkey

    - name: Disable requiretty.
      lineinfile:
        dest: /etc/sudoers
        regexp: '^Defaults.+requiretty'
        line: 'Defaults    !requiretty'
        state: present