Amela
Amela

Reputation: 23

Running playbook returns: Failed to connect to the host via ssh, solved by running ping all

I have an ansible playbook and I run it:

sudo ansible-playbook -i hosts startelk.yml -vvv

Every time, after I change the hosts file, running the same playbook results in "Failed to connect to the host via ssh". If I run

ansible all -m ping

first and then the playbook command, the playbook gets successfully started.

Does anyone know why do I have to run ping each time after changing hosts (or some other) file, and then my ssh connection for playbook works, otherwise no? I don't want to be running ping every time I need to change something in Ansible.

Thanks!

Upvotes: 0

Views: 1109

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

It's not a good idea to run "sudo ansible-playbook ..." This way the controller connects the host as root. Best practice is not to allow root ssh connections.

Best practice is to:

  1. run ansible-playbook as a normal user
  2. configure remote_user and
  3. escalate the privilege with become and become_user.

Read more at Understanding Privilege Escalation.

Upvotes: 2

Related Questions