使用 delegate_to 运行 adhoc wait_for

use*_*522 6 ansible

我的 Makefile 有以下步骤

   create:
       do create stuff
       # extra ansible adhoc command to wait for ssh

   setup:
       do other stuff
Run Code Online (Sandbox Code Playgroud)

我想在do create stuff等待所有主机都可以访问其 ssh 端口之后添加一个步骤,但我不想提交一个额外的剧本来执行此操作:

  - hosts: all
    tasks:
      - name: wait for ssh port
        wait_for:
          host: '{{ ansible_ssh_host }}'
          port: 22
          timeout: 300
        delegate_to: localhost
Run Code Online (Sandbox Code Playgroud)

我尝试使用一个临时命令来运行上面的命令,但使用本地操作

ansible all -i inventory -m local_action -a 'wait_for port=22 timeout=300'
Run Code Online (Sandbox Code Playgroud)

但这不是正确的原因

ERROR! this task 'local_action' has extra params, which is only allowed in the following modules: command, win_command, shell, win_shell, script, include, include_vars, add_host, group_by, set_fact, raw, meta
Run Code Online (Sandbox Code Playgroud)

tec*_*raf 1

\n

我的 Makefile 有以下步骤

\n\n
create:\n   do create stuff\n   # extra ansible adhoc command to wait for ssh\n
Run Code Online (Sandbox Code Playgroud)\n
\n\n

来自评论:

\n\n
\n
    \n
  • 那么您想检查所有目标服务器是否均可 ping 通吗?\xe2\x80\x93莎莎99
  • \n
  • 无法 ping 通。支持 SSH。
  • \n
\n
\n\n

我不知道你为什么要使用 Ansible 来做这件事。这是一个简单的 bash 循环(添加一个外部循环来迭代您的主机):

\n\n
for i in {1..30}; do\n  ssh $host true 2> /dev/null\n  if [ $? -eq 0 ]; then\n    exit\n  else\n    sleep 10\n  fi\ndone\n
Run Code Online (Sandbox Code Playgroud)\n