Control Node: Any host where Ansible is installed. Ansible control nodes are mainly used to run tasks on managed hosts. For our example, I'll call Control Node as Ansible Master or Master.
Managed Node: These are the servers which you want to automate by connecting them to Ansible Control Node. Ansible can communicate with them via SSH (in Linux) or WinRM (in Windows). For our example, I'll call Managed Node as Ansible Worker or Worker.
Ansible installation and establishing connection between Ansible Master and target hosts:-
Creation of 3 Instances in AWS EC2 with below configuration,
Connect to the VMs using ssh terminal. In my case I’m using Termius. For demonstration I’ll be creating Ansible Worker 1 in terminus,
Termius is available for iphone, android, mac and windows.
After Saving the key, the key will remain there in app.
Now terminal will open in Termius,
Similarly create for Ansible Master and Ansible Worker 2.
Now double click to on Ansible Master to open, after creating host on termius and type the below commands,
sudo apt-get update sudo apt-add-repository ppa:ansible/ansible #Add the Ansible PPA repository sudo apt install ansible #to download and install ansible ansible --version #to check the version of ansible
We can connect to Worker ,
ssh-keygen # Generate SSH key chmod 400 ~/.ssh #Change the permission of ssh key cat ~/.ssh/<given_name_of_generated_key>.pub #to display the public key to copy ssh-copy-id <username>@<managed_node_ip> # Copy your SSH key to the managed node
ssh-keygen will generate public and private keys. If the name is not given when asked after typing the command, by default it'll create id_rsa.pub (Public Key) and id_rsa (Private Key) in the location:
/home/ubuntu/.ssh/
.ssh-copy-id will copy the public key to the targetted server or worker. If not, generate an SSH key on your Ansible Master and copy the public key to your Ansible Worker in authorized_key which you will find in the below location of Ansible Worker,
/home/$USER/.ssh/authorized_keys
or can follow the below command,
cd ~ # will bring you in "/home/user" ls -a # display hidden files cd .ssh ls # will see authorized_keys vim authorized_keys # to edit
Follow above steps in Ansible worker to locate "authorized_keys" and add the Ansible Master's public key. And Restart.
We will create another file
/home/ubuntu/inventory.txt
where we will write the host name and add them in a tag. So that we don't have to connect every single host. One place we can manage all hosts by call them by their tag name.Eg.
If we want to do any specific to webserver, I can write the tag name and mention the inventory. Then whichever hosts will be there under webserver tag, we will be able to take control for those host.
We can also mention all hosts under one tag name, we can keep any name let's call as common. If we want to manage all the the hosts, then we can call common tag name and also write the inventory name. So all the hosts under common tag name we will be able to access.
Now we will be giving hosts detail in the below format,
vim inventory.txt
Add the host name into the inventory,
Format,
ansible-target ansible_host=<host public ip> ansible_connection=ssh ansible_user=<host username>
Tag name should be written in
[]
brackets.Connect the hosts with Ansible Master.
Now we're able to ping the individual hosts and the
Tag name: webservers
,Type the below to avoid getting ssh authentiacation,
export ANSIBLE_HOST_KEY_CHECKING=False