Install Ansible on Ubuntu and configure a control node and a target node
Ansible is an open-source automation tool used for IT tasks such as configuration management, application deployment, and task automation. Here are some key features and concepts of Ansible:
Key Features:
- Agentless: Ansible does not require any agent software to be installed on the nodes it manages. It uses SSH for communication.
- Easy to Learn: Ansible uses a simple syntax written in YAML called playbooks.
- Idempotency: Ansible ensures that changes are applied only when necessary, making it safe to run multiple times without causing unintended effects.
- Extensible: Ansible modules can be written in any language that can return JSON.
- Scalable: Ansible can manage small environments to large-scale deployments.
Before Installing Ansible, I must ensure that Python is already installed on control node and a target node
Step 1: Install Ansible on the Control Node
1 .Update the package list:
sudo apt update
sudo apt install ansible -y
3 .Verify the installation:
ansible --version
Output
Step 2: Configure SSH Access from Control Node to Target Node
- Generate SSH keys on the control node (if not already done):
ssh-keygen
Output
- RSA is the default algorithm that will be used
- Keys are generated by using SHA26
- id_rsa : Private Key
- id_rsa.pub : Public key
- Passphrase(not mandatory): a secret that the user must type to use the private key.
2 .Locate the SSH public key on the control node:
The SSH public key is usually located in the ~/.ssh
directory and typically named id_rsa.pub
or id_ecdsa.pub
. You can list the files in this directory to find the correct one:
ls ~/.ssh
3. Display the contents of the SSH public key:
Use the cat
command to display the contents of your public key file:
cat ~/.ssh/id_rsa.pub
Copy the entire output.
4. Connect to the target node:
Use SSH to connect to the target node:
ssh user@target_node_ip
Replace user
with the username and target_node_ip
with the IP address of the target node.
5. Create the .ssh
directory on the target node (if it doesn't exist):
mkdir -p ~/.ssh chmod 700 ~/.ssh
Append the public key to the authorized_keys
file:
On the target node, open the authorized_keys
file in an editor:
nano ~/.ssh/authorized_keys
Paste the public key content you copied earlier into this file. Save and close the file
Set the correct permissions for the authorized_keys
file:
chmod 600 ~/.ssh/authorized_keys
6 . Verification
Log out of the target node:
exit
Test passwordless SSH from the control node to the target node:
ssh user@target_node_ip
You should be able to log in without being prompted for a password.
Step 3: Configure Ansible Inventory File
Edit the inventory file (default location is /etc/ansible/hosts
):
sudo nano /etc/ansible/hosts
uncommit the file add the target node details.
Replace user
with the username and target_node_ip
with the IP address of the target node.
[webservers]
user@target_node_ip
save the file
Step 4: Test the Connection
Ping the target node using Ansible:
ansible webservers -m ping
You should see a response indicating success.
It’s done