2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PodmanでAnsibleを動かす環境構築メモ(RHEL8 + Python3.9対応)

Last updated at Posted at 2023-05-10

はじめに

本記事では、構成管理ツール Ansible を Podman コンテナ 環境で動作させるための手順を紹介します。

Ansible 実行環境をコンテナ化するメリットは以下の通りです。
 ・依存関係をすべてコンテナ内に閉じ込められるため、環境差異に強い
 ・複数のクラウドや仮想マシンでも一貫した動作を保証
 ・テスト・デプロイのポータビリティが高まる

RHEL 8 以降の環境を前提に、Podman を用いたコンテナ構築~実行手順を解説します。

全体構成と作業の流れ

以下は作業の概要と構成イメージです。

✅ 作業ステップ
1. Podman 環境で Ansible コンテナを構築
2. ホストの /etc/ansible をコンテナにマウント
3. Ansible コンテナを起動し、タスクを実行

📘 構成図
1.png

1. Podman のインストール(RHEL 8)

# dnf -y install podman

2. SELinux の一時的/永続的無効化

# 永続的に無効化
# sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config

# 即時無効化(再起動後は元に戻る)
# setenforce 0

3. Dockerfile 作成

Ansible 実行環境となるコンテナをビルドするための Dockerfileを用意します。
RHEL8でサポートされているPython 3.9を選択し、作業効率のために一部必須ではない設定も実施しています。

# vi Dockerfile

★★★以下を追記する★★★

# RHEL8ベースのイメージを使用
FROM registry.access.redhat.com/ubi8/ubi:latest

# 必要なパッケージをインストールするためにEPELリポジトリを追加
RUN dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
# EPELリポジトリをインストール
RUN dnf install -y epel-release
# 必要なパッケージをインストール
RUN dnf install -y python39 python39-pip python39-devel openssh-clients openssl-devel gcc sshpass unzip iputils
# インストール後のキャッシュをクリア
RUN dnf clean all

# AnsibleおよびWindows関連のパッケージをインストール
RUN pip3.9 install --upgrade pip && \
    pip3.9 install ansible==8.4.0 pywinrm

# ロケールを日本語に設定
RUN dnf -y install glibc-locale-source glibc-langpack-en && \
    dnf clean all && \
    rm -rf /var/cache/dnf/*
RUN localedef -f UTF-8 -i ja_JP ja_JP.utf8
RUN echo 'LANG="ja_JP.UTF-8"' >  /etc/locale.conf

# タイムゾーンをJSTに設定
RUN echo 'ZONE="Asia/Tokyo"' > /etc/sysconfig/clock
RUN rm -f /etc/localtime
RUN ln -fs /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# コンテナの実行ユーザーをrootに設定
USER root

# デフォルトの作業ディレクトリを設定
WORKDIR /ansible

# 親サーバのディレクトリをマウントするためのディレクトリを作成
VOLUME ["/ansible"]

# エイリアスを設定 (llコマンド)
RUN echo "alias ll='ls -l'" >> /etc/profile.d/aliases.sh

# カスタムプロンプトの設定
ENV PS1="[\u@\h ~]# "

# コンテナを実行する際のコマンド (bashシェルを実行)
CMD ["/bin/bash"]

4. コンテナイメージのビルド

# podman build -t ansible-container .

正常にビルドされると、以下のようなメッセージが表示されます。
Successfully tagged localhost/ansible-container:latest

5. Ansible ディレクトリの作成(ホスト側)

# mkdir /etc/ansible

6. Ansible コンテナの起動

# podman run -it --rm \
  --hostname ansible-container \
  -v /etc/ansible:/ansible \
  ansible-container

・ --rm:終了時に自動でコンテナ削除
・ --hostname:コンテナ内のホスト名指定
・ -v:ホストの /etc/ansible を /ansible にマウント

7. Ansible バージョンの確認

[root@ansible-container ~]# pip freeze | grep ansible

出力例:
ansible==8.4.0
ansible-core==2.13.8

8. Ansible 実行準備完了

Ansible コンテナの構築は完了しました。
あとは /etc/ansible に以下の資材を配置しておくことで、実行準備が整います。
 ・ ansible.cfg
 ・inventory ファイル(hosts)
 ・roles/ ディレクトリ など

【応用編】ビルド済みコンテナの別環境への移行

1. コンテナをイメージとして保存

構文:「podman save -o 出力パス コンテナイメージ名」

# podman save -o ansible-container.tar localhost/ansible-container

2. イメージファイルを別環境に転送

# scp ansible-container.tar root@xxx.xxx.xxx.xxx:~/

3. 別環境でイメージを復元

# podman load -i ansible-container.tar

おわりに

本記事では、Podman 上に Ansible 実行環境を構築する方法を紹介しました。

コンテナ化により、Ansible をより安全・効率的・再現性の高い方法で実行できるようになります。検証環境だけでなく、本番でも活用できる柔軟な手法です。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?