Ansible - How to loop through command with items until registered variables are equal?

一笑奈何 提交于 2019-12-11 08:01:26

问题


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.


回答1:


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;"


来源:https://stackoverflow.com/questions/56654059/ansible-how-to-loop-through-command-with-items-until-registered-variables-are

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!