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?

VSCodeのAnsible拡張をマルチルートワークスペースで使うとコケる問題に対応する

0
Last updated at Posted at 2026-03-11

マルチルートワークスペースでAnsible拡張を使いたい

マルチルートワークスペースの場合、.code-workspaceansible.python.interpreterPath が書かれていない場合、絶対パスで書き加えようとする謎仕様があります。これは2023年から続いているissueです。

例えば ansible/ 以下にAnsibleのファイルをまとめているとします。こんな感じです。

<root>
├── ansible/
│   ├── .vscode/
│   │   └── settings.json
│   └── .venv
└── foobar.code-workspace

ansible/.vscode/settings.json に相対パスで .venv を指定することになります。そうするとAnsible拡張が 勝手にfoobar.code-workspaceに絶対バスを書き加えます

複数のデバイスから実行する場合、ワークスペース設定を .gitignore してしまうと、今度は file.associations の設定を共有できなくなります。これはこれで roles_path の問題に行き当たります。

この絶対パス書き換えがかなり厄介で、以下のような設定でも太刀打ちできません。

空文字で置いておくと、ご丁寧に中身を絶対パスに上書きしてくれます。

// linterが走った瞬間に絶対パスで書き換えられる
"ansible.python.interpreterPath": ""

拡張機能の問題で ${workspaceFolder:ansible} のような特定のワークスペースを指定する記述も展開できません。

// ${workspaceFolder:ansible}/.venv/bin/pythonを文字列として渡してしまう。
"ansible.python.interpreterPath": "${workspaceFolder:ansible}/.venv/bin/python"

そんな状態なのでCommand variablesも展開されません。

// これはOK
"python.defaultInterpreterPath": "${workspaceFolder:ansible}/.venv/bin/python",

// Outputにエラーは出ない
// ただDevelopment Toolsをよく読むとエラーになっている
"ansible.python.interpreterPath": "${command:python.defaultInterpreterPath}"

解決策1: とりあえず対応する

対処療法的にこのような設定ができます。

foobar.code-workspace
{
  "folders": [
    {
      "path": "."
    },
    {
      "path": "terraform"
    },
    {
      "path": "ansible"
    }
  ],
  "settings": {
    "files.associations": {
      "**/roles_common/**/*.{yml,yaml}": "ansible",
      "**/roles_specific/**/*.{yml,yaml}": "ansible"
    },
    "ansible.python.interpreterPath": "${workspaceFolder}/.venv/bin/python"
  }
}

この設定で勝手な上書きを黙らせられます。これはVSCodeのDeveloper Toolsからチェックできます。

image.png

このままだとAnsible拡張が生成する .ansible ディレクトリがプロジェクトルートに生成されてしまうので、引数で --project-dir を渡します。これは ansibleディレクトリ内の /.vscode/settings.json に書きます。

ansible/.vscode/settings.json
{
  "ansible.validation.lint.arguments": "--project-dir .",
}

環境によってはAnsible拡張が /usr/sbin/ansible-linter を見に行ってしまうかもしれません。その場合 ansible ディレクトリ内で activate を指定します。

ansible/.vscode/settings.json
{
  "ansible.validation.lint.arguments": "--project-dir .",
  "ansible.python.activationScript": ".venv/bin/activate"
}

解決策2: 最新版を待つ or ビルドする

issueを読んでいくとわかるのですが、ちょうど3週間前にこのPRがマージされています。

昨年11月にリリースされたVSCode Python拡張の新機能である python.useEnvironmentsExtension を利用することで、VSCodeのPython拡張のAPIからPythonをとってきているようです。

このPRは先週 v26.2.0 でマージされているのですが、Ansible拡張の最新版は 26.1.3 です。まだMarketplaceに落ちてきていません。たぶんそんなに時間はかからないと思います。

せっかちな方は自分でビルドしましょう

まとめ

現時点(2026/3/11)では、解決策1で対応するのが一旦良いと思います。

次のバージョンでは解決していそうなので、気長に待ちましょう。またバージョン更新が来たら記事を修正します。

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?