Score:0

ansible registering variable outputs undefined variable

bo flag

I'm trying to do this playbook for half a day. I'm using stat module to check sha1sum of file and if it is not equal to second file it should replace correct file. But while registering some variables the output says that variables is undefined What am I doing wrong ?

---
- 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
      when: sum.stat.checksum != sum2.stat.checksum
br flag
The two *stat* tasks are redundant. The module [*copy*](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#ansible-builtin-copy-copy-files-to-remote-locations) compares the *checksum* of the *src* and *dest* by default. Quoting from [*checksum*](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#parameter-checksum): ``'... If this is not provided, ansible will use the local calculated checksum of the src file.'``
Score:1
in flag

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.

Zeitounator avatar
fr flag
Please note that all those checks are totally useless since ansible will only copy the file if source (local or remote) and destination are actually different. Otherwise it will simply report 'OK'.
bo flag
Thank You Gerald You made my day, now it works proper. It was so simple.
in flag
Great, but heed the advice of Zeitounator and Vladimir. These checks ARE superfluous, Ansible will do the same checks again in the `copy` task.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.