0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AnsiblePlaybookでssh-agentを使ってPrivateRepositoryをCloneする

0
Posted at

やりたいこと

  • リモート側でGithubに登録しているキーペアを使ってGit Cloneしたい
  • 単純に公開鍵をリモートへコピーするのでもよいが、セキュリティ的にあまりよろしくない
  • ssh-agentを使うことでssh接続をしているときにのみキーペアがセットされるのでファイルをそのままコピーするよりも良いかなと思う

ssh-agentをセットアップ

ssh-agentのPIDを表示

ssh-agent -s 

環境変数に入れ込む

export SSH_AUTH_SOCK=/tmp/ssh-I990nFpElTyM/agent.17453; export SSH_AUTH_SOCK;
export SSH_AGENT_PID=17454; export SSH_AGENT_PID;

登録している公開鍵を登録する

ssh-add ~/.ssh/id_rsa.pub

登録されているか確認する

ssh-add -l

出力例

3072 SHA256:exB9cunVDbSGaugRw0ePcpWohJQATGI8FiOd56QJ81s yuta@DESKTOP-T85N4KS (RSA)

ansible側のセットアップ

ansible.cfgにssh系の設定を書いておく

  • -o ForwardAgent=yesをつけることで、ローカルの公開鍵をリモート側で使うことができるようにする
ansible.cfg
[defaults]
host_key_checking = False
private_key_file=~/.ssh/id_rsa
transport = ssh

[ssh_connection]
ssh_args = -o ForwardAgent=yes

gitモジュールを使う

  • become_flags: '-E'が必要みたい
    • sudoしたときに環境変数を引き継ぐ設定
main.yaml
- name: Clone a private repository into folder.
  git:
    repo: "git@github.com:test/test.git"
    version: "master"
    dest: "/tmp/test"
    accept_hostkey: yes
    clone: yes
  become_flags: '-E'
  with_items: "{{ services }}"
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?