Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

Increase vm name by 1digit in ansible

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would use shell module to increment the value of the last integer part of the last element of the list.

---

- name: Sample playbook
  connection: local
  gather_facts: false
  hosts: localhost
  vars:
    domains:
      - Vmdemo1000
      - Vmdemo1001
      - Vmdemo1002
  tasks:
    - name: Get the new domain name(increment +1 to last domain)
      shell: echo {{ domains |last }} |perl -pe  's/(-*d+)$/$1+1/e'
      register: new_domain

    - debug: var=new_domain.stdout

Above would result in the below output:

PLAY [Sample playbook] *********************************************************************************************************************************************************************

TASK [Get the new domain name(increment +1 to last domain)] ********************************************************************************************************************************
changed: [localhost]

TASK [debug] *******************************************************************************************************************************************************************************
ok: [localhost] => {
    "new_domain.stdout": "Vmdemo1003"
}

PLAY RECAP *********************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...