1
4

More than 3 years have passed since last update.

Docker desktopが導入された環境でAnsibleを最短で試す。

Last updated at Posted at 2020-08-09

追記:
以下のようなイメージもありました。

Docker desktopが導入された環境でAnsible実行環境をdocker runする

環境

  • 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はメンテナンスされていないが、取り敢えず試すのには使える。

https://hub.docker.com/r/ansible/centos7-ansible
THESE IMAGES HAVE BEEN DEPRECATED
Ansible no longer maintains images in Dockerhub directly. There are several Ansible images on Dockerhub that are maintained by members of the Ansible community, which you can find with the following search

ですが最短で簡単に試すにはこれでも良いかと。

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 /]# 

折角なのでlocalhostへのpip installもansibleで行えば良かったかも。

docker buildし直す例


$ 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,は無くても同じでした。

ローカルのplaybookをdocker コンテナで実行する例

$ 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!となる。
1
4
0

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
1
4