跳转至内容

Ansible 开发环境

一个类似于 pip 的 Ansible collection 安装工具。

沟通交流

  • 加入 Ansible 论坛
  • 获取帮助:寻求帮助或帮助他人。如果您发起新的讨论,请添加适当的标签。
  • 社交空间:与同行爱好者聚集并互动。
  • 新闻与公告:跟踪包括社交活动在内的整个项目的公告。
  • Bullhorn 通讯:用于发布版本发布和重要变更通知。

有关沟通交流的更多信息,请参阅 Ansible 沟通指南

特性

  • 倡导一种“临时性(ephemeral)”的开发方法
  • 确保当前的开发环境是隔离的
  • 安装 collection 的所有 Python 依赖
  • 安装 collection 的所有测试依赖
  • 检查缺失的系统软件包
  • 将当前的 collection 软链接到当前 Python 解释器的 site-packages 中
  • 将所有 collection 依赖安装到当前 Python 解释器的 site-packages 中
  • 在可用时使用 uv env 代替 Python 的 venv 以提升性能。可通过 SKIP_UV=1 禁用

通过将 collection 放置在 Python site-packages 目录中,使其能够被 ansible 以及 python 和 pytest 识别。

使用方法

设置开发环境

建议

安装 ansible-dev-environment推荐方法是使用 ansible-dev-tools 软件包。Ansible 开发工具 (Ansible Development Tools) 旨在简化创建 Ansible 内容所需的多种工具的设置和使用。它将关键的 Ansible 开发包整合到了一个统一的 Python 包中。

# This also installs ansible-core if it is not already installed
pip3 install ansible-dev-tools
pip3 install ansible-dev-environment --user
$ git clone <collection_repo>
$ cd <collection_repo>
$ ade install -e .\[test] --venv .venv
INFO: Found collection name: network.interfaces from /home/bthornto/github/network.interfaces/galaxy.yml.
INFO: Creating virtual environment: /home/bthornto/github/network.interfaces/venv
INFO: Virtual environment: /home/bthornto/github/network.interfaces/venv
INFO: Using specified interpreter: /home/bthornto/github/network.interfaces/venv/bin/python
INFO: Requirements file /home/bthornto/github/network.interfaces/requirements.txt is empty, skipping
INFO: Installing python requirements from /home/bthornto/github/network.interfaces/test-requirements.txt
INFO: Installing ansible-core.
INFO: Initializing build directory: /home/bthornto/github/network.interfaces/build
INFO: Copying collection to build directory using git ls-files.
INFO: Running ansible-galaxy to build collection.
INFO: Running ansible-galaxy to install collection and it's dependencies.
INFO: Removing installed /home/bthornto/github/network.interfaces/venv/lib64/python3.11/site-packages/ansible_collections/network/interfaces
INFO: Symlinking /home/bthornto/github/network.interfaces/venv/lib64/python3.11/site-packages/ansible_collections/network/interfaces to /home/bthornto/github/network.interfaces
WARNING: A virtual environment was specified but has not been activated.
WARNING: Please activate the virtual environment:
source venv/bin/activate

拆除开发环境

$ ade uninstall ansible.scm
INFO     Found collection name: ansible.scm from /home/bthornto/github/ansible.scm/galaxy.yml.
INFO     Requirements file /home/bthornto/github/ansible.scm/requirements.txt is empty, skipping
INFO     Uninstalling python requirements from /home/bthornto/github/ansible.scm/test-requirements.txt
INFO     Removed ansible.utils: /home/bthornto/github/ansible.scm/venv/lib64/python3.11/site-packages/ansible_collections/ansible/utils
INFO     Removed ansible.utils*.info: /home/bthornto/github/ansible.scm/venv/lib64/python3.11/site-packages/ansible_collections/ansible.utils-2.10.3.info
INFO     Removed ansible.scm: /home/bthornto/github/ansible.scm/venv/lib64/python3.11/site-packages/ansible_collections/ansible/scm
INFO     Removed collection namespace root: /home/bthornto/github/ansible.scm/venv/lib64/python3.11/site-packages/ansible_collections/ansible
INFO     Removed collection root: /home/bthornto/github/ansible.scm/venv/lib64/python3.11/site-packages/ansible_collections

帮助

ade --help

ade install --help

ade uninstall --help