大网数据广告活动
服务器运维问答

get the checksum of a file and update another file with this checksum in ansible?

· 52CE 我爱测

I have a file test_proc.init in a particular directory. And I have one more file desc.cmd in the same directory which has below content: set_poc 204 6 send_data_file goldy_proc.init 4e8ee8946f7a89d2eb501a752c5e3ee6ea266e5b set_po…

问题原文

原帖链接 · user5447339 · CC BY-SA 4.0

I have a file test_proc.init in a particular directory. And I have one more file desc.cmd in the same directory which has below content:

set_poc 204 6
send_data_file goldy_proc.init 4e8ee8946f7a89d2eb501a752c5e3ee6ea266e5b
set_poc 204 7
send_data_file goldy_proc.init 4e8ee8946f7a89d2eb501a752c5e3ee6ea266e5b
set_poc 204 8
send_data_file goldy_proc.init 4e8ee8946f7a89d2eb501a752c5e3ee6ea266e5b
set_poc 204 9
send_data_file goldy_proc.init 4e8ee8946f7a89d2eb501a752c5e3ee6ea266e5b

Below is what I want to do:

  • I want to get the sha1sum of test_proc.init file: sha1sum test_proc.init.
  • And whatever is the output from that sha1sum command, I want to copy that checksum and update the desc.cmd file.

So if new checksum is e32313118e53b60140d2024dfa7578c3fd89b346 the my desc.cmd file will be like this:

set_poc 204 6
send_data_file goldy_proc.init e32313118e53b60140d2024dfa7578c3fd89b346
set_poc 204 7
send_data_file goldy_proc.init e32313118e53b60140d2024dfa7578c3fd89b346
set_poc 204 8
send_data_file goldy_proc.init e32313118e53b60140d2024dfa7578c3fd89b346
set_poc 204 9
send_data_file goldy_proc.init e32313118e53b60140d2024dfa7578c3fd89b346

Is this possible to do in ansible? I am not sure on how can I paste the checksum and update the file.

- name: Get sha1sum of file
  stat:
    path: /data/test_proc.init
    checksum_algorithm: sha1sum
    get_checksum: yes
  register: shell_stat

社区高分解答

原帖链接 · Henrik Pingel · CC BY-SA 4.0

The output of the stat command should already contain the sha of the file:

- name: Get sha1sum of file
  stat:
    path: /data/test_proc.init
    checksum_algorithm: sha1sum
    get_checksum: yes
  register: sha

It can than be used with a module like lineinfile or blockinfile to update the desc.cmd file like this:

- name: insert sha
  lineinfile:
    path: /data/desc.cmd
    line: "{{ sha.stat.checksum }}"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-10-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。