0

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!

Amela
  • 23
  • 5
  • There are hundreds "ansible ssh" issues at SO. Follow [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Vladimir Botka Apr 09 '19 at 08:57

1 Answers1

2

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.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • You're right, sudo was the problem, didn't know it makes the controller connect as root. Now it works, thanks! – Amela Apr 09 '19 at 09:29