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?

linux-mcp-serverでansibleトラブルシューティングを試す

2
Posted at

はじめに

RedHatが公開しているRHEL MCP serverは systemctl statusやjournalctlといった調査コマンドを読み取り専用のツールとしてAIエージェントに公開し、Linuxサーバの調査を行わせるMCPサーバです。
この中身はlinux-mcp-serverのようです。
今回、linux-mcp-serverをセットアップし、Ansible実行のトラブルシューティングを試してみました。

linux-mcp-server概要

動作イメージ

linux-mcp-serverでは通信方式が2種類あります。

  • stdio(デフォルト)
    エージェントと同じマシン上でMCPサーバを起動し、標準入出力で通信する。
  • HTTP(Shared Serverモード)
    MCPサーバを1台のサーバで常駐させ、複数のエージェントからHTTPで接続する。OAuth2認証と、誰がどのホスト・ツールを使えるかを定める認可ポリシー(LINUX_MCP_POLICY_PATH)を設定する。

いずれの場合もターゲット側には何もインストールしません。

サポートされるプラットフォーム

  • MCPサーバを導入するマシン
    Python3.10以上。導入方法はuvまたはコンテナです。
  • ターゲットマシン
    SSHで接続でき、systemdが動作していること。

実装されているツール

ツールの実装はこちらにあり、例えば system_info.pyに実装されている各ツールと実行コマンドは以下の通りです(v1.6.0時点)。

system_info.py
get_system_information  : hostname / cat /etc/os-release / uname -r / uname -m / uptime -p / uptime -s
get_cpu_information     : grep -m 1 "model name" /proc/cpuinfo / grep -c ^processor /proc/cpuinfo
                          / cat /proc/loadavg / top -bn1
get_memory_information  : free -b -w
get_disk_usage          : findmnt --df --json
get_hardware_information: lscpu / lspci / lsusb

読み取り専用について
環境変数LINUX_MCP_TOOLSETで制御されています。
デフォルト(fixed)では読み取り専用ですが、run_script(実行ツールのみ)またはboth(全ツール)に設定することで書き込みを有効化できます。

構成

今回はデフォルトのstdioを使い、エージェントを動かしているマシンにMCPサーバを同居させます。
コントロールノードとターゲットノードを以下の通り用意します。
vpc-2ec2.drawio.png

  • 検証したバージョン
    • コントロールノード: RHEL 10.2 (Amazon EC2)
      • Python 3.12.14
      • ansible-core 2.16.16
      • uv 0.12.21
      • linux-mcp-server 1.6.0
      • AIエージェント: Claude Code 2.1.285 (VS Code拡張機能)
    • ターゲットノード: Amazon Linux 2023 (Amazon EC2)

導入手順

コントロールノードのRHEL10に導入します。

  1. linux-mcp-serverをインストールする。

    コントロールノード
    $ uv tool install linux-mcp-server
    $ uv tool update-shell
    $ uv tool list
    linux-mcp-server v1.6.0
    
  2. ターゲットへのSSHエイリアスを作成する。
    linux-mcp-serverは接続情報を~/.ssh/configから取得します。

    ~/.ssh/config
    Host target
        HostName xxx.xxx.xxx.xxx
        User ec2-user
        IdentityFile ~/awskey-20260930.pem
        IdentitiesOnly yes
    

    また、ターゲットのセキュリティグループでコントロールノードからのTCP/22を許可しておきます。
      

  3. 疎通確認を行う。

    コントロールノード
    $ ssh -o StrictHostKeyChecking=accept-new target "hostname; grep PRETTY /etc/os-release; getenforce; id"
    Warning: Permanently added 'xxx.xxx.xxx.xxx' (ED25519) to the list of known hosts.
    ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal
    PRETTY_NAME="Amazon Linux 2023.12.20260928"
    Permissive
    uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal) ...
    
  4. ClaudeCodeにMCPサーバとして登録する。

    コントロールノード
    $ claude mcp add rhel-mcp --scope user \
        --env LINUX_MCP_ALLOWED_LOG_PATHS=/var/log/messages,/var/log/secure \
        -- "$HOME/.local/bin/linux-mcp-server"
    $ claude mcp get rhel-mcp
    rhel-mcp:
      Scope: User config (available in all your projects)
      Status: ✔ Connected
      Type: stdio
      Command: /home/ec2-user/.local/bin/linux-mcp-server
    
    • 保存先は~/.claude.json。--scope userで全プロジェクトから利用できます。
    • 追加後はエージェントの再起動が必要です。
        
  5. 動作確認を行う。
    ツールget_system_informationを呼び出ししてみます。

    {
      "hostname": "ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal",
      "os_name": "Amazon Linux 2023.12.20260928",
      "os_version": "2023",
      "kernel": "6.18.51-120.162.amzn2023.x86_64",
      "arch": "x86_64",
      "uptime": "up 58 minutes",
      "boot_time": "2026-09-30 12:12:01"
    }
    

    ここまででセットアップは完了です。

Ansible Playbook失敗時のトラブルシューティング

Playbookの実行が失敗した際の一次調査をAIエージェントに行わせてみます。

検証用Playbook

ターゲットのal2023にhttpdを導入し、非標準ポート8888で待ち受けさせます。

deploy-web.yml
---
- name: Deploy internal web app
  hosts: al2023
  become: true
  tasks:
    - name: Install httpd
      ansible.builtin.dnf:
        name: httpd
        state: present

    - name: Configure listen port
      ansible.builtin.lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen '
        line: 'Listen 8888'

    - name: Start and enable httpd
      ansible.builtin.systemd_service:
        name: httpd
        state: started
        enabled: true

実行と失敗

コントロールノード
$ ansible-playbook -i inventory.yml deploy-web.yml


