0

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 
1
  • 2
    The two stat tasks are redundant. The module copy compares the checksum of the src and dest by default. Quoting from checksum: '... If this is not provided, ansible will use the local calculated checksum of the src file.' Commented Jan 2, 2022 at 20:05

1 Answer 1

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.

3
  • 3
    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'. Commented Jan 2, 2022 at 20:01
  • Thank You Gerald You made my day, now it works proper. It was so simple. Commented Jan 2, 2022 at 20:34
  • Great, but heed the advice of Zeitounator and Vladimir. These checks ARE superfluous, Ansible will do the same checks again in the copy task. Commented Jan 3, 2022 at 6:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.