# See: http://docs.stackstorm.com/install/config.html#configure-ssh
---
- name: Create system user
  sudo: yes
  user:
    name: "{{ st2_system_user }}"
    home: "/home/{{ st2_system_user }}"
    generate_ssh_key: yes
    ssh_key_file: "{{ st2_ssh_key_file }}"
    state: present
  tags: [st2, user]
  register: _user

- name: Authorize public key for system user
  sudo: yes
  sudo_user: "{{ st2_system_user }}"
  authorized_key:
    user: "{{ st2_system_user }}"
    key: "{{ _user.ssh_public_key }}"
    state: present
  tags: [st2, user]

- name: Add system user to sudoers
  sudo: yes
  lineinfile:
    create: yes
    dest: /etc/sudoers.d/st2
    mode: 0440
    regexp: "^{{ st2_system_user }} ALL="
    line: "{{ st2_system_user }} ALL=(ALL) NOPASSWD: SETENV: ALL"
    state: present
    validate: 'visudo -cf %s'
  tags: [st2, user]

# See: http://docs.stackstorm.com/install/config.html#configure-ssh
---
- name: Create
  sudo: yes
  user:
    name: "{{ st2_system_user }}"
    home: "/home/{{ st2_system_user }}"
    generate_ssh_key: yes
    ssh_key_file: "{{ st2_ssh_key_file }}"
    state: present
  tags: [st2, user]
  register: _user

- name: Authorize key-based access
  sudo: yes
  sudo_user: "{{ st2_system_user }}"
  authorized_key:
    user: "{{ st2_system_user }}"
    key: "{{ _user.ssh_public_key }}"
    state: present
  tags: [st2, user]

- name: Add to sudoers
  sudo: yes
  lineinfile:
    create: yes
    dest: /etc/sudoers.d/st2
    mode: 0440
    regexp: "^{{ st2_system_user }} ALL="
    line: "{{ st2_system_user }} ALL=(ALL) NOPASSWD: SETENV: ALL"
    state: present
    validate: 'visudo -cf %s'
  tags: [st2, user]

Refactor st2 user creation role
Refactor st2 user creation role
