zaki work log

作業ログやら生活ログやらなんやら

miseを使用した外部と分離されたプロジェクト固有のAnsible環境を整える

おそらくpipxuvを完全に理解したらもっと良い構成にできるかもしれないけど、今はこれが精一杯。

miseで直接ansibleやansible-coreをインストールできるけど、依存してるPythonパッケージが必要な場合にどうすればいいかわからなかったため、従来通りpipで入れる構成。
また、pipxmiseで入れてpipx install ansible-coreすると、(従来のpipx同様に)ユーザーホームへのインストールになり、プロジェクト(ディレクトリ)を抜けてもansibleを実行可能なため(指定ディレクトリでのみ使用可能にしたい、という)今回の趣旨的に却下。
ちなみにmiseでansible-coreを入れるにはpipxも必要(後述)になるが、pipx管理でなくmise管理となる関係かpipx inject ansible-core proxmoxer requestsみたいに依存パッケージの追加に対応できてないっぽい。

本記事の内容は基本的に以下からAnsible用の設定を抜き出したもの。

zaki-hmkc.hatenablog.com

また、作るmise.tomlは全体でこんな感じ。
(terraformとか追加コマンドが必要なら書き足す)

[tools]
python = "3.13.6"
terraform = "1.12.2"

[env]
_.python.venv = { path = ".venv", create = true, python_create_args = ["--upgrade-deps"] }
PROJECT_NAME = "{{ config_root | basename }}"
ANSIBLE_COLLECTIONS_PATH = "{{ config_root }}/.ansible"

[tasks.install]
description = "install prerequisites"
alias = "i"
run = """
pip install -r requirements.txt
ansible-galaxy collection install -r requirements.yml
"""

miseのインストールについては公式参照

自分用環境

Pythonの指定

例としてPython 3.13.6を使う場合、作業ディレクトリ/プロジェクトディレクトリで以下実行。

$ mise use python@3.13.6
mise ~/local/src/pve/mise.toml tools: python@3.13.6

これでカレントにmise.tomlが生成され、このディレクトリ配下でPython 3.13.6が有効になる。

$ cat mise.toml 
[tools]
python = "3.13.6"

あとはこのファイルに必要な内容を追記していく。

venvの用意

以前のエントリでも書いた通り、下記定義で(mise.tomlのあるディレクトリに移動した時点で)自動でvenvの作成・有効化が行われる。

[env]
_.python.venv = { path = ".venv", create = true, python_create_args = ["--upgrade-deps"] }

Ansibleコレクションパス (環境変数設定)

これも以前のエントリでちょっと触れたが、mise.tomlのディレクトリに移動した際に環境変数が有効になるため、対象プロジェクトでのみ有効なコレクションのパスとなる。
venvのみ単体で使ってる場合に軽率にansible-galaxy collection installしてユーザーホーム以下にAnsibleコレクションがインストールされ、他のプロジェクトと環境が混ざってしまう、という恐れがなくなるので、miseと組み合わせた環境分離と仕組みしては秀逸。(個人の感想です)

[env]
ANSIBLE_COLLECTIONS_PATH = "{{ config_root }}/.ansible"

mise.tomlに入れたくない環境変数

APIキーやシークレットなどの機密性の高い情報などをmise.tomlに記述したくない場合は、以下のようにmise.fileを使うと外部の環境変数定義ファイルを読み込める。

[env]
...
mise.file = ".env"

Ansible / 依存パッケージ / Ansibleコレクションのインストール

従来通り、Ansibleや依存パッケージはrequirements.txtに、Ansibleコレクションはrequirements.ymlに必要内容を記載する。
インストールは手動でもできるが、miseのタスクランナーを使い、mise.tomlに以下のように定義。

[tasks.install]
description = "install prerequisites"
alias = "i"
run = """
pip install -r requirements.txt
ansible-galaxy collection install -r requirements.yml
"""

