ansible - 在安装角色之前,如何等待ssh在主机上可用?

标签 ansible ansible-role

有没有办法在安装角色之前等待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/

相关文章:

azure - 通过 Ansible 创建没有存储帐户的 Azure VM

ansible - 在 Ansible playbook 中指定角色的版本

Ansible:如何通过在主机上附加文件的任务实现幂等性(不恢复到初始状态)

Ansible - 如何提高跳过角色的性能

docker - 使用 Ansible 获取 Docker 的 IP 地址

python - 在 Ansible 中,如何访问回调插件中提供给 playbook 的额外参数?

command-line - ansible - 如何在运行 ansible-playbook 时传递本地 DNS 服务器来解析主机名

ansible - 使用依赖角色配置 Ansible 角色

Ansible 与通配符同步