- name: "Add PostgreSQL group"
  lineinfile:
    dest: /etc/group
    line: "postgres:*:770:"
    regexp: "^postgres:"
  notify: pwd_mkdb

- name: "Add PostgreSQL user"
  lineinfile:
    dest: /etc/master.passwd
    line: "postgres:*:770:770::0:0:PostgreSQL Daemon:/var/db/postgres:/bin/sh"
    regexp: "^postgres:"
  notify: pwd_mkdb

# Workaround for "pw: user '...' disappeared during update"
- name: "Flush handlers"
  meta: flush_handlers

- name: "Create home directory for PostgreSQL"
  file:
    path: "{{ item }}"
    state: directory
    mode: "0755"
    owner: postgres
    group: postgres
  with_items:
    - /var/db/postgres
    - /var/db/postgres/.ssh

- name: "Create ssh identity for PostgreSQL"
  openssh_keypair:
    path: /var/db/postgres/.ssh/id_ed25519
    comment: "postgresql@{{ ansible_hostname }}"
    type: ed25519
    owner: postgres
    group: postgres

- name: "Add PostgreSQL group"
  lineinfile:
    dest: /etc/group
    line: "postgres:*:770:"
    regexp: "^postgres:"
  notify: pwd_mkdb

- name: "Add PostgreSQL user"
  lineinfile:
    dest: /etc/master.passwd
    line: "postgres:*:770:770::0:0:PostgreSQL Daemon:/var/db/postgres:/bin/sh"
    regexp: "^postgres:"
  notify: pwd_mkdb

# Workaround for "pw: user '...' disappeared during update"
- name: "Flush handlers"
  meta: flush_handlers

- name: "Create home directory for PostgreSQL"
  file:
    path: "{{ item }}"
    state: directory
    mode: "0755"
    owner: postgres
    group: postgres
  with_items:
    - /var/db/postgres
    - /var/db/postgres/.ssh

- name: "Create ssh identity for PostgreSQL"
  openssh_keypair:
    path: /var/db/postgres/.ssh/id_ed25519
    comment: "postgres@{{ ansible_hostname }}"
    type: ed25519
    owner: postgres
    group: postgres

Fix username in SSH key comment (postgres)
Fix username in SSH key comment (postgres)
