How to change the IP and Hostname After create a guest VM through VMware vSphere with vsphere_guest - need help or advice

I am new in ansible…

After create a guest with vsphere_guest i want to be able :

  1. how I can create 5 VM and not just 1
  2. how can I give all of them IPs and Hostnames

Thanks Ben

my playbook:

Hi,

You can make use of the ‘with_items’ option for all the vmtasks. In the example below I only placed 2 of the tasks:

`

  • name: Depoly from Template
    vsphere_guest:
    vcenter_hostname: domain.com
    username: myuser
    password: mypassword
    guest: {{ item.hostname }}
    from_template: yes
    template_src: Template6.5-updated
    cluster: my_cluster
    validate_certs: no
    esxi:
    datacenter: CONNECT-Lab
    hostname: my_esxi_name
    with_items:

  • hostname: host1
    ip: 192.168.1.11

  • hostname: host2
    ip: 192.168.1.12

  • name: Power off the VM server
    vsphere_guest:
    vcenter_hostname: domain.com
    username: myuser
    password: mypassword
    guest: {{ item.hostname }}
    cluster: my_cluster
    validate_certs: no
    state: powered_off
    esxi:
    datacenter: CONNECT-Lab
    hostname: my_esxi_name
    with_items:

  • hostname: host1
    ip: 192.168.1.11

  • hostname: host2
    ip: 192.168.1.12

  • name: “Set correct ip”
    local_action:
    module: vmware_vm_shell
    hostname: domain.com
    username: myuser
    password: mypassword
    datacenter: CONNECT-Lab
    vm_id: {{ item.hostname }}
    vm_username: root
    vm_password: password
    vm_shell: ifconfig
    vm_shell_args: " eth0 {{ item.ip }}"
    vm_shell_env:

  • “PATH=/bin”
    vm_shell_cwd: “/tmp”
    with_items:

  • hostname: host1
    ip: 192.168.1.11

  • hostname: host2
    ip: 192.168.1.12

`

You’ll have to update the other tasks to.

The last task in above example is to set the ip to the vm. I haven’t tested it yet (As I have an other issue right now) but this is how I would do it.

Have fun! Or drop an reply if you encounter issues.

Kind regards,
Werner

hi @Wemer
I saw your answer – I’m hoping you can help me
the vmware_vm_shell module is throwing an error:
“msg”: “Permission to perform this operation was denied.”
I’m pretty sure I used the correct credentials and even add new ones, still nothing.
Do you have any workaround ?

Hi Everyone,

How to change the Hostname of the VM created with vmware_guest Ansible module.

Ajay