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

WSLでAnsible実行環境を構築

Posted at

クラウドが主流になってから、以前よりかはAnsibleの登場機会も減ったと思いますが、オンプレミスもクラウドも両方触れるインフラエンジニアの場合、まだまだ使う人は多いと思います。
今回WSLでAnsibleの実行環境を構築する機会があったので、ついでにブログに記載しようと思います。

前提環境

windows11でwsl2を利用
WSL 上で動作するUbuntu 20.04.4 LTS
これから構築するAnsibleのバージョン2.18
Pythonバージョン 3.13

公式ドキュメント
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

Ansibleの実行環境構築

バージョン選定

以下のドキュメント情報を読んで、Ansibleのバージョンとサポートされているpythonのバージョンを確認して、インストールするAnsibleのバージョンを決めます。
https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#support-life

サポートの期限なども考慮して今回は作業時点で最新だった2.18を利用することにしました。
そして2.18を利用するためにはコントロールノードのPythonバージョンは[Python 3.11 - 3.13]だったので、現在のPythonのバージョンを確認します。

Pythonのインストール

python3 -V
Python 3.8.10

現在はまだ古い3.8.10だったのでこれを3.13*にバージョンアップさせたいのでまずリポジトリを確認。

sudo apt list python3.*

<一部省略>
python3.9-minimal/focal-updates,focal-security 3.9.5-3ubuntu0~20.04.1 amd64
python3.9-venv/focal-updates,focal-security 3.9.5-3ubuntu0~20.04.1 amd64
python3.9/focal-updates,focal-security 3.9.5-3ubuntu0~20.04.1 amd64

3.13がaptのリポジトリにまだ無かったので、wgetでソースコードを取りに行ってからインストールします。

sudo wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tgz
sudo tar xvf Python-3.13.0.tgz
./configure --enable-optimizations
sudo make altinstall

この時点でインストールは完了していますが、python3コマンドを実行すると、まだpython3.8が機能してしまう状態なので以下の切り替えの対応をします。

python3 -V
Python 3.8.10
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.13 1
Python 3.13.0

これでpythonのバージョン切り替えは終了です。

Ansibleのインストール

以下コマンドでAnsibleのインストールをします。

python3 -m pip install --user ansible

の結果SSL errorが出てしまいました。

cd /Python-3.13.0
sudo make clean
sudo ./configure --enable-optimizations
sudo make -j$(nproc)
sudo make altinstall
python3.13 -c "import ssl; print(ssl.OPENSSL_VERSION)"
python3.13 -m pip install --user ansible
WARNING: The scripts … are installed in '/home/.local/bin' which is not on PATH.

The scripts … are installed in '/home/.local/bin' which is not on PATH.とかかれているため、まだPATHが通っていない状況のため、以下でPATHを通す

echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Ansibleのバージョン確認コマンドが利用できる=Ansibleがインストールできて、PATHが通っていることになるので以下実行して確認

ansible --version
ansible [core 2.18.5]
  config file = None
  configured module search path = ['/home/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/.local/lib/python3.13/site-packages/ansible
  ansible collection location = /home/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/.local/bin/ansible
  python version = 3.13.0 (main, May  3 2025, 23:23:27) [GCC 9.4.0] (/usr/local/bin/python3.13)
  jinja version = 3.1.6
  libyaml = True

SSHキー設定

Ansibleは管理サーバーと実行サーバー間はsshでやり取りするため、実行サーバーにはssh公開鍵を持たせるようにします。
実行サーバー側で以下操作して公開鍵を持たせます。

cd ~
mkdir .ssh
chmod 700 .ssh
cd .ssh
touch authorized_keys
chmod 600 authorized_keys 
vim authorized_keys #wsl内の公開鍵を張り付ける

実行テスト

管理サーバー側でホストファイルを作成します。
操作したい実行サーバー情報はhostsファイルに纏めます。

vi hosts

hostsファイルの中身は以下です。
[webservers]の下に実行サーバーのIPアドレスを記載
[webservers:vars]の下にssh接続するユーザー名と、管理サーバーの秘密鍵のPATHを書きます。

[webservers]
<IPaddress>

[webservers:vars]
ansible_ssh_user=dev
ansible_ssh_private_key_file=~/dev-ssh-key

宛先サーバーへ接続できるか以下で確認

ansible webservers -i hosts -m ping

ansible.cfg
[defaults]
hostfile=./hosts
remote_user=dev
private_key_file=./.ssh/dev-ssh-key

[privilege_escalation]
become = True

become = True
となっており成功していることが確認できました。
これで実行環境自体は作成できたので、ここからplaybookを書いて設定を進めていきます。

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