在Ubuntu使用 Ansible 配置 Azure 资源的动态清单

使用 Ansible 配置 Azure 资源的动态清单

简介

在主机变化不定的云环境中,Ansible 的动态清单功能可以消除维护静态清单文件的负担

本教程将带你使用 Azure 的 dynamic-inventory 插件,自动填充 Ansible 清单。


1.安装pipx

从 Ubuntu 23.04 开始,系统默认禁止在全局环境中使用 pip install,以避免与系统自带的 APT 包管理器(如 python3-* 包)产生冲突

所以因为我的是ubuntu版本为24 LTS, 必须要用pipx来安装ansible,而不能直接使用apt安装,这样后面才能安装依赖不报错。

参考文档:pipx--installation

shell 复制代码
sudo apt update
sudo apt install pipx
pipx ensurepath

2.通过 pipx 安装 Ansible

shell 复制代码
pipx install --include-deps "ansible<10"

参数:--include-deps

在使用 pipx 安装 Ansible 时,如果未指定 --include-deps 参数,pipx 只会安装主包,而不会安装其依赖项,包括ansible-core

他的输出会是这样

bash 复制代码
  installed package ansible 9.13.0, installed using Python 3.12.3
  These apps are now globally available
    - ansible
    - ansible-community
    - ansible-config
    - ansible-connection
    - ansible-console
    - ansible-doc
    - ansible-galaxy
    - ansible-inventory
    - ansible-playbook
    - ansible-pull
    - ansible-test
    - ansible-vault
done! ✨ 🌟 ✨

这里有个大坑,从 Ansible 10 开始,Ansible 官方做了一个大重构:不再提供 ansible 命令入口点,转而用 ansible-community 这个命令来代表 Ansible CLI 主入口。ansible 和 ansible-playbook 等命令被移除,取而代之的是通过 ansible-community 调用相应的插件。所以以前直接ansible是会报错的,这里降级安装 小于10的版本

装完之后需要重新打开一个bash终端,然后才能ansible --version

bash 复制代码
ansible [core 2.16.14]
  config file = None
  configured module search path = ['/home/azureuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/azureuser/.local/share/pipx/venvs/ansible/lib/python3.12/site-packages/ansible
  ansible collection location = /home/azureuser/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/azureuser/.local/bin/ansible
  python version = 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] (/home/azureuser/.local/share/pipx/venvs/ansible/bin/python)
  jinja version = 3.1.6
  libyaml = True

如果显示出来版本,证明你成功了,可以进入下一步

3.安装azure.azcollection

此集合提供了一系列用于与 Azure 交互的 Ansible 模块和插件

参考文档:Ansible collection for Azure

bash 复制代码
ansible-galaxy collection install azure.azcollection --force

输出这样就是成功了

bash 复制代码
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/azure-azcollection-3.3.1.tar.gz to /home/azureuser/.ansible/tmp/ansible-local-2726e_5yr3jc/tmp8eixr_93/azure-azcollection-3.3.1-52eb6zq_
Installing 'azure.azcollection:3.3.1' to '/home/azureuser/.ansible/collections/ansible_collections/azure/azcollection'
azure.azcollection:3.3.1 was installed successfully

4.安装集合所需的依赖项

复制代码
pipx runpip ansible install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt

5.生成动态库存

Ansible 提供了 Azure 动态库存插件,这里使用环境变量作为凭证,其他配置方法请阅读参考文档

参考文档:azure.azcollection.azure_rm inventory -- Azure 资源管理器库存插件

创建 myazure_rm.yml 文件,配置示例如下:

yaml 复制代码
plugin: azure.azcollection.azure_rm
include_vm_resource_groups:
  - ansible-inventory-test-rg
auth_source: auto

在Azure的Entra ID注册一个应用,使其成为一个服务主体,分配其权限,然后复制subscription_id、client_id、secret 和 tenant id,然后使其export为环境变量

参考文档:快速入门:为 Ansible 创建 Azure 服务主体

bash 复制代码
export AZURE_SUBSCRIPTION_ID="your_subscription_id"
export AZURE_CLIENT_ID="your_client_id"
export AZURE_CLIENT_SECRET="your_client_secret"
export AZURE_TENANT_ID="your_tenant_id"

运行命令查看资源组内的虚拟机:

bash 复制代码
ansible-inventory -i myazure_rm.yml --graph

运行结果示例:

复制代码
@all:
  |--@ungrouped:
  |  |--linux-vm_cdb4
  |  |--win-vm_3211

这里两个虚拟机都属于 ungrouped 组,ungroupedall 组的子组。


Azure 动态库存插件默认返回全局唯一的虚拟机名称,名称中会带有额外字符。如果想禁用此行为,可以在清单文件里添加:

yaml 复制代码
plain_host_names: yes
相关推荐
Maki Winster6 小时前
Peek-Ubuntu上Gif录制工具-24.04LTS可装
linux·ubuntu·peek
Maki Winster7 小时前
在 Ubuntu 下配置 oh-my-posh —— 普通用户 + root 各自使用独立主题(共享可执行)
linux·运维·ubuntu
泊浮目7 小时前
未来数据库硬件-网络篇
数据库·架构·云计算
Imagine Miracle9 小时前
Ubuntu for ARM 更换为阿里云镜像源
arm开发·ubuntu·阿里云
超龄超能程序猿13 小时前
dnSpy 使用教程
windows·microsoft
Leinwin16 小时前
微软开源GitHub Copilot Chat,AI编程领域迎新突破
microsoft·github·copilot
kikikidult1 天前
(2025.07)解决——ubuntu20.04系统开机黑屏,左上角光标闪烁
笔记·ubuntu
旷世奇才李先生1 天前
Pillow 安装使用教程
深度学习·microsoft·pillow
UI设计和前端开发从业者1 天前
UI前端大数据处理策略优化:基于云计算的数据存储与计算
前端·ui·云计算
BD_Marathon1 天前
Ubuntu:Mysql服务器
服务器·mysql·ubuntu