首页 > 解决方案 > Azure 资源管理器清单插件可以根据资源组名称的子字符串动态查找 VM(在 include_vm_resource_groups 中)

问题描述

Azure 资源管理器清单插件如何根据资源组名称的子字符串动态查找 VM(在 include_vm_resource_groups 中)

我可以使用 include_vm_resource_groups 中的完整资源组名称获取 VM 列表,但在使用资源组名称的部分\子字符串时不能。

请举个例子谢谢

标签: azureansible-inventory

解决方案


不,不能使用子字符串。

作为一种解决方法,我在剧本文件中的主机变量上放置了一个过滤器

# This is an explanation of the following command:
  # 1. Find the hostname of Ansible host using lookup("pipe","hostname").
  # 2. Pipe the result and replace "-management-ansible" with "_cluster". E.g of output: ddd-dev001-01_cluster 
  # 3. Pipe the result and replace "-" with "_". E.g of output:  ddd_dev001_01_cluster
  # 4. Append the result to hosts group "tag_workspace_". E.g of output:  tag_workspace_ddd_dev001_01_cluster
  # Note hyphens are not accepted in host group names.
  hosts: tag_workspace_{{ lookup("pipe","hostname") | regex_replace('-management-ansible', '_cluster') |  regex_replace('-', '_') }}

推荐阅读