'How to save Ansible register var as a file in the Ansible server

I would like to save the Ansible debug output to a file in the Ansible server. For example: I would like to save cat authorized_keys command output to a file in the Ansible server.

- name: Check authorized keys
  hosts: all
  remote_user: ansible
  become: true

  tasks:
  - name: cat /home/my_server_user/.ssh/authorized_keys
    ansible.builtin.command: "cat /home/my_server_user/.ssh/authorized_keys"
    register: hostname_authorized_keys_result_my_server_user
    notify:
    - my server user

  handlers:
  - name: Step - 1
    ansible.builtin.debug:
      var: hostname_authorized_keys_result_my_server_user.stdout_lines
    listen: "my server user"

  - name: Step - 2
    ansible.builtin.copy:
      content: "{{ hostname_authorized_keys_result_my_server_user.stdout_lines }}"
      dest: /tmp/audit.file
    listen: "my server user"

But my handler creates a /tmp/audit.file and save the output in my client local, not in the Ansible server.

I am very new to Ansible, any help is appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source