有没有办法在安装角色之前等待ssh在主机上可用?有wait_for_connection
,但我只想出了如何在任务中使用它。
在尝试安装角色之前,此特定的剧本会启动云提供商上的服务器。但是失败,因为主机上的ssh服务尚不可用。
我该如何解决?
---
- hosts: localhost
connection: local
tasks:
- name: Deploy vultr servers
include_tasks: create_vultr_server.yml
loop: "{{ groups['vultr_servers'] }}"
- hosts: all
gather_facts: no
become: true
tasks:
- name: wait_for_connection # This one works
wait_for_connection:
delay: 5
timeout: 600
- name: Gather facts for first time
setup:
- name: Install curl
package:
name: "curl"
state: present
roles: # How to NOT install roles UNLESS the current host is available ?
- role: apache2
vars:
doc_root: /var/www/example
message: 'Hello world!'
- common-tools
最佳答案
Ansible播放 Action 从pre_tasks
开始,然后是roles
,然后是tasks
,最后是post_tasks
。将wait_for_connection
任务移动为第一个pre_tasks
,它将阻止所有操作,直到连接可用:
- hosts: all
gather_facts: no
become: true
pre_tasks:
- name: wait_for_connection # This one works
wait_for_connection:
delay: 5
timeout: 600
roles: ...
tasks: ...
有关执行顺序的更多信息,请参见this title in role's documentation(注释上方的段落)。注意:您可能也想移动该部分中的所有当前示例任务,以便在执行其他任何操作之前先收集事实并安装curl。
关于ansible - 在安装角色之前,如何等待ssh在主机上可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65345720/