Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for ssh to become available on a host before installing a role?

Is there a way to wait for ssh to become available on a host before installing a role? There's wait_for_connection but I only figured out how to use it with tasks.

This particular playbook spin up servers on a cloud provider before attempting to install roles. But fails since the ssh service on the hosts isn't available yet.

How should I fix this?

---
- 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
like image 791
shellwhale Avatar asked Dec 17 '20 17:12

shellwhale


People also ask

How do I specify SSH ports in Ansible?

One of the first things I do when I get my hands on a new linux server is to change the SSH port. It's a basic, easy and efficient way of warding most brute force attempts. In a nutshell, you edit the Port parameter of /etc/ssh/sshd_config , restart sshd and you're done.

How do I delay an Ansible task?

To pause/wait/sleep per host, use the ansible. builtin. wait_for module. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely.

Does Ansible work over SSH?

By default, Ansible assumes you are using SSH keys to connect to remote machines. SSH keys are encouraged, but you can use password authentication if needed with the --ask-pass option.

What does the WAIT command do in SSH command?

This basically uses scp to copy the data over from the server to the local machine. This redirects the standard output of the script to a local file. The wait will pause the script until the ssh command (which runs as a background process) exits. Show activity on this post.

How do I exit an SSH session after it finishes?

The SSH session will not exit until the command (script) has finished executing on the remote server. Depending on whether the script outputs data to a file on the server or if it outputs data to standard output you can do one of two things. This basically uses scp to copy the data over from the server to the local machine.

Does waiting for a port to become available produce an error?

This does not produce an error. Waiting for a port to become available is useful for when services are not immediately available after their init scripts return which is true of certain Java application servers. It is also useful when starting guests with the community.libvirt.virt module and needing to pause until they are ready.


1 Answers

Ansible play actions start with pre_tasks, then roles, followed by tasks and finally post_tasks. Move your wait_for_connection task as the first pre_tasks and it will block everything until connection is available:

- 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: ...

For more info on execution order, see this title in role's documentation (paragraph just above the notes).

Note: you probably want to move all your current example tasks in that section too so that facts are gathered and curl installed prior to do anything else.

like image 124
Zeitounator Avatar answered Sep 22 '22 01:09

Zeitounator