Ansible-如何在命令与项之间循环,直到注册的变量相等? [英] Ansible - How to loop through command with items until registered variables are equal?

查看:70
本文介绍了Ansible-如何在命令与项之间循环,直到注册的变量相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找文件在过去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.

实际结果:

字典对象"没有属性结果.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