てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible/Azure] azure.azcollection コレクションの利用時の Python パッケージ不足によるエラー集

はじめに

各種 Azure リソースを操作する azure.azcollection コレクションのモジュールやプラグインを利用する場合は、依存 Python パッケージをインストールする必要があります。

上手くインストールできてなかったり、認識できていなかったりするとエラーが発生します。

環境や実行するモジュールによってエラーは様々だと思いますが、私が見かけたエラーを3つ掲載します。実際はもっと色々あると思います。

  • 環境
    • azure.azcollection 1.15.0
    • ansible-core 2.14.4
    • macOS 13.2.1

そもそも 依存 Python パッケージをインストールするには?

エラーの前に、パッケージのインストール方法にについてまとめます。

正しいインストール方法

README.md にも記載がありますが、コレクション内の requirements-azure.txtpip install -r で読み込んでインストールします。~/.ansible/collections ディレクトリがコレクションのインストール先のパスの場合、以下のような操作です。

pip install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements-azure.txt

コレクションのバージョンが違えば requirements-azure.txt の中身も違うこともあるため、インストールしたコレクションのバージョンに合わせてインストールする必要があります。

誤ったインストール方法

今では Microsoft Azure Guideにある pip install 'ansible[azure]' ではうまく依存パッケージをインストールできないのでご注意ください。(このページの親ページである Legacy Public Cloud Guides には may be out of date とあります)

WARNING の例:

% pip install 'ansible[azure]'
Collecting ansible[azure]
  Using cached ansible-7.4.0-py3-none-any.whl (43.3 MB)
WARNING: ansible 7.4.0 does not provide the extra 'azure'
...(略)...

エラー集

ここから本編のエラー集です。

1. ModuleNotFoundError: No module named 'packaging'

何も考えず、venv を作って以下のPlaybookを実行したときです。

---
- name: Azure
  hosts: localhost
  gather_facts: false
  connection: local

  vars:
    resource_group: rg-yokochi-dev

  tasks:
    - name:  Create a Resource Group 
      azure.azcollection.azure_rm_resourcegroup:
        name: "{{ resource_group }}"
TASK [Create a Resource Group] *********************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'packaging'
[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter at /usr/local/bin/python3.11,
but future installation of another Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-core/2.14/reference_appendices/interpreter_discovery.html for more information.
fatal: [localhost]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/local/bin/python3.11"}, "changed": false, "msg": "Failed to import the required Python library (packaging) on mbp16.local's Python /usr/local/opt/python@3.11/bin/python3.11. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

ansible_python_interpreter 変数もとくに指定してないため、Interpreter Discovery 機能によって /usr/local/bin/python3.11 が検出されて、この Python 環境を使った様子です。

ここで No module named 'packaging' を見て「あれ? packaging がない? これインストールしなきゃ」と早とちりしてしまわないよう・・・。

2. No module named 'msrest'

ansible_python_interpreter: "{{ ansible_playbook_python }}" という指定して、先程とおなじく azure.azcollection.azure_rm_resourcegroup モジュールを実行した時のエラーです。

ansible_playbook_python 変数は、ansible-playbook コマンド自体を実行してる Python 環境を指します。今回は、venv を作った後に pip install ansible しただけの環境です。

TASK [Create a Resource Group] *********************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'msrest'
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (msrestazure) on mbp16.local's Python /Users/sakana/venv/azure/bin/python3.10. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

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

msrestmsrestazure という文字面から、Azure 系の何かが足りないのかなと気付ける雰囲気です。

3. plugin: name 'azure_cloud' is not defined

依存パッケージをインストールしていない環境で、azure.azcollection.azure_rm インベントリプラグインを実行したときのエラーです。

 % ansible-inventory -i inventory_azure_rm.yml --graph
[WARNING]:  * Failed to parse /Users/sakana/ansible/azure/inventory_azure_rm.yml with auto
plugin: name 'azure_cloud' is not defined
[WARNING]:  * Failed to parse /Users/sakana/ansible/azure/inventory_azure_rm.yml with yaml
plugin: Plugin configuration YAML file, not YAML inventory
[WARNING]:  * Failed to parse /Users/sakana/ansible/azure/inventory_azure_rm.yml with ini plugin:
Invalid host pattern '---' supplied, '---' is normally a sign this is a YAML file.
[WARNING]: Unable to parse /Users/sakana/ansible/azure/inventory_azure_rm.yml as an inventory
source
[WARNING]: No inventory was parsed, only implicit localhost is available
@all:
  |--@ungrouped:

関連 issue(実際はバグではなく環境の問題です)

github.com

おわりに

なにか解せない環境エラーが発生したた場合は、依存パッケージのインストール状態や Python インタープリターの指定を確認するとよさそうです。

個人的には、コレクションをバージョアップしたのに、依存パッケージがそのまま、というケースがあります。