Home > Mobile >  Failed to get credentials when running ansible playbook from ansible tower to stop/start azure vm
Failed to get credentials when running ansible playbook from ansible tower to stop/start azure vm

Time:04-27

I am running a ansible playbook from ansible tower to stop/start vm. Below is the code.

---
- hosts: localhost
  gather_facts: yes
  vars:
    state : "{{ state }}"    
    env:
      ARM_SUBSCRIPTION_ID : "{{ subscription_id }}"
      ARM_TENANT_ID : "{{ tenant_id }}"
      ARM_CLIENT_ID : "{{ client_id }}"
      ARM_CLIENT_SECRET : "{{ secret_value }}" 
     
  collections:
    - ansible.tower  
  tasks:           
    - name: Power Off
      azure_rm_virtualmachine:
        resource_group: "{{ resource_group_name }}"
        name: "{{ virtual_machine_name }}"
        started: no
      when: state == "stop"
    - name: Deallocate
      azure_rm_virtualmachine:
        resource_group: "{{ resource_group_name }}"
        name: "{{ virtual_machine_name }}"
        allocated: no
      when: state == "delete"
    - name: Power On
      azure_rm_virtualmachine:
        resource_group: "{{ resource_group_name }}"
        name: "{{ virtual_machine_name }}"
      when: state == "start"   
  environment: "{{ env }}"  

This is giving below error:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to get credentials. Either pass as parameters, set environment variables, define a profile in ~/.azure/credentials, or log in with Azure CLI (az login)."}

Syntax wise everything looks good. Please help.

CodePudding user response:

You can pass the credentials by passing them as environment variables like below.

- name: Restart
  azure_rm_virtualmachine:
    resource_group: "{{ resource_group_name }}"
    name: "{{ virtual_machine_name }}"
    restarted: yes
    subscription_id : "{{ subscription_id }}"
    tenant : "{{ tenant_id }}"
    client_id : "{{ client_id }}"
    secret : "{{ secret_value }}"        
  when: state == "restart"
  • Related