---
# -------------------------------------------------------------
# comments

- name: Add rsa key with existing comment
  ansible.posix.authorized_key:
    user: root
    key: "{{ rsa_key_basic }}"
    state: present
    path: "{{ output_dir | expanduser }}/authorized_keys"
  register: result

- name: Change the comment on an existing key
  ansible.posix.authorized_key:
    user: root
    key: "{{ rsa_key_basic }}"
    comment: user@acme.com
    state: present
    path: "{{ output_dir | expanduser }}/authorized_keys"
  register: result

- name: Get the file content
  ansible.builtin.command: fgrep DATA_BASIC "{{ output_dir | expanduser }}/authorized_keys"
  changed_when: false
  register: content

- name: Assert that comment on an existing key was changed
  ansible.builtin.assert:
    that:
      - "'user@acme.com' in content.stdout"

- name: Set the same key with comment to ensure no changes are reported
  ansible.posix.authorized_key:
    user: root
    key: "{{ rsa_key_basic }}"
    comment: user@acme.com
    state: present
    path: "{{ output_dir | expanduser }}/authorized_keys"
  register: result

- name: Assert that no changes were made when running again
  ansible.builtin.assert:
    that:
      - not result.changed

- name: Debug the result and content
  ansible.builtin.debug:
    var: "{{ item }}"
    verbosity: 1
  with_items:
    - result
    - content
