Search code examples
azureansibleansible-inventory

How to pass Azure service principal details to Ansible via command-line?


I am able to connect to Azure using Ansible by putting my service principle details into the credentials file stored in ~/.azure/credentials

That was OK for development, now (in production) I want to move away from using the text credentials file and pass the credentials to Ansible via the command-line via parameters.

How should this be done? Any help is appreciated - thanks

I have tried:

ansible-playbook -i ./dev-env/epazure_rm.yml ./dev-env/site.yml -vvvv -u adminuser --extra-vars "AZURE_SUBSCRIPTION_ID=XXX AZURE_CLIENT_ID=XXX AZURE_SECRET=XXX AZURE_TENANT=XXX"

My Azure Dynamic Inventory plugin file looks like this

    --- 
plugin: azure_rm
include_vm_resource_groups: 
  - rg-devdonal-eastus01
auth_source: auto
subscription_id: "{{ AZURE_SUBSCRIPTION_ID }}"
client_id: "{{ AZURE_CLIENT_ID }}"
secret: "{{ AZURE_SECRET }}"
tenant: "{{ AZURE_TENANT }}"

keyed_groups:
- prefix: tag
  key: tags

Solution

  • You can use the environment variables for the credential and then read the variables from the environment, here is the example:

    - debug: msg="{{ lookup('env','HOME') }} is an environment variable"
    

    And there is also another issue shows the example.