Ansible在使用凭据文件和Azure\u rm\u资源模块时访问Azure凭据
当我使用ansible在Azure上创建一些基础设施时,我试图在ansible运行期间访问Ansible在使用凭据文件和Azure\u rm\u资源模块时访问Azure凭据,azure,ansible,Azure,Ansible,当我使用ansible在Azure上创建一些基础设施时,我试图在ansible运行期间访问subscription\u id。我按照以下步骤设置凭据文件: ansible文档中的Azure指南 当我运行ansible playbook时,我添加了以下参数-e ansible_PROFILE=“new env”,该参数完成了它需要执行的操作,并为运行提供了正确的凭证以正确执行模块 但是我正试图通过Azure\u rm\u资源模块使用Azure REST API,我希望在请求主体中发送一个JSON表
subscription\u id
。我按照以下步骤设置凭据文件:
ansible文档中的Azure指南
当我运行ansible playbook时,我添加了以下参数-e ansible_PROFILE=“new env”
,该参数完成了它需要执行的操作,并为运行提供了正确的凭证以正确执行模块
但是我正试图通过Azure\u rm\u资源
模块使用Azure REST API,我希望在请求主体中发送一个JSON表示,其中包括订阅id
,以格式化Azure中资源的id。
假设Azure中的VNet ID类似于“ID”:“/subscriptions//resourceGroups/myresourcegroup/providers/Microsoft.Network/virtualNetworks/myVNET”,
我能够格式化vnet的名称,但我需要获取订阅id的信息,该信息由ansible在playbook运行期间从~/.azure/credentials
文件中读取
如何访问此信息?有特殊变量吗?我查看了lookup env
功能,但它没有使用变量AZURE\u SUBSCRIPTION\u ID
它既不在事实中,也不在事实中,我尝试使用以下命令公开事实AZURE\u PROFILE=“new env”ansible localhost-m setup
凭证文件示例
cat ~/.azure/credentials
[new-env]
subscription_id=****************************
client_id=****************************
secret=****************************
tenant=****************************
谢谢
更新#1
我最终使用了ansible ini查找功能
"{{ lookup('ini', 'subscription_id section={{ azure_profile }} file={{ ansible_env.HOME }}/.azure/credentials') }}"
这将返回我正在查找的确切值,我使用
set\u fact
在ansible运行过程中对其进行设置。使用ini查找,我将直接在凭据文件中搜索,然后将其设置为事实,以便稍后重新使用
tasks:
- name: set fact to reduce the verbosity of the variables
set_fact:
azure_profile: "{{ AZURE_PROFILE }}"
azure_subscription_id: "{{ lookup('ini', 'subscription_id section={{ azure_profile }} file={{ ansible_env.HOME }}/.azure/credentials') }}"