在GCP Cloud Shell中使用Ansible
我刚开始使用 Cloud Shell,可能还没有完全理解,所以可能存在错误。
想做的事情
- Cloud Shell上でAnsibleの環境を作る
只要所有模块都能本地安装,由于Home Directory有5G字节可用且可持久使用,那么可以一直使用,这是一种简单直接的想法。
- https://cloud.google.com/shell/docs/?hl=ja
引入
安装pyenv
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
创建文件
请使用以下内容创建.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的库存和操作的情况。
- http://docs.ansible.com/ansible/latest/guide_gce.html
在这里,我们首先尝试进行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的虚拟机实例