Generating SSH Key Pairs
Initialize secure authentication by creating an RSA key pair on the management node. The following command generates the keys without a passphrase for automation purposes.
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -N ""
Non-Interactive Authentication Setup
To facilitate scripted connections where manual password entry is not feasible, utilize the sshpass utility. This tool supplies the password directly to the SSH client.
sshpass -p"SecurePass123" ssh -o StrictHostKeyChecking=no 10.0.0.101 hostname
Automating Key Distribution with Shell Scripts
Deploy public keys to multiple remote servers efficiently using a Bash loop. This script iterates through a list of host identifiers and pushes the authentication key.
#!/bin/bash
# Script: /opt/scripts/distribute_keys.sh
SUBNET="10.0.0"
NODE_IDS=(101 102 103)
AUTH_PASS="SecurePass123"
for host_id in "${NODE_IDS[@]}"; do
sshpass -p"${AUTH_PASS}" ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa.pub root@${SUBNET}.${host_id}
done
Execute the script to establish passwordless access across the cluster.
bash /opt/scripts/distribute_keys.sh
Verify connectivity by logging into the remote hosts without a password prompt.
ssh 10.0.0.101 hostname
ssh 10.0.0.102 hostname
Ansible Installation and Configuration
Install the automation framwork and locate the primary configuration directory.
yum install -y ansible
rpm -ql ansible | grep -v /usr/
Key configuration files reside in /etc/ansible/, specifically ansible.cfg and the inventory file hosts.
Configuring the Inventory File
Define managed nodes within the inventory file. Ensure SSH key-based authentication is already established for these hosts.
# /etc/ansible/hosts
[nodes]
10.0.0.101
10.0.0.102
10.0.0.103
Verifying Host Connectivity
Use the built-in ping module to confirm that Ansible can communicate with all defined hosts in the group.
ansible nodes -m ping
Successful responses will return a "pong" status indicating reachability.
Executing Ad-Hoc Commands
Run standard shell commands across the server group using the commmand module. This method is suitable for simple tasks without special shell characters.
ansible nodes -m command -a 'uptime'
Target specific individual hosts by specifying the IP address directly.
ansible 10.0.0.101 -a 'hostname'
Execute commands against all known hosts in the inventory.
ansible all -a 'hostname'
File Transfer Operations
Utilize the copy module to distribute files from the control node to remote destinations.
ansible nodes -m copy -a 'src=/etc/hostname dest=/tmp/'
Verify the file transfer by checking the content on the remote system.
ansible nodes -a 'cat /tmp/hostname'
Consult module documentation for specific parameter requirements using the ansible-doc utility.
ansible-doc -s copy