Ansible Modules for Nutanix


Has anyone used Ansible for Automating Nutanix tasks. Can someone share some examples please.

19 replies

Userlevel 1
Badge +2
Hello,

I'm using Ansible to configure prism settings. DNS, NTP, Alerts, proxy, authentication, and stuff like that. I will try to clean up all my specific data and publish them on the nutanix git, or my own git portal.
Badge
Hey @itguyadam - I am trying to list VMs on specific cluster using ansible - Currently, I am listing all the VMs and then filtering based on cluster_name.

Is there better way where I could pass cluster_name as filter and get VMs only on that particular cluster ?

TIA
Userlevel 1
Badge +2
@hfg_nutanix I've uploaded an ansible role to git. There are a few tasks i'm still working on and getting ready to publish but the bones of everything is there.

https://github.com/AD-Code/Automation/tree/master/ansible/nutanix_cluster_baseline
Userlevel 1
Badge +2
@OkBeacon What commands are you using currently?
Badge
@itguyadam - Thanks for reply!
I eventually figured out how to do it (Couldn't find any documentation about filters)

code:
- import_tasks: get_session_cookie.yml

- name: Get VMS list
uri:
url: "{{ api_url_v3 }}/vms/list"
body:
kind: vm
sort_order: ASCENDING
offset: 0
length: 1000
sort_attribute: ''
filter: "cluster_name=={{ cluster_name }}"
method: POST
validate_certs: no
body_format: json
status_code: 200
headers:
Cookie: "{{ session_cookie }}"

register: vms_result
Userlevel 1
Badge +2
@OkBeacon I've also found some of the API documentation a bit lacking in areas like that. I will most likely steal your task and use it in some other playbooks i'm working on. thanks
Badge
I am reusing code from
https://github.com/mbach04/nutanix_vm_provisioner

Currently working on provisioning k8s cluster on nutanix - I will put it on github once I am happy with it.
Userlevel 1
Badge +2

I’ve just created an ansible role (https://galaxy.ansible.com/cybergavin/nutanix_vm_create) for bulk creation of VMs using different images and in different subnets. It also mounts and enables NGT.

Badge

@itguyadam

- Thanks for reply!
I eventually figured out how to do it (Couldn't find any documentation about filters)

 

The Filter specification was KILLING ME!    Thank you for sharing this.  It worked perfectly once I knew the context and syntax. 

Badge

Folks,

Is it possible to automate VM snapshots process with Ansible ?

I want to take VM’s snapshot as well as restore the same before/after maintenance activity.

Badge

Hi

I added an Ansible role to take snapshots of a VM : https://github.com/Fredouye/nutanix_ansible

There are also roles to create a VM, and to add disks to an existing VM.

Hi,

 

Could someone help below requirements?

 

How to use ansible to remove nutanix vm ?

How to get if the vm snapshot sucessful or failure with ansible?

The vms are placed different nutanix cluster while taking vm snapshot how to make condition if vm not present in one cluster then check for other cluster?

How to list vm snapshot along with date ansible?

 

Regards

Raj

Badge

Hi

you can easily use Ansible to delete a VM, using the “DELETE /vms/{{ uuid }}” API call.

My Ansible role checks for snapshot completion, if there’s an issue the task will fail.

To list snapshots, you can use the “GET /snapshots” API call.

When taking a snapshot, you don’t need to specify the cluster, but I guess your Prism has to manage all your hosts. I’m new to Nutanix, please forgive me if I’m wrong :)

Hi,

Thanks for the information.Could you help to provide complete ansible playbook for snapshot removal? I tried it something like below doesn’t seems working.

 

    - name: DELETE VM Snapshot {{ inventory_hostname }}
      uri:
        url: "{{ base_urlv2 }}/{{ vm_uuid }}"
        method: DELETE
        user: "{{ username }}"
        password: "{{ password }}"
        body:
         snapshot_specs:
         - uuid: "{{ vm_uuid }}"
           snapshot_name: "{{ inventory_hostname }}_test"
        body_format: json
        headers:
          Content-Type: application/json
          Accept: application/json
        force_basic_auth: yes
        validate_certs: no
        status_code: 201
 

Yes we are using prism console to manage hosts/VM.We have vm placed in different cluster it would be great if any condition can be made if vm doesn’t present in one cluster then search for other.

 

Regards

Raj

Badge

I added to role which removes all the snapshots of a given VM : https://github.com/Fredouye/nutanix_ansible/tree/main/roles/vm_snapshots_remove

Hi Fredouye,

 

Thanks.It’s working as expected.

 

Is there way to get snapshot creation time in human readable format? By default it shows in epoch format.

 

Is there way to make condition if the vm not present in one prism then check other prism? The challenge is we have vm’s placed across different cluster manage through different prism.

 

Regards

Raj

Badge

Hi Raj

 

it seems Nutanix’s API only returns dates in micro seconds :

 

"json": {
"cluster_uuid": "0005cec4-5d52-efbf-2bb0-00505698bc02",
"complete_time_usecs": 1636384114684320,
"create_time_usecs": 1636384114376874

 

You can use strfime filter to convert it to human readable format : https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#handling-dates-and-times

 

- name: Wait till task has ended
uri:
url: '{{ api_url_v2 }}/tasks/{{ task_uuid }}'
method: GET
validate_certs: false
force_basic_auth: true
url_username: "{{ prism_user }}"
url_password: "{{ prism_password }}"
return_content: true
register: task_status
delegate_to: localhost
retries: 10
delay: 5
until: task_status.json.progress_status == "Succeeded"

- name: Display snapshot creation time to human readable format
debug:
msg: "Snapshot creation time : {{ '%Y-%m-%d %H:%M:%S' | strftime((task_status.json.create_time_usecs|string)[:10]) }}"

 

The result will be :

 

TASK [snap_nutanix : Display snapshot creation time to human readable format] **********************************************************************************
ok: [rhel7-1] => {
"msg": "Snapshot creation time : 2021-11-08 16:24:04"
}

 

Badge

Regarding multiple clusters, as I only have access to a single node Community Edition cluster, I don’t know…

 

Maybe you can use “GET /vms/”, and loop over your clusters until you find the VM.

Hi Fredouye,

 

Thanks for all the previous help.

 

Could you help with below issues?

- During snapshot creation sometimes facing below issues while taking bunch of vms? Is there any timeout or something need to be increased?

 

TASK [Get the UUID of VM] **********************************************************************************************************************************************
Sunday 28 November 2021 23:27:21 +0800 (0:00:17.746) 0:00:17.746 *******
fatal: FAILED! => {"changed": false, "content": "", "elapsed": 30, "msg": "Status code was -1 and not [200]: Connection failure: ('The read operation timed out',)", "redirected": false, "status": -1, "url": ""}

 

- The condition are failing(Wait till task complete) for few vm's after taking snapshot though snapshot sucessful in previous task 

{"msg": "The conditional check 'task_status.json.progress_status == \"Succeeded\"' failed. The error was: error while evaluating conditional (task_status.json.progress_status == \"Succeeded\"): 'dict object' has no attribute 'json'"}

 

 

- The provided deletion snapshot playbook works well but it removes all the snapshots associated with VM.We have requirement to delete specific snapshot?

- We are passing the VM name in the form of inventory file however for few vm's the snapshot failing due to the inventory name and VM display name are case-sensitive ?

 

 

Regards

Raj

Reply