ansible registering variable outputs undefined variable

Solution 1:

You are missing the hyphen that defines the second stat as a task.

---
- hosts: all
  remote_user: root
  tasks:
    - name: get sum of file
      stat:
        path: /home/roundcube/config.php
        checksum_algorithm: sha1
        get_checksum: yes
      register: sum      
      
    - stat:
        path: /home/archive/config.php
        checksum_algorithm: sha1
        get_checksum: yes
      register: sum2
      
    - name: result
      ansible.builtin.copy:
        src: /home/archive/config.php
        dest: /home/roundcube/config.php
        remote_src: yes
      when: sum.stat.checksum != sum2.stat.checksum

Note that you are also missing the remote_src: yes parameter in the copy task. Without it Ansible assumes that the file is on your local machine, not the remote host.