Ansible 是否可以使用类似于散列字符串的查找方式对文件进行散列(例如,{{ 'test1' | hash('sha1') }})?
Ansible 是否可以使用类似于散列字符串的查找方式对文件进行散列(例如,{{ 'test1' | hash('sha1') }})?
Linux 命令行 (WORKS)
sha1sum /etc/default/grub
返回哈希值:f2de8d3dfe08c34615145f212e5a32facf575cb3
Ansible stat 模块 (WORKS)
- name: checksum | /etc/default/grub (stat)
delegate_to: localhost
stat:
path: "/etc/default/grub"
checksum_algorithm: sha1
register: local_grub_orig_sha1
返回哈希值: f2de8d3dfe08c34615145f212e5a32facf575cb3
使用哈希过滤器进行 Ansible 查找(失败)
- name: checksum | /etc/default/grub (lookup)
delegate_to: localhost
set_fact:
local_grub_sha1: "{{ lookup('file', '/etc/default/grub') | hash('sha1') }}"
返回哈希:834f3f662f6a19cf273d87a00d4af2645ab18dcd
注意:此实现仅限于本地主机。请参阅下面 @Vladimir Botka 的回答,了解使用 stat 的一般解决方案。
Can Ansible hash files using lookup similar to how it can hash strings (e.g., {{ 'test1' | hash('sha1') }})?
Linux command line (WORKS)
sha1sum /etc/default/grub
returns hash: f2de8d3dfe08c34615145f212e5a32facf575cb3
Ansible stat module (WORKS)
- name: checksum | /etc/default/grub (stat)
delegate_to: localhost
stat:
path: "/etc/default/grub"
checksum_algorithm: sha1
register: local_grub_orig_sha1
returns hash: f2de8d3dfe08c34615145f212e5a32facf575cb3
Ansible lookup with hash filter (FAILS)
- name: checksum | /etc/default/grub (lookup)
delegate_to: localhost
set_fact:
local_grub_sha1: "{{ lookup('file', '/etc/default/grub') | hash('sha1') }}"
returns hash: 834f3f662f6a19cf273d87a00d4af2645ab18dcd
NOTE: This implementation is limited to localhost. See @Vladimir Botka's answer below for a general solution using stat.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 统计。例如,对其进行测试
您应该会从 command 和 stat 中看到相同的结果。
Use stat. Test it, for example
You should see the same results from the command and stat.
通过使用
lookup('template', ...)
而不是lookup('file', ...)
解决了该问题。但是,我不清楚是什么导致了行为差异。返回哈希:f2de8d3dfe08c34615145f212e5a32facf575cb3
The issue was solved by using
lookup('template', ...)
rather thanlookup('file', ...)
. However, it is not clear to me what is causing the difference in behavior.returns hash: f2de8d3dfe08c34615145f212e5a32facf575cb3