[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

【Ansible 2.12】CentOS 6用のAnsible実行環境を、WSL2内のDockerで構築

皆様こんにちは。
最近、寝れない程の腹痛と悪寒に襲われ、体力を喪失したシステムソリューション部所属の なか です。

今回は「CentOS 6 環境操作用の Ansible 実行環境を、WSL2 内の Docker で構築」のやり方を書いた記事となります。

CentOS 6 環境といった古い環境は移行を強く推奨されていますが、様々な事情により残ってしまっている事があります。

それら環境において、現行バージョンの Ansible では基本的には対応できません。
そのため手軽に利用できる Docker を使った旧バージョンの環境構築手順を用意してみました。

前提の説明をしつつ、順を追って手順の説明・解説をしていきます。

実行環境

■ Linux 環境
OS : AlmaLinux release 8.5(WSL2環境)
Shell : Bash
Docker : 26.1.0, build 9714adc

■ Windows 環境
OS : Windows11 Pro(バージョン:23H2)
言語設定 : 日本語に変更

■ CentOS 6 環境(Vagrant+VirtualBox)
OS: CentOS 6.9(bento / centos-6.9)
Vagrant : 2.4.1
VirtualBox : 7.0.18 r162988(Qt5.15.2)
IP : 192.168.33.15

前提の説明

Q.何故 CentOS 6 環境向けの Ansible 環境が別途必要なの?

A. 新しいバージョン(2.13以降)の Ansible だと、CentOS 6 標準の Python2.6 が対応外となり、基本的には Playbook を流し込めなくなったため

Q.何故 WSL2 内の Docker?

A. メインで使っている Ansible 環境が、AlmaLinux 8(WSL2)を使用しており、その中で実行できる方が Playbook の管理も含めて楽なため

Q.pyenv 系ではなく Docker?

A. 要約:Docker の方が筆者の立場的に便利で楽なため

  1. (利用頻度が高くないので)Python バージョン管理に環境すると、PC移行時の再構築が面倒になるため
  2.  Dockerfileの方が他の人が同じ環境を作る際に使用でき、再利用性が高いため
  3.  Python バージョン管理より Docker を触るほうが、インフラエンジニア的に楽しい知見になるため
  4. (筆者の心理的に)何を入れたのか忘れても Dockerfile を見れば何とかなるのは凄く楽

Q.何故 Ansible-core 2.12なの?

A. 2.12 がマネージドノードの Pythonが2.6 に対応可能な中で最新のマイナーバージョンのため

※ どうしてもコントロールノード側で Python2.7 や、3.7 で実行したい場合は 2.11 をご利用ください

構築手順

1. WSL2 準備

今回の本題ではないので簡素に解説します。

「Windows の機能」で「仮想マシンプラットフォーム」を有効化します。

その上で、Microsoft Store にて「Windows Subsystem for Linux」と「AlmaLinux 8 WSL」を入れてください。

「AlmaLinux 8 WSL」を起動し、初回起動時のユーザ名とパスワードを設定すれば利用できます。

2. Docker 準備

まずはDockerを入れます。

#dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#dnf install docker-ce

#systemctl start docker
#systemctl enabled docker

#docker --version
Docker version 26.1.0, build 9714adc ※筆者環境の場合

3. Dockefile 作成

作業をディレクトリを決め、そこで Dockerfile を作成します。

筆者は /home/AlmaLinux/docker-study/ という配置で作業していますが、各々ご自由にご設定ください。

# イメージ指定
FROM almalinux:8.9
# タイムスケールを日本時間に変更
RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# 必要パッケージ郡導入
RUN dnf -y install \
    python38 \
    python38-devel \
    sshpass \
    openssh-clients \
 && rm -rf /var/cache/dnf/* \
 && dnf clean all

# Ansible2.12導入
RUN pip3 --no-cache-dir install ansible-core==2.12.10

# バインドマウント先指定&自動作成
# & コンテナにアクセスした際のデフォルト先
WORKDIR /work

FROM almalinux:8.9

WSL2 で使用しているディストリビューション・メジャーバージョンに合わせています。

RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

日本時間に合わせています。

ログを残す場合等で UTC のままだとズレて面倒なので、とりあえず時間を合わせておきたいです。

RUN dnf -y install \

AlmaLinux 8 の Appstream リポジトリにある物、python3.8(3.9でも良い)

そして、ssh関係で sshpass 及び イメージに openssh-client が入ってない事があるためそれらを導入

&& rm -rf /var/cache/dnf/* \

dnf(≒yum) のキャッシュを削除して Docker イメージの容量を削減します。

後述の dnf clean all コマンドで消えない事があるようなので、まずは手動で消します。

&& dnf clean all

dnf(≒yum) のキャッシュを削除して Docker イメージの容量を削減します。

なんで "RUN" じゃなくて "&&" ?

Docker イメージの容量削減のため。

RUN は実行の度に、Docker イメージ内のレイヤが増えるため容量が増加します。
そのため、1回の RUN でまとめられる部分はまとめておくことで容量を抑えていきます。

RUN pip3 --no-cache-dir install ansible-core==2.12.10

Ansible 2.10 以降から 2.9 以前とは形式が変わったため、軽量化された本体部分の 「ansible-core」 を指定します。
その中で、2.12 の最新リリースである 2.12.10 を指定。

昔から利用している Playbook を流用したい場合は、互換性的に「ansible==2.9.27(もしくは昔利用していたバージョン)」と指定した方が良いでしょう。
※ 古すぎるバージョンの場合、Python2系や3.8以前を要求されると思いますので、調整が必要になります。

補足:Collectionについて

「ansible-core」は最小構成のため、使用できるのは基本Collection(ansible.builtin)のみとなります。

「ansible-core 2.12.10」を使いつつ多数のモジュールを使いたい場合は、「ansible-core 2.12.10」とモジュール関係がセットになったコミュニティパッケージの「ansible==5.10.0」を指定すると良いでしょう。

ただし、イメージサイズが大きくなります。
また、Playbookで必要とするCollectionをインストールさせる書き方をしているのであれば「ansible-core」のみでも問題ないかと思います。

Collection関係とバージョン対応はややこしいため、今回の記事では基本となる「ansible-core」のみを利用する形で記載しています。

WORKDIR /work

Dockerfile 内で作業ディレクトリを指定する物です。
今回はバインドマウントする際の配置先ディレクトリを指定しています。

WORKDIR で指定すると mkdir(Linux側コマンド)しなくても自動で作成されるため、記述量が減ります。
またコンテナに入った際も設定が継続するので、これにより直接バインドマウント先から作業を開始できます。

4. docker build

Dockerfile からイメージを作り出します。

#docker build -t ansible2.12:v1 -f Dockerfile .

#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ansible2.12 v1 58f4663b655b 14 seconds ago 286MB

リポジトリ名「ansible2.12」タグ名「v1」という形で、イメージを無事に作ることできました。

5. docker run(バインドマウント)

筆者は WSL2 と VScode を連携して Playbook 等々を書いています。

そのため、Ansible 2.12 作業用に work というディレクトリを用意して、共有ディレクトリとしてバインドマウントさせます。

#cd /home/ユーザー名/docker-study ※筆者のworkを配置した検証用ディレクトリ
#docker run -it --name Ansible2.12 --mount type=bind,src=./work,dst=/work ansible2.12:v1

ブログ公開用に src=./work と相対パスで書きましたので、上記実行時はそれぞれ「work」ディレクトリがある場所で実行してください。
絶対パスの記述でも問題ないので、その辺は好き好きです。

6. 実行環境起動

それでは走らせましょう。

#docker run -it --name Ansible2.12 --mount type=bind,src=./work,dst=/work ansible2.12:v1

[root@cf3c2fa0c4d8 work]#

実行と同時に「-it」オプションでコンテナに接続しました。
WORKDIR も効いており「/work」に移動した状態になっていますね。

7. pingテスト

Ansible に問題がないかの簡単なテストとして、 ansible-core の標準コレクション(ansible.builtin)にある ping モジュールを使ってみましょう。

[root@cf3c2fa0c4d8 work]# ansible -m ping -i hosts targetnode
[DEPRECATION WARNING]: ansible-core 2.13 will require Python 2.7 or newer on the target. Current version: 2.6.6
(r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]. This feature will be removed in version
2.13. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
targetnode | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}

警告が出ていますが、ターゲット(マネージドノード)の CentOS 6(Python2.6)のため
「ansible-core 2.13 から Python2.7 以降しか使えないよ(警告)」と通知されているだけなのでスルーで問題ありません。

検証用の CentOS 6 環境に無事に ping が届いていますね。

完成

以上で完成となります。

後は playbook と Inventory を配置すれば作業可能です。

次回以降の記事で CentOS 6 対象の Ansible 記事を書こうと思います。

最後に

CentOS 6 環境で Ansible でサクっとやろうとしたのに、現行バージョンでは実行できない。
専用の環境構築とそれに伴う検証が面倒だから結局手動で……という起こりえそうな悲しみを回避すべく、事前に準備したのが本記事のキッカケです。

その中で手っ取り早いと思って Docker を使いましたが、やはり手軽で便利ですね。(運用面に関しては目を瞑ります)
インフラエンジニア業務で今後更に活用していければと思いました。

この記事を読んだ方に多少でも役に立つ知識/情報となれれば幸いです。
ここまで読んで頂きありがとうございました。

参考資料

Releases and maintenance | Ansible公式
https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html

Dockerfile リファレンス — Docker-docs-ja 24.0 ドキュメント
https://docs.docker.jp/engine/reference/builder.html

バインドマウントの利用 | Docker ドキュメント
https://docs.docker.jp/storage/bind-mounts.html

レイヤ — Docker-docs-ja 24.0 ドキュメント | Dockerドキュメント
https://docs.docker.jp/build/guide/layers.html

クラウド / サーバー設計・構築なら
(サービスページはコチラ)

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
1
読み込み中...
1 票, 平均: 1.00 / 11
47
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

inside

Beyond mid-career in 2022 Belongs to
the System Solutions Department
LPIC-3 I have a 304 and AWS SAA I only
have three choices for regular drinks: milk, cola, and black tea.