我正在尝试查看文件是否存在以及是否存在,是否与另一台服务器上的最新版本匹配。我正在使用stat
和file
模块来帮助实现此目的,但是我一直遇到以下问题:如果文件不存在,我无法以编程方式说出是否是因为文件版本错误或如果文件不在服务器上,那么我正在尝试设计一个代码,该代码可以判断该文件是错误版本还是不存在。
我意识到错误是当我尝试将两个语句添加到when
条件时。它说该变量未填充且为空。我不确定如何解决此问题。
代码:
- name: Check for latest version of EMC Grab is available on File server
stat:
path: /tmp/{{ emc_grab_version }}
register: Fileserver
delegate_to: "{{ delegate_host }}"
- debug:
msg: "{{ Fileserver.stat.exists }}"
# See if it exists on the target host
- name: Check if latest version of EMC Grab is available on target host
stat:
path: /tmp/{{ emc_grab_version }}
register: target_host_version
- debug:
msg: "{{ target_host_version.stat.exists }}"
# Deletes older version
- name: Runs if version mismatch (Find old version, delete, install new)
block:
- name: Find old version
find:
paths: /tmp
patterns: emcgrab_Linux
use_regex: yes
register: target_host_tar
- name: Remove old tar file
file:
path: /tmp/{{ target_tar_version }}
state: absent
- name: Coyping new tar file over (Old tar removed)
synchronize: src=/tmp/{{ emc_grab_version }} dest=/tmp
delegate_to: "{{ delegate_host }}"
become: yes
when:
- target_host_version.stat.exists == false
- emc_grab_version != "{{ target_tar_version }}"
# Runs if it never existed, the new version is copied over
- name: Copying tar file over (Never existed)
synchronize: src=/tmp/{{ tar_version }} dest=/tmp
delegate_to: {{ delegate_host }}
become: yes
品种:
delegate_host: 10.x.x.x
emc_grab_version: "emcgrab_Linux_v4.8.3.tar"
target_host_file: "{{ target_host_tar.files | first }}"
target_tar_version: "{{ target_host_file.path | basename }}"