Ansible - How to loop through command with items u

2019-07-27 01:00发布

I'm trying to find if a file has had any writes in the last 15 seconds.

- name: 'Check File for Writes'
  shell: tail -n 50 /path/to/some/file | sha1sum
  loop:
    - 1
    - 2
  register: file_writes
  loop_control:
    pause: 15
  until: file_writes.results[0].stdout == file_writes.results[1].stdout

The expected behavior is as follows:
1.) This task would run the 'tail' command once
2.) It would then wait for 15 seconds
3.) Then run the 'tail' command again
4.) The outputs of both tail commands would be registered in 'file_writes'.results
5.) Steps 1 through 4 would be looped until the first 'tail' command's hash matches the second 'tail' commands' hash.

The actual result:

'dict object' has no attribute results.

标签: ansible tail
1条回答
看我几分像从前
2楼-- · 2019-07-27 01:35

It is possible to put all the logic into a script

- shell: "hash0=$(tail -n 50 /path/to/some/file | sha1sum);
          sleep 15;
          hash1=$(tail -n 50 /path/to/some/file | sha1sum);
          while [ \"$hash0\" != \"$hash1\" ]; do
               sleep 15;
               hash0=$hash1;
               hash1=$(tail -n 50 /path/to/some/file | sha1sum);
          done;"
查看更多
登录 后发表回答