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

**URL:** <https://forum.ansible.com/t/how-to-change-the-ip-and-hostname-after-create-a-guest-vm-through-vmware-vsphere-with-vsphere-guest-need-help-or-advice/21131>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [April 11, 2016, 4:19pm UTC](https://forum.ansible.com/t/how-to-change-the-ip-and-hostname-after-create-a-guest-vm-through-vmware-vsphere-with-vsphere-guest-need-help-or-advice/21131 "2016-04-11T16:19:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben1](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Ben1](https://forum.ansible.com/u/Ben1)\
**Post date:** [April 11, 2016, 4:19pm UTC](https://forum.ansible.com/t/how-to-change-the-ip-and-hostname-after-create-a-guest-vm-through-vmware-vsphere-with-vsphere-guest-need-help-or-advice/21131/1 "2016-04-11T16:19:28Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Werner\_Dijkerman](https://avatars.discourse-cdn.com/v4/letter/w/8491ac/32.png) [@Werner\_Dijkerman](https://forum.ansible.com/u/Werner_Dijkerman)\
**Post date:** [May 1, 2016, 12:44pm UTC](https://forum.ansible.com/t/how-to-change-the-ip-and-hostname-after-create-a-guest-vm-through-vmware-vsphere-with-vsphere-guest-need-help-or-advice/21131/2 "2016-05-01T12:44:01Z")

</div>

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](http://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](http://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](http://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

---

<div class="post-metadata">

**Author:** ![raid.rohana](https://avatars.discourse-cdn.com/v4/letter/r/ecc23a/32.png) [@raid.rohana](https://forum.ansible.com/u/raid.rohana)\
**Post date:** [December 15, 2016, 6:09pm UTC](https://forum.ansible.com/t/how-to-change-the-ip-and-hostname-after-create-a-guest-vm-through-vmware-vsphere-with-vsphere-guest-need-help-or-advice/21131/3 "2016-12-15T18:09:33Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Ajay\_Sharma1](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@Ajay\_Sharma1](https://forum.ansible.com/u/Ajay_Sharma1)\
**Post date:** [January 25, 2018, 7:27am UTC](https://forum.ansible.com/t/how-to-change-the-ip-and-hostname-after-create-a-guest-vm-through-vmware-vsphere-with-vsphere-guest-need-help-or-advice/21131/4 "2018-01-25T07:27:35Z")

</div>

Hi Everyone,

How to change the Hostname of the VM created with vmware\_guest Ansible module.

Ajay
