3

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
user5447339
  • 141
  • 2
  • 6

1 Answers1

4

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 }}"
Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
  • thanks for explanation but my `desc.cmd` file is little bit different. Somehow I need to replace previous checksum with new checksum so I need to do something to replace the previous one with new one. basically my command should know where to replace with new one? – user5447339 Oct 05 '19 at 17:09
  • [lineinfile](https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html#parameters) module comes with a `regexp` parameter to identify the line which should be replaced. – Henrik Pingel Oct 05 '19 at 17:19
  • Aah I see. Can you tell me how can I use to work in my case? – user5447339 Oct 05 '19 at 17:21
  • you still around? – user5447339 Oct 06 '19 at 16:30