I have ansible playbook which get vm name from vcenter. I want to generate new VM name base on last VM name exist in vcenter. For example I will search vm name starting with vmdemo in vcenter and will get below vm names.
Vmdemo1000
Vmdemo1001
Vmdemo1002
So I want to generate my next new VM name would be vmdemo1003
How can I achieve this.
- name: Gather Existing VM Details
vmware_vm_facts:
hostname: "{{ vsphere_host }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_password }}"
validate_certs: no
vm_type: vm
register: info
ignore_errors: true
- set_fact:
server_list: []
- set_fact:
server_list: "{{ server_list + [ item.guest_name ] }}"
when: (item.guest_name | string).find('{{ vm_name }}') != -1
with_items: "{{ info.virtual_machines| list }}"
loop_control:
label: " "
- debug: msg="{{ server_list }}"
- debug: msg="{{ server_list | sort }}"
- set_fact:
new_vm_name: "{{ vm_name }}{{ default_no }}"
when: server_list == []
- set_fact:
new_vm_name: "{{ ((server_list | sort | max) | int) +1 }}"
when: server_list != []
- debug: msg="{{ new_vm_name }}"
I am trying above code but getting new_vm_name as 1 value instead of server name
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…