【问题标题】:Ansible access Azure credentials while using the credentials file and the azure_rm_resource moduleAnsible 在使用凭据文件和 azure_rm_resource 模块时访问 Azure 凭据
【发布时间】:2019-02-04 19:05:08
【问题描述】:

当我使用 ansible 在 Azure 上创建一些基础架构时,我试图在 ansible 运行期间访问 subscritpion_id。我按照以下设置凭据文件 ansible 文档https://docs.ansible.com/ansible/2.6/scenario_guides/guide_azure.html中的 Azure 指南

当我运行 ansible playbook 时,我添加了以下参数 -e ANSIBLE_PROFILE="new-env",它完成了它需要做的事情并为运行提供正确的凭据以正确执行模块。

但我正在尝试通过 azure_rm_resource 模块使用 Azure REST API,并且我想在请求正文中发送 JSON 表示形式,其中包括 subscription_id 以格式化 Azure 中资源的 id。 https://docs.ansible.com/ansible/2.6/modules/azure_rm_resource_module.html

假设 Azure 中的 VNet ID 看起来像 "id": "/subscriptions/<subscription_id>/resourceGroups/myresourcegroup/providers/Microsoft.Network/virtualNetworks/my-vnet", 我可以格式化 vnet 的名称,但我需要在 playbook 运行期间从 ~/.azure/credentials 文件中获取 ansible 读取的订阅 ID 的信息。

我如何访问这些信息?有特殊变量吗?我查看了lookup env 功能,变量AZURE_SUBSCRIPTION_ID 不存在。

事实并非如此,我尝试使用以下命令AZURE_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_fact 在我的 ansible 运行中设置它。

【问题讨论】:

    标签: azure ansible


    【解决方案1】:

    使用 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') }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2022-07-14
      • 1970-01-01
      相关资源
      最近更新 更多