How to run an ansible-playbook with a passphrase-protected-ssh-private-key?

后端 未结 3 1328
花落未央
花落未央 2021-01-30 22:26

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

相关标签:
3条回答
  • 2021-01-30 23:15

    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.

    0 讨论(0)
  • 2021-01-30 23:18

    I solved it by running ssh-add once and use it like if it's not password protected.

    0 讨论(0)
  • 2021-01-30 23:20

    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.

    0 讨论(0)
提交回复
热议问题