使用delegate_to运行adhoc wait_for

时间:2016-12-01 10:06:07

标签: ansible

我的Makefile有以下步骤

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

   setup:
       do other stuff

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

  - hosts: all
    tasks:
      - name: wait for ssh port
        wait_for:
          host: '{{ ansible_ssh_host }}'
          port: 22
          timeout: 300
        delegate_to: localhost

我尝试过使用adhoc命令运行上面的命令,但是使用了本地操作

ansible all -i inventory -m local_action -a 'wait_for port=22 timeout=300'

但那不正确导致

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

2 个答案:

答案 0 :(得分:1)

  

我的Makefile有以下步骤

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

来自评论:

  
      
  • 所以你想检查所有目标服务器是否可以ping通? - Shasha99
  •   
  • 不可ping。 SSH-能。
  •   

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

for i in {1..30}; do
  ssh $host true 2> /dev/null
  if [ $? -eq 0 ]; then
    exit
  else
    sleep 10
  fi
done

答案 1 :(得分:0)

@Shasha99's suggestion为我工作,以验证wait_for是否按我期望的方式工作:

ansible --inventory=PATH -m wait_for -a 'host=IP port=PORT delay=10 timeout=300' 127.0.0.1
相关问题