gpt4 book ai didi

Ansible:如何从列表中检查远程文件夹中是否存在多个文件

转载 作者:行者123 更新时间:2023-12-02 18:38:43 25 4
gpt4 key购买 nike

我正在尝试将列表中的一些文件(在我的变量文件中设置)从我的 Ansible 服务器(Oracle Linux 8.4 上的 Ansible 2.11.2)复制到远程主机(CentOS 7)。
现在我需要在复制后检查所有文件是否都正确复制,并检查每个文件的校验和。

我用于将文件从 Ansible 复制到远程主机的主要任务是:
任务/main.yum:

- name: Jenkins - Configuration | Copy plugin files
copy:
src: "files/plugins/{{item.vars_plugin_name}}"
dest: "{{ vars_jenkins_home }}/plugins/"
owner: jenkins
group: jenkins
loop: "{{ vars_plugin_files }}"
notify: restart-jenkins

这是我的 vars 文件:vars/main.yml

vars_plugin_files:
- vars_plugin_name: ansible.hpi
vars_plugin_checksum: 471787b86173cc6ef0a2243ec12f9a3419d1ac51
- vars_plugin_name: apache-httpcomponents-client-4-api.hpi
vars_plugin_checksum: 2d1b3f9961e5484bb70a45be8deb88d96fc0bf95
- vars_plugin_name: caffeine-api.hpi
vars_plugin_checksum: 69d651b736ee06b18d9b0f3efe332ef0628dee37
- vars_plugin_name: credentials.hpi
vars_plugin_checksum: c93d41e6989319e497dd601295036313b8b666a9
- vars_plugin_name: display-url-api.hpi
vars_plugin_checksum: 4b698121ecda4aa07ef1d80dd50158738949e796
- vars_plugin_name: git-client.hpi
vars_plugin_checksum: 1ec3107ba44f1e98a909101c932f4e478182f02b
- vars_plugin_name: git.hpi
vars_plugin_checksum: 49a35f9f0c27089a1bad3c88311b00728e716cd3
- vars_plugin_name: jsch.hpi
vars_plugin_checksum: d7f8ce10821a5e8f76d7efd8e8aab19d8e914b92
- vars_plugin_name: ldap.hpi
vars_plugin_checksum: 26e3781bba24144efc66da706c6b39cc860b44f8
- vars_plugin_name: mailer.hpi
vars_plugin_checksum: 204c1bd78772a1408142425128de6eaa17813069
- vars_plugin_name: plain-credentials.hpi
vars_plugin_checksum: 1aecd466a0fb1af38917a90ddd8f30dba6309d53
- vars_plugin_name: scm-api.hpi
vars_plugin_checksum: 0e73425e7d07d553ddb637c3b57a3f7cc78d6ff7
- vars_plugin_name: script-security.hpi
vars_plugin_checksum: 2ae3bd3a02124304bbf84f1ec57b14ca44322c21
- vars_plugin_name: ssh-credentials.hpi
vars_plugin_checksum: aa58526e1491a4f44d4a6f7045ab01b6b7564534
- vars_plugin_name: ssh.hpi
vars_plugin_checksum: 9390055b936f3f51f329937dceead7e896a2e23e
- vars_plugin_name: structs.hpi
vars_plugin_checksum: 37b98115acc372cbdc517217ad45c74bd965e570
- vars_plugin_name: trilead-api.hpi
vars_plugin_checksum: 6f440d2136873ce92bbef1e14a878e88c8f9c633
- vars_plugin_name: workflow-scm-step.hpi
vars_plugin_checksum: b4fa0b9a07c0ac36fd502947aa0bb074604ac597
- vars_plugin_name: workflow-step-api.hpi
vars_plugin_checksum: b3eb4f90b8fcccba673cecb3b34cb9a19fa39772

现在,当我使用 var 文件中的列表将文件复制到远程主机上后,如何测试这些文件。我需要执行此操作:

  • compare list of files in the remote folder with var list
  • compare checksum for each files with var list

感谢您的支持。马可

最佳答案

给定远程主机上的文件

admin@test_11:~ $ find /home/admin/local/jenkins/plugins/ -type f | xargs sha1
SHA1 (/home/admin/local/jenkins/plugins/credentials.hpi) = 5ceb410e2c54743b70ca92392a89dd2946e6f1c7
SHA1 (/home/admin/local/jenkins/plugins/ansible.hpi) = 25ebdd4c915ccb847c95610cea5e2f95fd9e9710
SHA1 (/home/admin/local/jenkins/plugins/caffeine-api.hpi) = fff9d3adfc318729922be735a40e5ff7d17a41b2

和变量

    vars_jenkins_home: /home/admin/local/jenkins
vars_plugin_files:
- vars_plugin_name: ansible.hpi
vars_plugin_checksum: 25ebdd4c915ccb847c95610cea5e2f95fd9e9710
- vars_plugin_name: caffeine-api.hpi
vars_plugin_checksum: fff9d3adfc318729922be735a40e5ff7d17a41b2
- vars_plugin_name: credentials.hpi
vars_plugin_checksum: 5ceb410e2c54743b70ca92392a89dd2946e6f1c7

查找包含校验和的文件并创建字典,例如

- hosts: test_11
tasks:
- find:
path: "{{ vars_jenkins_home }}/plugins"
patterns: "*.hpi"
get_checksum: true
register: result
- set_fact:
_dict: "{{ dict(_names|zip(_chcks)) }}"
vars:
_names: "{{ result.files|map(attribute='path')|map('basename')|list }}"
_chcks: "{{ result.files|map(attribute='checksum')|list }}"

给出

ok: [test_11] => 
_dict:
ansible.hpi: 25ebdd4c915ccb847c95610cea5e2f95fd9e9710
caffeine-api.hpi: fff9d3adfc318729922be735a40e5ff7d17a41b2
credentials.hpi: 5ceb410e2c54743b70ca92392a89dd2946e6f1c7

关于如何比较结果有很多选择。例如,创建一个字典并进行比较

    - set_fact:
dict_files: "{{ vars_plugin_files|
items2dict(key_name='vars_plugin_name',
value_name='vars_plugin_checksum') }}"
- debug:
msg: "{{ dict_files == _dict }}"

给出

ok: [test_11] => 
msg: true

如果字典不同,您可能需要迭代这些项目,例如

    - debug:
msg: "{{ item.vars_plugin_name }} checksum error."
loop: "{{ vars_plugin_files }}"
when: item.vars_plugin_checksum != _dict[item.vars_plugin_name]

关于Ansible:如何从列表中检查远程文件夹中是否存在多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68375306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com