Ansible - 如何使用项目循环命令,直到注册的变量相等? [英] Ansible - How to loop through command with items until registered variables are equal?
问题描述
我正在尝试查找文件在过去 15 秒内是否有任何写入.
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
预期行为如下:
1.) 此任务将运行一次tail"命令
2.) 然后它会等待 15 秒
3.) 然后再次运行'tail'命令
4.) 两个 tail 命令的输出都将注册在 'file_writes'.results
5.) 将循环第 1 步到第 4 步,直到第一个tail"命令的哈希值与第二个tail"命令的哈希值匹配.
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.
实际结果:
'dict object'没有属性结果.
The actual result:
'dict object' has no attribute results.
推荐答案
可以把所有的逻辑放到一个脚本中
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;"
这篇关于Ansible - 如何使用项目循环命令,直到注册的变量相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!