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?

Ansible Automation Platform (コンテナ版)のジョブログをホストVMに出力する方法

0
Last updated at Posted at 2026-06-07

はじめに

Ansible Automation Platform (コンテナ版) でジョブ実行時のログを、ホスト VM 上のファイルに書き出す方法をまとめました。
コンテナ版の AAP ではジョブが隔離された環境 (isolated job) で動くため、ログをホスト側に残すにはいくつかの設定が必要です。本記事ではその手順を順を追って説明します。


今回のポイントは以下の3つです。

  • AAPの設定でホストディレクトリをコンテナにマウント
  • SELinuxでコンテナから書き込み可能にする
  • ansible.cfgでログ出力先をマウント先に指定

動画

当記事の動画を作成しました。理解の一助にご参照ください。


環境

Ansible Automation Platform 2.6
AWS EC2 上の RHEL10 に構築(コンテナ版)

設定の流れ

  1. AAP GUI でボリュームマウントを設定
  2. ホスト VM でディレクトリ作成 & SELinux 設定
  3. ansible.cfg でログ出力先を指定
  4. 稼働確認

1. AAP GUI 設定

AAP GUI - 設定 - ジョブ に移動し、右上の編集 を押します

aap_jobsetting1.png

Paths to expose to isolated jobs にホストでマウントする場所、コンテナのマウント先情報を記入し、Save を押します。

- /home/ansible/log:/mnt/logs:rw

aap_jobsetting2.png

設定が反映されていることを確認。

aap_jobsetting3.png


2. AAP ホストVMの設定

  • ログ保存用ディレクトリを作成
$ mkdir -p /home/ansible/log
  • SELinux 設定
$ sudo semanage fcontext -a -t svirt_sandbox_file_t '/home/ansible/log(/.*)?'
  • ポリシーを実際に適用
$ restorecon -Rv /home/ansible/log
/home/ansible/log not reset as customized by admin to unconfined_u:object_r:container_file_t:s0

restorecon 実行時に以下のメッセージが出ることがありますが、正常です。
/home/ansible/log not reset as customized by admin to unconfined_u:object_r:container_file_t:s0
svirt_sandbox_file_t と指定しても、semanage が内部で現行の型名 container_file_t に自動解決します。

  • コンテキストの確認
$ ls -laZ /home/ansible/log
total 4
drwxrwxrwx.  2 ansible ansible unconfined_u:object_r:container_file_t:s0   22 Jun  4 05:17 .
drwx------. 17 ansible ansible unconfined_u:object_r:user_home_dir_t:s0  4096 Jun  4 05:14 ..

container_file_t が付与されていれば OK です。

container_file_t とは?

コンテナからアクセス可能なSELinuxタイプ。SELinuxがブロックすると書き込みできないので許可設定を行っています。


3. anislbe.cfg の設定

プロジェクト内の ansible.cfg にログ出力先を指定します。
パスはコンテナ内のマウント先 (/mnt/logs/) を指定する点に注意してください。

ansible.cfg
[defaults]
log_path = /mnt/logs/test.log

4. 稼働確認

ログファイルを事前に作成

$ touch /home/ansible/log/test.log
$ ls -l /home/ansible/log/test.log
-rw-r--r--. 1 ansible ansible 0 Jun  4 05:16 /home/ansible/log/test.log

ansible.cfg で log_path 確認

$ grep log_path ansible.cfg
log_path = /mnt/logs/test.log
  • テスト用のプレイブック

動作確認用に以下のシンプルなプレイブックを用意しました。

test.yml
---
- name: ログ出力テスト用プレイブック
  hosts: localhost
  gather_facts: no

  tasks:

    - name: 開始メッセージを表示
      debug:
        msg: "プレイブックの実行を開始します"

    - name: テストファイルを作成
      copy:
        content: "これはテストファイルです"
        dest: /tmp/testfile.txt

    - name: ファイルの存在確認
      stat:
        path: /tmp/testfile.txt
      register: file_stat

    - name: ファイル状態を表示
      debug:
        var: file_stat

    - name: 終了メッセージを表示
      debug:
        msg: "プレイブックの実行が完了しました"
  • test.yml を設定したジョブテンプレートを実行

test.yml を設定したジョブテンプレートを AAP GUI から実行します。

test.yml実行.png

test.yml実行結果.png

  • ジョブログ確認

ジョブ完了後、ホスト側でログが生成されていることを確認します。

$ ls -l /home/ansible/log
total 4
-rw-r--r--. 1 ansible ansible 3033 Jun  4 05:21 test.log
$ cat /home/ansible/log/test.log
2026-06-04 05:21:34,167 p=10 u=root n=ansible | PLAY [ログ出力テスト用プレイブック] ********************************************
2026-06-04 05:21:34,188 p=10 u=root n=ansible | TASK [開始メッセージを表示] ****************************************************
2026-06-04 05:21:34,207 p=10 u=root n=ansible | ok: [localhost] => {
    "msg": "プレイブックの実行を開始します"
}
2026-06-04 05:21:34,212 p=10 u=root n=ansible | TASK [テストファイルを作成] ****************************************************
2026-06-04 05:21:35,019 p=10 u=root n=ansible | changed: [localhost]
2026-06-04 05:21:35,024 p=10 u=root n=ansible | TASK [ファイルの存在確認] ******************************************************
2026-06-04 05:21:35,303 p=10 u=root n=ansible | ok: [localhost]
2026-06-04 05:21:35,308 p=10 u=root n=ansible | TASK [ファイル状態を表示] ******************************************************
2026-06-04 05:21:35,332 p=10 u=root n=ansible | ok: [localhost] => {
    "file_stat": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1780550494.9845726,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 8,
            "charset": "unknown",
            "checksum": "354cb7b3ba01c75754602a07c2b07f1f4d492c9e",
            "ctime": 1780550494.9845726,
            "dev": 1048671,
            "device_type": 0,
            "executable": false,
            "exists": true,
            "gid": 0,
            "gr_name": "root",
            "inode": 109156328,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mimetype": "unknown",
            "mode": "0644",
            "mtime": 1780550494.651571,
            "nlink": 1,
            "path": "/tmp/testfile.txt",
            "pw_name": "root",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 36,
            "uid": 0,
            "version": null,
            "wgrp": false,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    }
}
2026-06-04 05:21:35,337 p=10 u=root n=ansible | TASK [終了メッセージを表示] ****************************************************
2026-06-04 05:21:35,358 p=10 u=root n=ansible | ok: [localhost] => {
    "msg": "プレイブックの実行が完了しました"
}
2026-06-04 05:21:35,375 p=10 u=root n=ansible | PLAY RECAP *********************************************************************
2026-06-04 05:21:35,376 p=10 u=root n=ansible | localhost                  : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

ホスト VM 上にログが出力されていることが確認できました。


まとめ

コンテナ版 AAP でジョブログをホストVM に出力するには、以下の 3 点がポイントです。

  • AAP GUI: Paths to expose to isolated jobs にホスト↔コンテナのパスマッピングを追加
  • ホスト VM: SELinux コンテキスト (container_file_t) をログディレクトリに付与
  • ansible.cfg: log_path にコンテナ内のマウント先パスを指定

SELinux の設定を忘れると書き込みが拒否されてログが出力されないため、注意が必要です。

以上、ご参考まで。

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?