【RHCE対策】Windows WSL2にansible-navigatorをインストールする(②Playbook実行のための環境設定)


はじめに

前回の記事ではWSL2上にansible-navigatorをインストールしました。せっかくansible-navigatorをインストールしたのですが、すぐにPlaybookを実行することができません。この記事では、Playbookを実行できるようにするための最低限の環境設定について書きます。

環境構築の目的

Ansibleを学習する目的で、ansible-navigatorを使って、Playbookを実行したり、実行環境のイメージを作ったり、といった作業ができる環境を構築します。実際にマシンの構築目的で使う環境ではありません。

以下のような最低限の練習環境をWSL2上のRHEL 9に作ります。Playbookを実行するコントロールノードと管理対象ノードが同じという構成です。

  • studentという名前のユーザーを登録する。

  • インベントリーには127.0.0.1のみ登録する。

  • ローカルの通信なのでSSHは使わない。

このようなシングルノードの環境ではありますが、モジュール、ロール、プラグインの使い方を確認したり、カスタム実行環境イメージを作って検証したりすることができます。

ユーザーの作成

rootでPlaybookを動かすのは気持ちが悪いのでユーザーを作ります。コマンドプロンプトからwslコマンドを実行してRHELを起動します。studentというユーザーを作成し、sudoで昇格できるように/etc/sudoersを設定します。studentユーザーができたら、これ以降はstudentになって作業します。

C:\Users\fusay>wsl
# adduser student
# passwd student
Changing password for user student.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
#echo "student  ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
# su - student
$ whoami
student

ansible.cfg

studentユーザーでPlaybookの実行をして、sudoで権限を昇格するようにansible.cfgを設定します。インベントリーファイルの名前はinventoryとします。

[defaults]
inventory = inventory
remote_user = student
deprecation_warnings = false
gathering=explicit

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

サンプル Playbook

inventory

インベントリーファイルには、管理対象マシンとして自分自身に設定します。自分自身にSSHで通信をする必要はないので、ansible_connection=localを設定します。

[workstation]
127.0.0.1 ansible_connection=local

Playbook

テスト用に hello.yaml という簡単なPlaybookを作ります。

---
- name: Sample playbook
  hosts: workstation
  tasks:
   - name: Print message
     ansible.builtin.debug:
       msg: Hello world

ここまで作成したファイルは以下のようになります。作業ディレクトリとして/home/stuent/testを作成しています。

[student@exstream test]$ pwd
/home/student/test

[student@exstream test]$ ls -l
total 12
-rw-r--r-- 1 student student 205 Jul  5 12:15 ansible.cfg
-rw-r--r-- 1 student student 135 Jul  5 12:15 hello.yaml
-rw-r--r-- 1 student student  50 Jul  4 00:21 inventory

Playbookの実行

ここまでで設定ファイルの準備は整いましたが、/home/stuent/testのディレクトリにおいてansible-navigatorでhello.yamlを実行すると、localeを初期化できないというエラーが出てしまいます。

$ ansible-navigator run hello.yaml -m stdout
Warning: Issues were found while applying the settings.
   Hint: Command provided: 'run hello.yaml -m stdout'

  Error: 'ansible --version' reported the following errors:
         ERROR: Ansible could not initialize the preferred locale: unsupported
         locale setting

   Note: Configuration failed, using default log file location.
         (/home/student/work/ansible-navigator.log) Log level set to debug
   Hint: Review the hints and log file to see what went wrong.

ansible-playbookコマンドでも同様なエラーになります。

$ ansible-playbook hello.yaml
ERROR: Ansible could not initialize the preferred locale: unsupported locale setting

WSL用のRHELイメージにはglibc-langpackがインストールされていないようです。この環境では日本語を使う必要がないので、glibc-langpack-enをインストールしておきます。

$ sudo dnf install -y glibc-langpack-en
<略>
Installed:
  glibc-langpack-en-2.34-100.el9_4.12.x86_64

Complete!

再度、ansible-navigatorを実行します。

$ ansible-navigator run hello.yaml -m stdout
--------------------------------------------------------------------------------------------------------------
Execution environment image and pull policy overview
--------------------------------------------------------------------------------------------------------------
Execution environment image name:     registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest
Execution environment image tag:      latest
Execution environment pull arguments: None
Execution environment pull policy:    tag
Execution environment pull needed:    True
--------------------------------------------------------------------------------------------------------------
Updating the execution environment
--------------------------------------------------------------------------------------------------------------
Running the command: podman pull registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest
Trying to pull registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest...
Error: initializing source docker://registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest: unable to retrieve auth token: invalid username/password: unauthorized: Please login to the Red Hat Registry using your Customer Portal credentials. Further instructions can be found here: https://access.redhat.com/RegistryAuthentication
  Error: Execution environment pull failed
   Hint: Check the execution environment image name, connectivity to and
         permissions for the registry, and try again

今度は、実行環境のイメージをプルするところで止まっています。studentユーザーで、registry.redhat.ioにログインします。

$ podman login registry.redhat.io
Username: neverbird
Password:
Login Succeeded!

これで実行環境イメージを使ってPlaybookが実行できるようになりました。

$ ansible-navigator run hello.yaml -m stdout
--------------------------------------------------------------------------------------------------------------
Execution environment image and pull policy overview
--------------------------------------------------------------------------------------------------------------
Execution environment image name:     registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest
Execution environment image tag:      latest
Execution environment pull arguments: None
Execution environment pull policy:    tag
Execution environment pull needed:    True
--------------------------------------------------------------------------------------------------------------
Updating the execution environment
--------------------------------------------------------------------------------------------------------------
Running the command: podman pull registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest
Trying to pull registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest...
Getting image source signatures
Checking if image destination supports signatures
Copying blob 653abc57dbe4 skipped: already exists
Copying blob dc35b837139a skipped: already exists
Copying blob ecb69917dbda skipped: already exists
Copying blob 0d20ea2422e4 skipped: already exists
Copying config 39a93719fc done   |
Writing manifest to image destination
Storing signatures
39a93719fc259d5df31b52dec6cd8ff3654e92d4b7fdc76d94b4355d731e0df0

PLAY [Sample playbook] *********************************************************

TASK [Print message] ***********************************************************
ok: [127.0.0.1] => {
    "msg": "Hello world"
}

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

おわりに

これでようやくPlaybookの実行を試すことができるようになりました。Playbookで使用するモジュールによっては実行環境のイメージを切り替えたり、カスタムイメージを作成する必要があるかもしれませんが、そちらは別の記事で紹介することにしましょう。

いいなと思ったら応援しよう!