LoginSignup
5
7

More than 3 years have passed since last update.

Ansibleでとりあえず authorized_key を通す

Last updated at Posted at 2015-12-05

Ansible Advent Calendar 2015 の5日目の記事です。

authorized_key モジュール

ansible実行時にSSHのパスワード入力ではなく、公開鍵認証で済ませたい。
そしてその設定1回だけのためにplaybookを書きたくないな~ということで、どう書けるのか試して見ました。

$ ANSIBLE_HOST_KEY_CHECKING=False \
ansible -i hosts all -m authorized_key -a "user=hoge key='`cat $HOME/.ssh/id_rsa.pub`'" -k

ansibleコマンドだと、ドキュメントに記載があるような、lookupが使えるのかわかりませんでしたので、catで文字列を取得しています。

playbookで実施する場合は、素直にドキュメント通りにやりましょう。
http://docs.ansible.com/ansible/authorized_key_module.html

以上です。


(2019.8.15 追記)

ansible-playbook を実行する前に、リモートのauthorized_keyの設定だけは事前に済ませておきたい! という場面で上記のコマンドは便利かと思います。

-k, --ask-pass ask for connection password

を指定し、sshパスワード認証をしつつリモートの$HOME/.ssh/authorized_keys にローカルの公開鍵をコピーします。

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7