2017-04-18 14 views
-1

로컬 디렉터리의 파일 목록을 기반으로 체크섬을 수행하려고합니다. 그런 다음 해당 파일의 체크섬을 가져 와서 원격 시스템의 동일한 파일의 체크섬과 비교할 수 있습니다. 당신은 두하여이 작업을 수행 할 수Ansable : sha1 체크섬에 대한 로컬 및 원격 파일 집합을 확인하는 방법

# 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

:

나는 나는 다음과 같은

# 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 

으로 얻을 수 알고 난으로 검사 할 파일을 통해 루프를 시도했다 작업 : (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