How to configure target servers in ansbile server.
Note: you must be able to connect to the target server/s without password.
- Simple way to generating and copying the ssh keys
You won’t be able to ssh the target server, before ssh keys.
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-18.png)
Run below command on both (ansible and target) servers.
ssh-keygen
Below one is Ansible server.
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-14.png)
Target server:
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-15.png)
Now copy the pub key from Ansible server to authorised_keys in target server.
Ansible server: (Note: These were test servers and they are terminated after this exercise).
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-16.png)
Note: you can use below command too.
cat ~/.ssh/id_rsa.pub | ssh username@remote_host “mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys”
Target server:
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-17.png)
Once ssh keys are sorted you should be able to ssh the target server.
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-19.png)
Adhoc command
At Ansible server, make sure you have entered the private ip of the all target servers and categorized them. Below are the examples.
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-10.png)
ubuntu@ip-172-31-18-85:~$ ansible -i inventory all -m “shell” -a “touch Testdevops”
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-8.png)
Now let’s confirm it has created a file Testdevops in target server.
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-9.png)
Now let’s run first playbook.
Make sure you have entries of the server/s in inventory file. Also your ansible playbook is written correctly.
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-11.png)
Let’s run playbook.
ansible-playbook -i inventory first-playbook.yml
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-12-1024x148.png)
Now let’s check if iginx is installed on the target server.
![](https://www.sanjayshonak.co.uk/wp-content/uploads/2024/06/image-13-1024x174.png)