Ansible (part 2) → connecting two ubuntu machines

Aakib
4 min readJul 29, 2023

In the first part we saw what is ansbile ,what is the difference between ansbile and terraform etc etc

first part link → Ansible → The Configuration tool (part 1)

In this part we step by step process on how to connect two ubuntu machines →

Step 1 →Sign into your aws account and launch two ubuntu machine name one of your machine is ansible server and other one is ansible node

Step 2 → Then take ssh of both of them and run

  1. sudo su
  2. apt update -y in both of them

now we are first going to take ssh of ansible node from ansible server

Step 3 → Run the following commands on both instance

  1. sudo apt install openssh-server
  2. sudo systemctl start ssh
  3. sudo systemctl enable ssh
  4. sudo systemctl status ssh

Step 4 → create a user name ansible in both machine and set password

  1. adduser ansible

give ansible users sudo permission or edit the sudoers file

2. visudo →enter the following text

ansible ALL=(ALL) NOPASSWD: ALL →#add this text to your visudo file

3. press ctrl+o and enter to save file and then ctrl +x to exit from the file

Step 5 → Now generate public and private keys by the following command

  1. ssh-keygen

2. now run on server → cat /root/.ssh/ id_rsa.pub

you will given a public key copy that key from ansible server

3. go to ansible node and run → vim /root/.ssh/authorized_keys

press i to insert and first clear all the text and then paste the public key of your ansible server in ansible node authorized_keys file

Step 6→ Now we have to do some changes in sshd_config file do these steps in both the machines

  1. run → vim /etc/ssh/sshd_config

press i to enter

2. uncommented permit root login yes and password authentication yes

press esc :wq to save and exit from the file

3. Now run →service sshd restart

you are now able to take ssh of ansible node from ansible user

step 7 → now switch to your ansible on both machine user by following command

  1. su — ansible #enter password

On ansible user run the following command

2. sudo ssh-copy-id user@remote_machine_ip

#user =ansible

#remote_machine_ip = your ansible node ip

3. now run →ssh ‘ansible@34.229.7.184’

now you are inside the ansible node

run → exit
you have been logout now from ansible node

Step 8 → Now let’s connect it with ansible

  1. run → apt install ansible
  2. Create the Directory → sudo mkdir -p /etc/ansible
  3. sudo vim/etc/ansible/inventory.ini # in this file we store the users ip within a group
  4. enter the following text →

[ubuntu_servers] #this is the group name
machine1 ansible_host=192.168.1.101 # this is your ansible node ip

5 . esc :wq to save file and exit

Step 8 now ping your ansible node from server through ansbile

  1. run the command →ansible -i inventory.ini -u ansible -m ping

Now finally you are connect with ansible node 😉

let’s install git from server to the node →

Step 1 →Create a new file named install_git.yml

Step 2 run → vim install_git.yml

Step 3: Add the following content to the install_git.yml playbook

Step 4: Save and close the file.

Step 5: Run the Ansible playbook using the ansible-playbook command:

ansible-playbook -i inventory.ini -u ansible install_git.yml

checkout the result on ansible node

If you have any doubt you could ask me on linkedin here is my linked_id → Aakib_khan

--

--

Aakib

Cloud computing and DevOps Engineer and to be as a fresher I am learning and gaining experiance by doing some hands on projects on DevOps and in AWS OR GCP