Create VM from template using vsphere_guest module

Hello, I am new to Ansible and am having trouble spinning up a vm from a template using Ansible. I am unsure on what to put for “hosts” in my playbook. Should this be the vcenter hostname , vsphere hostname or something else? Any help is appreciated. Thanks in advance.

PlayBook

In this case you can probably just use

hosts: localhost

because vsphere_guest will be using an api to talk to your vcenter.

By the way, there is a lot of development work going on around the vmware_guest module at the moment. If you can test it out on latest development version of ansible, now would be a good time to report any issues.

Jon

I tried adding local but that did not help. Here is the output:

Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: create_appVCLOUD.yml *************************************************
1 plays in create_appVCLOUD.yml

PLAY RECAP *********************************************************************

Here is the error message when I use localhost in the playbook.

ERROR MESSAGE

Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: create_appVCLOUD.yml *************************************************
1 plays in create_appVCLOUD.yml

PLAY [localhost] ***************************************************************

TASK [vsphere_guest] ***********************************************************
task path: /etc/ansible/create_appVCLOUD.yml:6
Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/cloud/vmware/vsphere_guest.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c ‘( umask 77 && mkdir -p “echo $HOME/.ansible/tmp/ansible-tmp-1484165467.41-270348352264162” && echo ansible-tmp-1484165467.41-270348352264162=“echo $HOME/.ansible/tmp/ansible-tmp-1484165467.41-270348352264162” ) && sleep 0’
<127.0.0.1> PUT /tmp/tmp_bDCQ3 TO /root/.ansible/tmp/ansible-tmp-1484165467.41-270348352264162/vsphere_guest.py
<127.0.0.1> EXEC /bin/sh -c ‘chmod u+x /root/.ansible/tmp/ansible-tmp-1484165467.41-270348352264162/ /root/.ansible/tmp/ansible-tmp-1484165467.41-270348352264162/vsphere_guest.py && sleep 0’
<127.0.0.1> EXEC /bin/sh -c ‘/usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1484165467.41-270348352264162/vsphere_guest.py; rm -rf “/root/.ansible/tmp/ansible-tmp-1484165467.41-270348352264162/” > /dev/null 2>&1 && sleep 0’
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File “/tmp/ansible_vw2rQu/ansible_module_vsphere_guest.py”, line 1909, in
main()
File “/tmp/ansible_vw2rQu/ansible_module_vsphere_guest.py”, line 1776, in main
viserver.connect(vcenter_hostname, username, password)
File “build/bdist.linux-x86_64/egg/pysphere/vi_server.py”, line 101, in connect
File “build/bdist.linux-x86_64/egg/pysphere/resources/VimService_services.py”, line 2170, in RetrieveServiceContent
File “build/bdist.linux-x86_64/egg/pysphere/ZSI/client.py”, line 295, in Send
File “/usr/lib64/python2.7/httplib.py”, line 1227, in connect
HTTPConnection.connect(self)
File “/usr/lib64/python2.7/httplib.py”, line 807, in connect
self.timeout, self.source_address)
File “/usr/lib64/python2.7/socket.py”, line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
fatal: [localhost]: FAILED! => {
“changed”: false,
“failed”: true,
“invocation”: {
“module_name”: “vsphere_guest”
},
“module_stderr”: “Traceback (most recent call last):\n File "/tmp/ansible_vw2rQu/ansible_module_vsphere_guest.py", line 1909, in \n main()\n File "/tmp/ansible_vw2rQu/ansible_module_vsphere_guest.py", line 1776, in main\n viserver.connect(vcenter_hostname, username, password)\n File "build/bdist.linux-x86_64/egg/pysphere/vi_server.py", line 101, in connect\n File "build/bdist.linux-x86_64/egg/pysphere/resources/VimService_services.py", line 2170, in RetrieveServiceContent\n File "build/bdist.linux-x86_64/egg/pysphere/ZSI/client.py", line 295, in Send\n File "/usr/lib64/python2.7/httplib.py", line 1227, in connect\n HTTPConnection.connect(self)\n File "/usr/lib64/python2.7/httplib.py", line 807, in connect\n self.timeout, self.source_address)\n File "/usr/lib64/python2.7/socket.py", line 571, in create_connection\n raise err\nsocket.error: [Errno 111] Connection refused\n”,
“module_stdout”: “”,
“msg”: “MODULE FAILURE”
}
to retry, use: --limit @/etc/ansible/create_appVCLOUD.retry

PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1