在ansible中循环shell命令

时间:2017-08-17 19:18:23

标签: ansible

我必须在我的剧本中重新启动systemd单元并等到服务处于活动状态,所以我的检查并等待"逻辑必须如下所示"伪代码":

for iterations=0; iterations < 10; iterations++ {
    status = systemctl is-active myservice
        if (status == active)
            break
        sleep 3s
    }
}
if status != active {
    failure: exit playbook
}

似乎在ansible playbook中我可以迭代shell命令的结果但是在查看&#34; loops&#34;在文件中: http://docs.ansible.com/ansible/latest/playbooks_loops.html#iterating-over-the-results-of-a-program-execution 根据输出,我无法找到如何重复命令本身。可以吗?

2 个答案:

答案 0 :(得分:2)

Ansible中有do-until个循环:

- shell: systemctl is-active myservice
  register: results
  until: results | success
  retries: 10
  delay: 3

答案 1 :(得分:0)

为此,我会使用servicesystemdwait_for模块。