This page looks best with JavaScript enabled

How to create elliptic-curve key for ansible user and add it to remote servers using Bash script

 ·  ☕ 1 min read  ·  🐧 sysadmin
  1. Here is a video tutorial

Scripts and configuration files are available here:

  1. Install sshpass

SLES | openSUSE Leap 15.4

1
sudo zypper install sshpass 

Debian

1
sudo apt install sshpass 

Red Hat

1
sudo dnf install sshpass 
  1. Create a file
1
vim pass_file_ansible

and place a password for the ansible user to connect to remote hosts.

  1. Make the script read only for this user
1
chmod 400 pass_file_ansible
  1. Create a list of servers with IP addresses or hostnames
1
vim servers
  1. Create a script
1
vim  ssh-copy-id.sh

And add the below content

1
2
3
4
5
6
#!/bin/bash
ssh-keygen -t ed25519 -C "ansible@rancher.local"
servers=$(cat servers)
for i in $servers; do
  sshpass -f pass_file_ansible ssh-copy-id -i ~/.ssh/id_ed25519.pub ansible@$i
done
  1. Make sure that PasswordAuthentication has set boolean value to yes in /etc/ssh/sshd_config file on remote servers.

  2. Make the script executable

1
chmod +x ssh-copy-id.sh 
  1. Execute the script
1
./ssh-copy-id.sh 
Share on

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist