I have an autoscaling group on Amazon EC2
and I have added my public key when I create AMI with packer
so I can run ansible-playbook and ssh to the hos
In ansible There is no option to store passphrase-protected private key
For that we need to add the passphrase-protected private key in the ssh-agent
Start the ssh-agent in the background.
# eval "$(ssh-agent -s)"
Add SSH private key to the ssh-agent
# ssh-add ~/.ssh/id_rsa
Now try running ansible-playbook and ssh to the hosts.
I solved it by running ssh-add
once and use it like if it's not password protected.
Building up on @javeed-shakeel's answer, I added the following lines to my .bashrc
:
command -v ansible > /dev/null &&
alias ansible='ssh-add -l > /dev/null || ssh-add 2> /dev/null && ansible'
command -v ansible-playbook > /dev/null &&
alias ansible-playbook='ssh-add -l > /dev/null || ssh-add 2> /dev/null && ansible-playbook'
This will run ssh-add
before ansible(-playbook)
iff there was no key added to the ssh-agent, yet. This has the advantage that one does not need to run ssh-add
by hand and one will be asked for the passphrase only if it is necessary.