LoginSignup
8
13

More than 3 years have passed since last update.

今更Ansible実行環境をDockerで作ってみる

Posted at

概要

ansibleの練習やテストを実行する環境としてDockerを使ってみようと思い、今更ながらDockeransibleの環境構築をしてみる。
まずはローカルでansibleを実行できるコンテナを今回は作成してみる。

Dockerfile作成

Dockerfile
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"]

inventoryファイル作成

hosts
[local]
127.0.0.1:22

playbookファイル作成

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

テキストファイルを作成するだけのplaybook。
これがコンテナ内で実行できれば動作確認OK。

ansible.cfgファイル作成

ansible.cfg
[ssh_connection]
ssh_args = -o StrictHostKeyChecking=no

ssh時のフィンガープリントのチェックをしないように設定。

コンテナ起動

1.Dockerfileからコンテナイメージを作成

docker build -t ansible-docker .

2.コンテナイメージからコンテナを起動

# 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-composeansibleサーバとwebサーバ2台で別サーバに対してplaybookを実行してみる構成を試したい。(どんどんansible自体の勉強から離れていきそう。。)

8
13
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
8
13