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!

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right, sudo was the problem, didn't know it makes the controller connect as root. Now it works, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.