Ansible

ansible 註冊變數輸出未定義的變數

  • January 2, 2022

我正在嘗試做這本劇本半天。我正在使用 stat 模組檢查文件的 sha1sum,如果它不等於第二個文件,它應該替換正確的文件。但是在註冊一些變數時,輸出顯示變數未定義我做錯了什麼?

---
- 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

您缺少將第二個統計數據定義為任務的連字元。

---
- 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

請注意,您還缺少remote_src: yes複製任務中的參數。沒有它,Ansible 假定文件在您的本地機器上,而不是遠端主機上。

引用自:https://serverfault.com/questions/1088791