GCE上でAnsibleのチュートリアル準備

GCE上でちょこっとAnsibleを試そうと思ったときに、思いのほか認証周りが面倒だったからメモっておきます。 Ansibleをそもそもまともに使ったことがないのが原因かも。

ansibleのインストール

GCE上のCentOS6でのインストール virtualenvでpython2.7環境で

workon ansibleenv
pip install ansible

External IPなしのインスタンス作成

こんなかんじで 節約のため --preemptible で一日インスタンス 外部IPなしは --no-address

gcloud compute \
  --project "shaped-infusion-632" instances create "test01" \
  --zone "asia-east1-a" \
  --machine-type "n1-standard-1" \
  --network "default" \
  --no-address \
  --no-restart-on-failure \
  --maintenance-policy "TERMINATE" \
  --preemptible \
  --scopes "https://www.googleapis.com/auth/devstorage.read_only" "https://www.googleapis.com/auth/logging.write" \
  --tags "http-server" "https-server" \
  --image "https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-6-v20150710" \
  --boot-disk-type "pd-standard" \
  --boot-disk-device-name "test01"

ansibleの疎通確認

ふつうにやってみる

echo "test01" > hosts
ansible -i hosts test01 -m ping

うまくいかない

test01 | FAILED => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

鍵を指定する

echo "test01 ansible_ssh_private_key_file=~/.ssh/google_compute_engine" > hosts
ansible -i hosts test01 -m ping

結果はまだダメ

test01 | FAILED => SSH Error: Host key verification failed.
    while connecting to 相手のIP
    It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

Host Keyがダメっていうから、チェックを環境変数から外す

export ANSIBLE_HOST_KEY_CHECKING=False

これで

ansible -i hosts test01 -m ping

結果が無事帰ってくる

test01 | success >> {
    "changed": false,
    "ping": "pong"
}

認証周りがこれでクリアするので、あとは普通に使えるかと