PLAY [Deploy internal web app] *************************************************

TASK [Gathering Facts] *********************************************************
[WARNING]: Platform linux on host al2023 is using the discovered Python
interpreter at /usr/bin/python3.9, but future installation of another Python
interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-
core/2.16/reference_appendices/interpreter_discovery.html for more information.
ok: [al2023]

TASK [Install httpd] ***********************************************************
changed: [al2023]

TASK [Configure listen port] ***************************************************
changed: [al2023]

TASK [Start and enable httpd] **************************************************
fatal: [al2023]: FAILED! => {"changed": false, "msg": "Unable to start service httpd: Job for httpd.service failed because the control process exited with error code.\nSee \"systemctl status httpd.service\" and \"journalctl -xeu httpd.service\" for details.\n"}

PLAY RECAP *********************************************************************
al2023                     : ok=3    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Ansibleログからわかるのは「httpd.serviceの起動に失敗した」というところまでです。

エージェントへの依頼

上記の実行結果をそのままエージェントに渡します。

ClaudeCodeチャット
以下のAnsible実行が失敗しました。linux-mcp-serverのツールで
失敗ホストを調査し、原因と推奨対処をレポートしてください。
システムへの変更は行わないでください。

(cat /tmp/ansible.logの内容)

調査の流れ

エージェントが呼び出したツールと、その結果は以下の通りでした。

  1. サービスの状態を確認する
    get_service_status(host="al2023", service_name="httpd")
    × httpd.service - The Apache HTTP Server
         Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
         Active: failed (Result: exit-code) since Tue 2026-09-22 15:16:19 UTC; 8min ago
        Process: 7401 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
       Main PID: 7401 (code=exited, status=1/FAILURE)
         Status: "Reading configuration..."
    
    起動プロセスが異常終了しています。ここまではAnsibleのメッセージ通りです。
      
  2. サービスのログを確認する
    get_service_logs(host="al2023", service_name="httpd", lines=20)
    Oct 22 15:16:19 amazonlinux2023 httpd[7401]: (13)Permission denied: AH00072: make_sock: could not bind to address [::]:8888
    Oct 22 15:16:19 amazonlinux2023 httpd[7401]: (13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:8888
    Oct 22 15:16:19 amazonlinux2023 httpd[7401]: no listening sockets available, shutting down
    Oct 22 15:16:19 amazonlinux2023 httpd[7401]: AH00015: Unable to open logs
    Oct 22 15:16:19 amazonlinux2023 systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
    
    Permission deniedで8888へのbindに失敗しています。ポートが既に使用されている場合は通常Address already in useとなるため、権限側の問題が疑われます。
      
  3. ポートの使用状況を確認する
    get_listening_ports(host="al2023")
    Proto    Local Address                  Status          PID/Program
    --------------------------------------------------------------------------------
    UDP      127.0.0.1:323                  LISTEN          
    UDP      [::1]:323                      LISTEN          
    TCP      0.0.0.0:22                     LISTEN          
    TCP      [::]:22                        LISTEN          
    
    8888番は他プロセスが使っていないため、SELinuxの状態を確認します。
      
  4. SELinuxステータスを確認する
    read_file(/sys/fs/selinux/enforce)
    {"result":"1"}
    

エージェントの出力

最終的に以下のレポートが出力されました。

ClaudeCodeチャット
結論: SELinux(Enforcing)が、httpd によるポート 8888 の使用を拒否している疑いがあります。Ansible の「Configure listen port」タスクで Listen 8888 に変更しましたが、ポート 8888 に http_port_t のラベルが付いておらず、SELinux 側のポート許可が不足している可能性があります。

調査は linux-mcp-server の読み取り専用ツールだけで行い、ホストには何も変更を加えていません。

## 確認した事実

| 確認項目 | 結果 |
|---|---|
| ホスト | Amazon Linux 2023(ip-xxx-xxx-xxx-xxx)。カーネル 6.18.51 |
| `httpd.service` | `failed (exit-code)`。15:16:19 に失敗。`enabled` は反映済み |
| httpd のエラー | `(13)Permission denied: AH00072: make_sock: could not bind to address [::]:8888` / `0.0.0.0:8888` → `no listening sockets available` |
| ポート競合 | なし。待ち受けているのは 22/tcp だけで、8888 はどのプロセスも使っていない |
| `/etc/httpd/conf/httpd.conf` | `Listen 8888`。この 1 行以外は標準のまま |
| SELinux | `/etc/selinux/config` は `SELINUX=enforcing`、`/sys/fs/selinux/enforce` は `1` |
root で起動しているのに EACCES になり、ポート競合もありません。SELinux が Enforcing であることと合わせると、SELinux による拒否が疑わしいと考えられます。ただし AVC の拒否記録は確認していないため、確定には audit ログ(`ausearch -m AVC -c httpd` など)の確認が必要です。同じエラーは 15:10 の実行時にも出ており、再実行しても再現しています。

推奨対処:
  上記を確認のうえ、SELinuxが原因であれば以下のタスクをhttpd起動前に追加してください。
  実行はしていません。

    - name: Allow httpd to bind to port 8888
      community.general.seport:
        ports: 8888
        proto: tcp
        setype: http_port_t
        state: present

実際に推奨対処の内容を手動実施し、playbookが実行できるようになることを確認しました。

まとめ

linux-mcp-serverをAnsibleのコントロールノードに導入し、Playbook失敗時の一次調査を行わせるまでを紹介しました。
実行されるコマンド自体は基本的なもので、linux-msp-serverを利用しない場合も同じような調査はできるかと思います。
本MCPサーバを挟む利点としては、ユーザがコマンド実行を逐一内容確認し許可する必要なく、読み取り専用が保証される点にあると感じました。

参考

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?