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
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With