LoginSignup
3

More than 5 years have passed since last update.

GCP Cloud Shell 上から Ansibleを利用する

Last updated at Posted at 2017-12-11

使い始めたばかりで Cloud Shell事がまだ理解できていないかもしれませんので誤りがあるかもしれません。

やりたいこと

  • Cloud Shell上でAnsibleの環境を作る

Home Directoryが5GByteあり永続的に利用できるとのことなので全てのモジュールがローカルにインストールできればずっと使えるだろうという安直な考え

導入

pyenvの導入

curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

ファイルの作成

.bash_profileを以下の内容で作成します。

~/.bash_profile
export PATH="/home/bmxlesson/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

pythonの導入

pyenv install 2.7.14
pyenv global 2.7.14
python --version
  • 途中で The Python bz2 extension was not compiled. Missing the bzip2 lib? というエラーがでますが今回は気にしないでおいておきます。

Ansibleの導入

pip install ansible

結果ホームディレクトリ以下に導入されました。

$ ansible --version
ansible 2.4.2.0
  config file = None
  configured module search path = [u'/home/bmxlesson/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/bmxlesson/.pyenv/versions/2.7.14/lib/python2.7/site-packages/ansible
  executable location = /home/bmxlesson/.pyenv/versions/2.7.14/bin/ansible
  python version = 2.7.14 (default, Dec 11 2017, 16:55:06) [GCC 4.9.2]

Ansibleの利用

AnsibleからGCPを使うことを調べてみるとInventoryと操作とどちらも出来るようですね。次回試してみたいと思います。

ここでは ping をまず試してみます。暫定的にエフェメラルIPを直接書いています。きちんとするならInventoryを利用するのが正しいのだと思いますが動作確認なので

~/ansible/inventory/hosts

35.227.19.xxx ansible_ssh_private_key_file=/home/bmxlesson/.ssh/google_compute_engine

で実行してみます。

$ ansible -i ansible/inventory/hosts all -m ping
35.227.19.xxx | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

という感じで起動したので一応動いている模様です。Cloud Shellでは自動的にVMインスタンスに接続するためのSSH鍵が自動的に作られているようなのでこれで接続は出来ます。この辺は楽ですね。実際の運用において楽がいいことかはケースバイケースですが今は楽で便利です。

次の記事
* AnsibleでGCP のVMインスタンスを操作する

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
3