2017-04-18 4 views
-1

ローカルディレクトリ内のファイルのリストに基づいてチェックサムを実行したいと考えています。次に、これらのファイルのチェックサムを取得し、それをリモートシステム上の同じファイルのチェックサムと比較することができます。Ansable:sha1チェックサムのファイルのローカルおよびリモートセットを確認する方法

私は、次の

# Local File 
- stat: 
    path: "{{ playbook_dir }}/roles/common/files/myfile.dat" 
    checksum_algorithm: sha1 
    delegate_to: localhost 
    run_once: true 
    register: localsha_result 

# Remote file 
- stat: 
    path: "{{ rmt_dest_dir }}/myfile.dat" 
    checksum_algorithm: sha1 
    register: sha_result 

を得ることができます知っていると私は私がチェックサムにしたいファイルをループにしようとしている:あなたは、ちょうど2つでこれを行うことができ

# Gather Files 
- name: gather names of files 
    local_action: shell ls {{ playbook_dir }}/roles/common/files/*.dat | awk -F '/' '{ print $NF }' 
    register: datfiles 

# Local File 
- stat: 
    path: "{{ playbook_dir }}/roles/common/files/{{ item }}" 
    checksum_algorithm: sha1 
    with_items: "{{ datfiles.stdout_lines }}" 
    delegate_to: localhost 
    run_once: true 
    register: localsha_result 

# Remote file 
- stat: 
    path: "{{ rmt_dest_dir }}/{{ item }}" 
    checksum_algorithm: sha1 
    with_items: "{{ datfiles.stdout_lines }}" 
    register: sha_result 

- name: check sha1 
    fail: msg="SHA1 checksum fails" 
    when: not sha_result.stat.checksum is defined or not sha_result.stat.checksum == "{{ item.stat.checksum }}" 
with_items: "{{ datfiles.stdout_lines}}" 

答えて

1

タスク:(1)ローカルチェックサムを登録する、(2)リモートチェックサムをチェックして対応するローカルと比較する:

--- 
- hosts: test-server 
    tasks: 
    - stat: 
     path: "{{ item }}" 
     checksum_algorithm: sha1 
     delegate_to: localhost 
     with_fileglob: /tmp/*.dat 
     register: local_files 
    - stat: 
     path: "/tmp/{{ item.stat.path | basename }}" 
     checksum_algorithm: sha1 
     failed_when: remote_files.stat.checksum != item.stat.checksum 
     # failed_when condition checked after every iteration 
     # and remote_files here is a result of individual task 
     # but after loop is finished, remote_files is a cobination 
     # of all iterations results 
     with_items: "{{ local_files.results }}" 
     register: remote_files 
     loop_control: 
     label: "{{ item.stat.path | basename }}" 
+0

魅力! 'failed_when'は非常に役に立ちます。 ありがとうございます! – Cale

関連する問題