# How can I run the commnads in Linux vm after creation with vmware\_guest

**URL:** <https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [September 18, 2019, 11:36am UTC](https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306 "2019-09-18T11:36:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phani\_akkina](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@phani\_akkina](https://forum.ansible.com/u/phani_akkina)\
**Post date:** [September 18, 2019, 11:36am UTC](https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306/1 "2019-09-18T11:36:37Z")

</div>

Hi All,

I am deploying vms using **vmware\_guest** module in vcenter.

I need to run some commands after the creation of the vms either Windows or Linux. I am able run the commands in Windows with runonce.

Now I blocked to know how to run the commands in Linux after creation.

Can anyone guide me on this?

Thanks,  
Phani Akkina

---

<div class="post-metadata">

**Author:** ![Aravind\_Balaji](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@Aravind\_Balaji](https://forum.ansible.com/u/Aravind_Balaji)\
**Post date:** [September 18, 2019, 1:03pm UTC](https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306/2 "2019-09-18T13:03:36Z")

</div>

Hi phani akkina,

With the help of vmware\_vm\_shell module([https://docs.ansible.com/ansible/latest/modules/vmware\_vm\_shell\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_vm_shell_module.html)) you can execute command inside linux vm.

**Thanks & Regards** ,  
Aravind Balaji S

---

<div class="post-metadata">

**Author:** ![phani\_akkina](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@phani\_akkina](https://forum.ansible.com/u/phani_akkina)\
**Post date:** [September 19, 2019, 7:46am UTC](https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306/3 "2019-09-19T07:46:40Z")

</div>

Hi Arvind,

Thanks for your reply.

I need to run the commands in Linux after the creation of the vm by vmware\_guest. And these commands also need to run only once. Is that possible with vmware\_vm\_shell? If yes do you have any example?

Thanks,  
Phani Akkina

---

<div class="post-metadata">

**Author:** ![Aravind\_Balaji](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@Aravind\_Balaji](https://forum.ansible.com/u/Aravind_Balaji)\
**Post date:** [September 19, 2019, 9:39am UTC](https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306/4 "2019-09-19T09:39:50Z")

</div>

Hi Phani Akkina

Here is the example:

- name: Create a virtual machine on given ESXi hostname  
vmware\_guest:  
hostname: “{{ vcenter\_hostname }}”  
username: “{{ vcenter\_username }}”  
password: “{{ vcenter\_password }}”  
validate\_certs: no  
folder: /DC1/vm/  
name: test\_vm\_0001  
state: poweredon  
guest\_id: centos64Guest

# This is hostname of particular ESXi server on which user wants VM to be deployed

esxi\_hostname: “{{ esxi\_hostname }}”  
disk:

- size\_gb: 10  
type: thin  
datastore: datastore1  
hardware:  
memory\_mb: 512  
num\_cpus: 4  
scsi: paravirtual  
networks:

- name: VM Network  
mac: aa:bb:dd:aa:00:14  
ip: 10.10.10.100  
netmask: 255.255.255.0  
device\_type: vmxnet3  
wait\_for\_ip\_address: yes  
delegate\_to: localhost  
register: deploy\_vm

- name: Change hostname of guest machine  
vmware\_vm\_shell:  
hostname: “{{ vcenter\_hostname }}”  
username: “{{ vcenter\_username }}”  
password: “{{ vcenter\_password }}”  
validate\_certs: no  
datacenter: “DC1”  
folder: “/DC1/vm”  
vm\_id: “{{ vm\_name }}”  
vm\_username: testUser  
vm\_password: SuperSecretPassword  
vm\_shell: “/usr/bin/hostnamectl”  
vm\_shell\_args: “set-hostname new\_hostname \> /tmp/$$.txt 2\>&1”  
delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![phani\_akkina](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@phani\_akkina](https://forum.ansible.com/u/phani_akkina)\
**Post date:** [September 19, 2019, 11:40am UTC](https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306/5 "2019-09-19T11:40:38Z")

</div>

Hi Aravind,

This will execute the commands on the vm whenever I run the playbook. But I want to run the commands when vm is created only.

Thanks,  
Phani Akkina

---

<div class="post-metadata">

**Author:** ![Aravind\_Balaji](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@Aravind\_Balaji](https://forum.ansible.com/u/Aravind_Balaji)\
**Post date:** [September 20, 2019, 6:41am UTC](https://forum.ansible.com/t/how-can-i-run-the-commnads-in-linux-vm-after-creation-with-vmware-guest/31306/6 "2019-09-20T06:41:37Z")

</div>

Hi Phani Akkina,

In the response of vmware\_guest module you can find whether VM is created or not while executing vmware\_guest module.

Use the response of vmware\_guest module and execute vmware\_vm\_shell module.

**Thanks & Regards** ,  
Aravind Balaii S