これでmise run installあるいはmise run iを実行すれば、ここで記述している以下コマンドが実行されるため、必要なパッケージ類が全てインストールされる。

pip install -r requirements.txt
ansible-galaxy collection install -r requirements.yml

requirementsの例はProxmox操作用ならこんな感じ

ansible-core==2.18.8
proxmoxer==2.2.0
requests==2.32.3
---
collections:
- name: community.general
  version: 10.7.3

共有されたmise.tomlを使う場合

mise.tomlのtrust

配布されたmise.tomlファイルのあるディレクトリに初めて移動すると、以下の警告がでる。
これは出所不明のmise.tomlを無条件に解釈しないための安全対策(意訳)のため。

$ cd tmp/example/
mise WARN  Config files in ~/local/src/tmp/example/mise.toml are not trusted.
Trust them with `mise trust`. See https://mise.jdx.dev/cli/trust.html for more information.

有効化するには、このディレクトリで以下コマンドを実行する。

$ mise trust
mise trusted /home/zaki/local/src/tmp/example
mise creating venv with stdlib at: ~/local/src/tmp/example/.venv

これでmise.tomlが解釈されるようになり、同時にファイル内に定義されているvenvも作成・有効化される。

あとはタスクランナーを使い、必要パッケージをインストールすればOK

$ mise run i
[install] $ pip install -r requirements.txt
Collecting ansible-core==2.18.8 (from -r requirements.txt (line 1))
  Using cached ansible_core-2.18.8-py3-none-any.whl.metadata (7.7 kB)
Collecting proxmoxer==2.2.0 (from -r requirements.txt (line 2))
  Using cached proxmoxer-2.2.0-py3-none-any.whl.metadata (5.2 kB)
Collecting requests==2.32.3 (from -r requirements.txt (line 3))
  Using cached requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
:
:
$ ansible --version
ansible [core 2.18.8]
  config file = None
  configured module search path = ['/home/zaki/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/zaki/local/src/tmp/example/.venv/lib/python3.13/site-packages/ansible
  ansible collection location = /home/zaki/local/src/tmp/example/.ansible
  executable location = /home/zaki/local/src/tmp/example/.venv/bin/ansible
  python version = 3.13.6 (main, Aug  6 2025, 22:57:45) [Clang 20.1.4 ] (/home/zaki/local/src/tmp/example/.venv/bin/python)
  jinja version = 3.1.6
  libyaml = True

$ ansible-galaxy collection list

# /home/zaki/local/src/tmp/example/.ansible/ansible_collections
Collection        Version
----------------- -------
community.general 10.7.3 

Appendix

miseでAnsibleをインストールする場合

pipxを入れていないとインストールに失敗する。

$ mise use ansible-core@2.19.0
mise ERROR Failed to install tool: ansible-core@2.19.0

Individual error details:
  1. ansible-core@2.19.0:
     Error {
         msg: "failed to install pipx:ansible-core@2.19.0",
         source: Error {
             msg: "failed to execute command: pipx install ansible-core==2.19.0",
             source: Os {
                 code: 2,
                 kind: NotFound,
                 message: "No such file or directory",
             },
         },
     }
mise ERROR Run with --verbose or MISE_VERBOSE=1 for more information

mise use pipxで使用可能にしておけば、上記エラーは解消されインストールできる。

環境

$ mise version
              _                                        __              
   ____ ___  (_)_______        ___  ____        ____  / /___ _________
  / __ `__ \/ / ___/ _ \______/ _ \/ __ \______/ __ \/ / __ `/ ___/ _ \
 / / / / / / (__  )  __/_____/  __/ / / /_____/ /_/ / / /_/ / /__/  __/
/_/ /_/ /_/_/____/\___/      \___/_/ /_/     / .___/_/\__,_/\___/\___/
                                            /_/                 by @jdx
2025.8.7 linux-x64 (2025-08-06)
mise WARN  mise version 2025.8.9 available

あ、更新あるな。