Configure SSH
Ansible is regarded as agentless because it doesn’t rely on the installation of an agent to effect change on the hosts it controls. Instead, it uses the operating system’s built-in remote management services, such as SSH and WinRM.
In this article, we’ll walk through the process of generating and setting up SSH keys for authenticating to the Ansible Controller. We’ll also configure a built-in SSH service to allows us to log in to remote hosts from the Ansible Controller while still using the SSH keys stored on our workstation.
Key Generation
On your workstation (not the Ansible controller), open a terminal and create your SSH keypair.
ssh-keygen -t ecdsa -b 512You’ll be asked to create a password that will protect the use of your SSH key pair. It’s entirely up to you whether you want to enter a password when logging onto a device using your SSH key pair. If you’d prefer not to have a password, simply press Enter when prompted.
You’ll now have two new files located in your ~/.ssh/ directory (or \ if you’re on Windows).
~/.ssh/id_ecdsa # Private Key
~/.ssh/id_ecdsa.pub # Public Keyid_ecdsa is your private key and must be protected at all times. Store a copy of this key within your password manager in case your workstation fails.
id_ecdsa.pub is your public key. We’ll be installing this key on the Ansible controller and any hosts we’re managing via Ansible. It’s through this public key that our authentication request to connect to the host will be validated and verified.
Configure SSH Agent
Depending on whether your working off of Windows or Linux/Mac, enable the SSH Agent.
On Debian, ensure you have the openssh-client package installed.
sudo apt-get install openssh-clientAdd your SSH private key to the SSH Agent.
ssh-add ~/.ssh/id_ecdsaEnsure the OpenSSH client is installed on your workstation.
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0Once installed, start the service.
Start-Service -Name ssh-agentConfigure the service to start automatically on boot.
Set-Service -Name ssh-agent -StartupType AutomaticLastly, add your SSH private key to the SSH Agent.
ssh-add.exe ~\.ssh\id_ecdsaKey Installation
Install the public key onto the Ansible Controller. Depending on whether your workstation is Windows or Linux-based the process varies slightly.
Linux
For Debian-based systems, the openssh-client package comes with a handy shell script to make installing the public key simple.
ssh-copy-id username@hostWindows
For Windows no handly utility is provided so we’ll have to use a bit of PowerShell.
type $env:USERPROFILE\.ssh\id_ecdsa.pub | ssh username@host "cat >> .ssh\authorized_keys"Testing
With our SSH key’s generated and public key installed, we can now logon to the Ansible Controller. If all goes well, you’ll be automatically logged in without any password prompts (unless you created one during the SSH key generation process).
ssh username@hostTo check that the SSH Agent is functioning, run the following command.
echo $SSH_AUTH_SOCKYou should see output similar to below. If blank, you have a configuration error.
/tmp/ssh-XXXXkVwIiN/agent.895SSH Forwarding
As we want to use our private key to logon to the various hosts managed by Ansible, we need to tell SSH to forward our private key to the remote hosts when authenticating. This can be easily done by updating the sshd_config.
On the Ansible Controller, open /etc/ssh/sshd_config using vim or an equivilent text editor.
sudo vim /etc/ssh/sshd_configUpdate AllowAgentForwarding from no to yes.
|
|
Save the file and restart the SSH service.
sudo systemctl restart sshSuccess! We’re now able to log onto the Ansible Controller via our SSH keys stored on your workstation. We’re also all setup to have Ansible logon to our remote hosts using our SSH keys.
Next, we’ll configure our remote hosts.