Ansible:如何检查sha1校验和的本地和远程文件集

时间:2017-04-18 20:21:29

标签: loops ansible checksum stat

我希望能够根据本地目录中的文件列表进行校验和。然后能够获取这些文件校验和并将其与远程系统上相同文件的校验和进行比较。

我知道我可以通过以下方式获得

# 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

我试图通过以下方式遍历我想要校验和的文件:

# 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 个答案:

答案 0 :(得分:2)

您只需执行以下两项任务即可:(1)注册本地校验和,(2)检查远程校验和,将它们与相应的本地校验进行比较:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:background="@android:color/white">

        <LinearLayout
            android:id="@+id/layout_form"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginBottom="5dp"
                android:background="#88ffffff"/>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/list_prdt_detail"
                android:divider="@android:color/white"
                android:layout_marginBottom="50dp"
                android:scrollbars="vertical"
                android:nestedScrollingEnabled="true"/>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ADD TO CART"
            android:background="@android:color/holo_orange_light"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="20dp"
            android:textColor="@android:color/white"
            android:fontFamily="sans-serif"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BUY NOW"
            android:background="@android:color/holo_red_dark"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="20dp"
            android:textColor="@android:color/white"
            android:fontFamily="sans-serif"/>

    </LinearLayout>
</RelativeLayout>