Skip to content

Instantly share code, notes, and snippets.

@gowatana
Created October 30, 2023 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gowatana/3151124a28da64328fe69320da37b89c to your computer and use it in GitHub Desktop.
Save gowatana/3151124a28da64328fe69320da37b89c to your computer and use it in GitHub Desktop.
---
- name: Get VM Description-Tags
hosts: localhost
gather_facts: false
collections:
- nutanix.ncp
module_defaults:
group/nutanix.ncp.ntnx:
nutanix_host: 'lab-nxce-02.go-lab.jp'
nutanix_username: admin
nutanix_password: nutanix/4u
validate_certs: false
vars:
target_vm_name: "vm01"
tasks:
- name: Set VM Name
set_fact:
vm_name: "{{ target_vm_name }}"
- name: Get VM info by vm_name
ntnx_vms_info:
filter:
vm_name: "{{ vm_name }}"
kind: vm
register: result
ignore_errors: True
- name: Get VM UUID
set_fact:
vm_uuid: "{{ result.response.entities[0].metadata.uuid }}"
- name: Get description (from VM)
set_fact:
tags: "{{ result.response.entities[0].spec.description }}"
- name: Output VM UUID
debug:
var: vm_uuid
- name: Output VM desc
debug:
var: tags
- name: Output tags
debug:
var: tags
- name: Split description (from VM desc)
set_fact:
tags: "{{ tags.split(',') | map('trim') | list }}"
- name: Convert tags (list to dict)
set_fact:
dict_tag: "{{ dict_tag | default({}) | combine({item.split('=')[0]: item.split('=')[1]}) }}"
loop: "{{ tags }}"
- name: Output the dict
debug:
var: dict_tag
- name: Test VM-Tag (env=dev)
debug:
msg: "test-ok"
when: dict_tag["env"] == "dev"
@gowatana
Copy link
Author

下記の投稿むけ。

Nutanix UVM の Description フィールドを Tag 欄として利用してみる。(Ansible)
https://blog.ntnx.jp/entry/2023/10/31/034114

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment