Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup SSH connection with Ansible?

Tags:

ssh

ansible

I am brand new to learning Ansible. Here is a pretty easy example.

I have computer A, where I will be running playbooks from.

And 10 other host machines that need to be configured. My question is, do I just need to put the public SSH key of my host machine on the 10 hosts in ~/.ssh/authorized_keys ?

I guess my understanding of how to efficiently setup SSH connections between my main computer and all the clients is a little fuzzy. Any help would be appreciated here.

like image 711
patterns Avatar asked Oct 22 '25 05:10

patterns


1 Answers

You create a file called hosts with this content

[test-vms]
10.0.0.100 ansible_ssh_pass='password' ansible_ssh_user='username'

In above hosts file leave off ansible_ssh_pass='password' if using ssh keys ... Then you can create a playbook with the commands and call the playbook like below. The first line of the playbook needs to have the hosts declaration

---
- hosts: test-vms

  tasks:
  -name: "This is a test task"
   command: /bin/hostname

Finally, you call the playbook like this

ansible-playbook -i <hosts-file> <playbook.yaml>
like image 128
SamK Avatar answered Oct 23 '25 20:10

SamK