在已安装Docker桌面的环境中,尝试使用最短的时间运行Ansible
附注:也存在以下这样的印象。
在安装了Docker桌面的环境中,使用docker run来创建Ansible执行环境。
环境
-
- MAC (Windowsでは未確認だがdockerが動くなら問題無いでしょう)
- Docker desktop
执行
$ docker run -it --rm ansible/centos7-ansible:stable
...
[root@900dd389c443 ~]# cat <<EOC > hello.yml # もちろん $ vi hello.yml などでも
> - hosts: localhost
> gather_facts: false
> tasks:
> - debug:
> msg: hello world
> EOC
[root@900dd389c443 ~]# ansible-playbook -i localhost, hello.yml
PLAY [localhost] ***************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "hello world"
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
使用ansible/centos7-ansible进行测试是可行的,尽管它没有维护。
这些镜像已经被废弃
Ansible不再直接在Dockerhub上维护镜像。有几个由Ansible社区成员维护的Ansible镜像可在Dockerhub上找到,您可以用以下搜索找到它们。
但是如果要尽快且简单地尝试一下的话,这样也可以。
Dockerfile:
https://hub.docker.com/r/ansible/centos7-ansible/dockerfile
还可以用-v选项共享目录,有各种可能性。
Ansible环境在容器上
$ docker run -it --rm ansible/centos7-ansible:stable
[root@a7b5e990df51 /]# ansible --version
ansible 2.2.0.0
config file =
configured module search path = Default w/o overrides
# とりあえずコンテナ内でpip install --upgrade を行ってみる
[root@a7b5e990df51 /]# export LANG=en_US.utf8
[root@a7b5e990df51 /]# pip install --upgrade pip
...
[root@a7b5e990df51 /]# pip install --upgrade ansible
...
[root@a7b5e990df51 /]# ansible --version
/usr/lib64/python2.7/site-packages/cryptography/__init__.py:39: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
CryptographyDeprecationWarning,
ansible 2.9.11
config file = None
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
[root@a7b5e990df51 /]#
可能由于这种情况,也许可以使用Ansible在本地主机上进行pip安装。
重新构建 Docker 的示例。
$ git clone git@github.com:ansible/ansible-docker-base.git # Dockerfileをダウンロードする為なので、Dockerfileを直接書くなら不要。
...
$ cd ansible-docker-base/stable-centos7/
$ cat Dockerfile
# Latest version of centos
FROM centos:centos7
MAINTAINER Toshio Kuratomi <tkuratomi@ansible.com>
RUN yum clean all && \
yum -y install epel-release && \
yum -y install PyYAML python-jinja2 python-httplib2 python-keyczar python-paramiko python-setuptools git python-pip
RUN mkdir /etc/ansible/
RUN echo -e '[local]\nlocalhost' > /etc/ansible/hosts
RUN pip install ansible
$ docker build -t ansible:1 . # ansible/centos7-ansible:latestなどと指定することも可能
...
$ docker run -it --rm ansible:1
[root@0870b36022ab /]# ansible --version
ansible 2.9.11
config file = None
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
由于Dockerfile没有指定版本,因此Ansible会变成最新版本。
由于在/etc/ansible/hosts中添加了localhost,因此执行ansible-playbook时的-i localhost可以省略,结果是一样的。
在docker容器中执行本地的playbook示例
$ cat ./hello.yml
- hosts: localhost
gather_facts: false
tasks:
- debug:
msg: hello world
$ docker run -it --rm -v $(pwd):/playbook ansible/centos7-ansible:stable ansible-playbook /playbook/hello.yml
PLAY [localhost] *************************************************************************************************
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"msg": "hello world"
}
PLAY RECAP *******************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
使用选项-v,将$(pwd)共享为/playbook。
使用docker容器中的ansible,使用本地的ssh密钥来在外部节点上执行本地playbook的示例。
$ cat hello.yml
---
- hosts: all
gather_subset:
- "!all" # <= minの情報のみ取得
tasks:
- debug:
msg: hello world
- debug:
var: ansible_distribution
- debug:
var: ansible_os_family
- debug:
var: ansible_distribution_major_version
- debug:
var: ansible_distribution_version
- debug:
var: ansible_distribution_release
$ docker run -it --rm \
> -v ~/.ssh/id_rsa:/root/.ssh/id_rsa \
> -v ~/.ssh/config:/root/.ssh/config \
> -v $(pwd):/playbook \
> ansible/centos7-ansible \
> ansible-playbook -i p8126a, /playbook/hello.yml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
[WARNING]: Platform aix on host p8126a is using the discovered Python
interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/referen
ce_appendices/interpreter_discovery.html for more information.
ok: [p8126a]
TASK [debug] *******************************************************************
ok: [p8126a] => {
"msg": "hello world"
}
TASK [debug] *******************************************************************
ok: [p8126a] => {
"ansible_distribution": "AIX"
}
TASK [debug] *******************************************************************
ok: [p8126a] => {
"ansible_os_family": "AIX"
}
TASK [debug] *******************************************************************
ok: [p8126a] => {
"ansible_distribution_major_version": "7"
}
TASK [debug] *******************************************************************
ok: [p8126a] => {
"ansible_distribution_version": "7.2"
}
TASK [debug] *******************************************************************
ok: [p8126a] => {
"ansible_distribution_release": "2"
}
PLAY RECAP *********************************************************************
p8126a : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
-
- ~/.ssh/configにて行っているProxyCommandを使用した踏み台経由ssh接続設定を引き継いで使用
-
- docker buildしなおしたansible 2.9.11イメージを使用している
なお、ansible/centos7-ansible:stableのansible v2.2環境で実行すると、ansible_distribution_major_versionが、VARIABLE IS NOT DEFINED!となる。