我现在想在Docker中创建一个执行Ansible的环境

总结

我想尝试使用Docker作为执行Ansible练习和测试的环境,并且现在才开始尝试使用Docker来构建Ansible环境。
首先,我打算在本地创建一个可以运行Ansible的容器来尝试。

创建Docker文件

FROM centos:7
MAINTAINER soichiro0311

RUN yum install epel-release -y && \
    yum -y update && \
    yum --enablerepo=epel install python2-pip.noarch -y && \
    yum install openssh-server -y && \
    yum install openssh-clients -y && \
    yum reinstall glibc-common -y && \
    yum install sshpass -y && \
    yum remove python27-chardet.noarch -y && \
    pip install --upgrade pip && \
    pip install ansible && \
    pip install pywinrm && \
    yum clean all

ENV LANG ja_JP.UTF-8

# sshでrootログインする際のパスワードを設定
RUN sed -ri 's/^#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    echo 'root:password' | chpasswd && \
    mkdir -p /etc/ansible

# コード管理しているinventoryファイルをコンテナ内にコピー
COPY hosts /etc/ansible

# コード管理しているテスト用のplaybookファイルをコンテナ内にコピー
COPY test.yml /etc/ansible

# ssh時のフィンガープリントのチェックをしないように設定
COPY ansible.cfg /etc/ansible

EXPOSE 22

CMD ["/bin/bash"]

创建库存文件

[local]
127.0.0.1:22

创建playbook文件

- hosts: local
  tasks: 
    - name: test
      shell: touch test.txt

只需创建一个文本文件的playbook。
只要在容器内执行它,就可以确认运行是否正常。

创建ansible.cfg文件

[ssh_connection]
ssh_args = -o StrictHostKeyChecking=no

将SSH时的指纹检查设置为不进行确认。

启动容器

通过 Dockerfile 创建容器镜像。

docker build -t ansible-docker .

从容器映像中启动容器。

# sshdを起動するためにホストのデバイスにアクセスできるようにする必要があったので、--privilegedオプションを追加
docker run -d --privileged ansible-docker sbin/init

3. 进入集装箱

docker exec -it {起動したコンテナのコンテナID} /bin/bash

4. 执行playbook

[root@3d1ea3151836 /]# cd /etc/ansible
[root@3d1ea3151836 ansible]# ansible-playbook -i hosts test.yml --ask-pass
# パスワードを求められるため、passwordと入力
SSH password:

PLAY [local] ****************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [127.0.0.1]

TASK [test] *****************************************************************************************************************************************
 [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file is insufficient
you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

changed: [127.0.0.1]

PLAY RECAP ******************************************************************************************************************************************
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@3d1ea3151836 ansible]# cd ~
[root@3d1ea3151836 ~]# ll
total 4
-rw------- 1 root root 3415 Aug  1 01:10 anaconda-ks.cfg
-rw-r--r-- 1 root root    0 Sep 26 15:58 test.txt #playbookの実行内容が反映されていることが確認できる

試著去搭建環境

    • sshdの起動に関して少しハマった。Dockerコンテナはオプションなしで起動するとデフォルトで全てのデバイスにアクセスできない状態で起動するため、systemd経由でsshdを起動する場合はprivelegedオプションが必要だった。

 

    • playbook実行時にパスワード認証を求められる形で実装してしまったのがイケてない。テスト用や練習用なら鍵認証にして毎回手動でパスワード入れる手間は無くしたい。

 

    次はdocker-composeでansibleサーバとwebサーバ2台で別サーバに対してplaybookを実行してみる構成を試したい。(どんどんansible自体の勉強から離れていきそう。。)
bannerAds