Skip to content

Instantly share code, notes, and snippets.

@gowatana
Created October 30, 2023 18:06
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/6791d08b00b96e920c523b8917979a85 to your computer and use it in GitHub Desktop.
Save gowatana/6791d08b00b96e920c523b8917979a85 to your computer and use it in GitHub Desktop.
---
- name: Set 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"
vm_desc_tags:
env: dev
app: web
cmdb: id-12345
tasks:
- name: Set VM Name
set_fact:
vm_name: "{{ target_vm_name }}"
- name: Output vm_desc_tags (to set)
debug:
var: vm_desc_tags
- name: Convert vm_desc_tags (dict to list)
set_fact:
tags_list: "{{ tags_list | default([]) + [item.key + '=' + item.value] }}"
loop: "{{ vm_desc_tags | dict2items }}"
loop_control:
loop_var: item
- name: Convert vm_desc_tags (list to string)
set_fact:
tags_string: "{{ tags_list | join(', ') }}"
- name: Output vm_desc_tags string (for VM desc)
debug:
var: tags_string
- 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: Output VM Info (Original)
debug:
var: result.response.entities[0].spec.description
- name: Output VM UUID
debug:
var: vm_uuid
- name: Update VM
ntnx_vms:
vm_uuid: "{{ vm_uuid }}"
desc: "{{ tags_string }}"
register: result
@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