用Ansible 2.0版本的Docker连接插件,连接到放在GCP上的Docker容器的备忘录
首先
尝试使用 Ansible 2.0 这个新增的自动化连接插件 ansible-docker-connection-plugin。之前的先辈们已经留下了许多有参考价值的案例,所以这只是我自己想尝试的备忘录。虽然我当前是在将 GCP 作为 docker-machine,但只要可以正常执行 docker 命令,也可以使用 virtualbox 或其他任何容器。
以下是三个关键点。
-
- インベントリファイルにはコンテナ名を書く
eval $(docker-machine env hogehoge) やってれば ansible から対象コンテナにそのまま繋がる
対象コンテナに python がインストールされている必要がある
准备下来
准备Docker主机
借助GCP(谷歌计算引擎)和Docker Machine构建,同时以newton作为我们家猫的名字。
$ gcloug auth login
$ docker-machine create -d google \
--google-project ${GCLOUG_PROJECT_NAME} \
--google-preemptible \
--google-tags 'http-server,https-server' \
--google-zone asia-east1-a \
--google-machine-type "g1-small" \
newton
$ eval $(docker-machine env newton)
只是附带一提,可以通过以下命令在创建的docker主机上进行ssh连接。
$ docker-machine ssh newton
$ ssh docker-user@$(docker-machine ip newton) -i ~/.docker/machine/machines/newton/id_rsa
到这里还不错。
使用pip安装ansible 2.0
在本地运行 pip install ansible,版本将会安装为2.0.0.1。
$ mkdir hoge
$ cd hoge
$ virtualenv .
$ . ./bin/activate
$ pip install ansible
Collecting ansible
Using cached ansible-2.0.0.1.tar.gz
Collecting paramiko (from ansible)
Using cached paramiko-1.16.0-py2.py3-none-any.whl
Collecting jinja2 (from ansible)
Using cached Jinja2-2.8-py2.py3-none-any.whl
Collecting PyYAML (from ansible)
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./lib/python2.7/site-packages (from ansible)
Collecting pycrypto>=2.6 (from ansible)
Collecting ecdsa>=0.11 (from paramiko->ansible)
Using cached ecdsa-0.13-py2.py3-none-any.whl
Collecting MarkupSafe (from jinja2->ansible)
Building wheels for collected packages: ansible
Running setup.py bdist_wheel for ansible
Stored in directory: /Users/ma2saka/Library/Caches/pip/wheels/7a/e1/81/d35ca9bdfb4da7f44d31db8af498e731795fe2cbe086dd1285
Successfully built ansible
Installing collected packages: ecdsa, pycrypto, paramiko, MarkupSafe, jinja2, PyYAML, ansible
Successfully installed MarkupSafe-0.23 PyYAML-3.11 ansible-2.0.0.1 ecdsa-0.13 jinja2-2.8 paramiko-1.16.0 pycrypto-2.6.1
获取和启动 Nginx 容器
在中国某平台镜像中下载nginx。容器将被命名为nginx01。
$ docker pull nginx
$ docker run -d -p 80:80 --name nginx01 nginx
874e6894f85e81368d30ac7b56003bf8cc2b5f38ccc74790d3cfcda536936c2c
可以确认已经成功启动了。
$ curl $(docker-machine ip newton)
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
在 Nginx 容器中安装 Python
$ docker exec -t nginx apt-get update
$ docker exec -t nginx apt-get install -y python
很遗憾,要在目标容器上使用ansible docker connection执行各种操作,目标容器必须安装了Python。这一点非常令人懊悔。
Ansible与Docker的连接
让我们试试看。
创建一个包含容器名称的清单文件。
创建以下方式的库存文件。
[containers]
nginx01
nginx01 是刚刚创建的容器名称。也就是说,可以通过指定容器名称进行连接。由于 Docker 主机信息是与 docker 命令共享的,所以只要 docker 命令正常工作,就不需要担心它。
用ansible代替nginx的文件。
Nginx默认页面文件位置为 /usr/share/nginx/html/index.html。
让我来重新修改一下这个东西。
我准备了一个像以下的 playbook.yml 文件。
- hosts: all
connection: docker
tasks:
- name: "Nginxの標準ページの書き換え"
replace: >-
dest='/usr/share/nginx/html/index.html'
regexp='nginx'
replace='Nyanginx'
執行
[ma2saka@localhost:hoge]$ ansible-playbook -i hosts playbook.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [nginx01]
TASK [Nginxの標準ページの書き換え] ********************************************************
changed: [nginx01]
PLAY RECAP *********************************************************************
nginx01 : ok=2 changed=1 unreachable=0 failed=0
确认动作
确认引擎X被替换成猫引擎X。
[ma2saka@localhost:hoge]$ curl $(docker-machine ip newton)
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Nyanginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to Nyanginx!</h1>
<p>If you see this page, the Nyanginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://Nyanginx.org/">Nyanginx.org</a>.<br/>
Commercial support is available at
<a href="http://Nyanginx.com/">Nyanginx.com</a>.</p>
Nyanginx
</body>
</html>
成功了。
总结
通过共享Docker命令和连接信息,可以通过容器名称来抽象化连接目标,这很酷。但是,在容器中安装Python是件麻烦的事情。有没有办法解决这个问题呢…?
因为觉得可以通过容器名称解决问题很有趣,所以尝试了一下,但用途啊,嗯,可能就是使用Dockerfile构建Docker容器的基础镜像,并使用与本地和其他现有资源共享的Ansible来配置应用程序层的配置吧。嗯,这就是它的工作方式啊。