--- - hosts: 127.0.0.1 connection: local user: root sudo: false gather_facts: false serial: 1 vars: vcenter_hostname: UK.server.local esxhost: xxx.xxx.xxx.xxx datastore: UK1 network: Web vmcluster: UKCLUSTER guest_name: server1, server2, server3, server4 folder: Utilities notes: Created by Ansible tasks: - name: Create VM from template vsphere_guest: vcenter_hostname: "{{ vcenter_hostname }}" username: "{{ username }}" password: "{{ password }}" guest: "{{ guest_name }}" vm_extra_config: notes: "{{ notes }}" folder: "{{ folder }}" from_template: yes template_src: "{{ vmtemplate }}" cluster: "{{ vmcluster }}" resource_pool: "/Resources" esxi: datacenter: UK hostname: "{{ esxhost }}"
Any ideas or suggestions would be really appreciated.
Cheers
---
serverlist:
- server1
- server2---
- hosts: 127.0.0.1
connection: local
user: root
sudo: false
gather_facts: false
serial: 1
vars:
vcenter_hostname: UK.server.local
esxhost: xxx.xxx.xxx.xxx
datastore: UK1
network: Web
vmcluster: UKCLUSTER
serverlist: files/hostnames
folder: Utilities
notes: Created by Ansible
tasks:
- name: Create VM from template
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
guest: "{{ list }}"
vm_extra_config:
notes: "{{ notes }}"
folder: "{{ folder }}"
from_template: yes
template_src: "{{ vmtemplate }}"
cluster: "{{ vmcluster }}"
resource_pool: "/Resources"
with_items: "{{ serverlist }}
esxi:
datacenter: UK
hostname: "{{ esxhost }}"with_items: "{{ serverlist }}The other thing is actually getting your file containing the serverlist variable loaded into Ansible. There's at least a couple of ways of doing this - either do an include_vars: file_containing_vars.yml at the top of your playbook, or you can pass in the contents of a yaml file on the command line using -e @/path/to/yaml/file.yml. Not sure what is going to suit your usage best
should probably be:
with_items: "{{ serverlist }}"
I have then put the variable "{{ list }}" for the guest option, but how does Ansible know to create multiple VM's with those servers names?
I’m not very familiar with that module, but the iterator on a loop (which is effectively what you have with the with_items stanza) is “item" and not “list”
So try changing the guest part to:-
guest: “{{ item }”
Nigel.
[ Nigel Metheringham ------------------------------ ni...@dotdot.it ]
[ Ellipsis Intangible Technologies ]
--- - hosts: 127.0.0.1 connection: local user: root sudo: false gather_facts: false serial: 1 vars: vcenter_hostname: UK.server.local esxhost: xxx.xxx.xxx.xxx datastore: UK1 network: Web vmcluster: UKCLUSTER folder: Utilities notes: Created by Ansible
tasks: - name: pick up hostnames to create include_vars: hostnames.yml - name: Create VM from template vsphere_guest: vcenter_hostname: "{{ vcenter_hostname }}" username: "{{ username }}" password: "{{ password }}" guest: "{{ list }}" vm_extra_config: notes: "{{ notes }}" folder: "{{ folder }}" from_template: yes template_src: "{{ vmtemplate }}" cluster: "{{ vmcluster }}" resource_pool: "/Resources" esxi: datacenter: UK hostname: "{{ esxhost }}" with_items: serverlist
- name: Testing vsphere_guest module in VMware
hosts: localhost
connection: local
vars_prompt:
- name: "vcenter_hostname"
prompt: "Enter vcenter hostname"
private: no
default: "vcsa"
- name: "vcenter_user"
prompt: "Enter vCenter username"
private: no
default: root
- name: "vcenter_pass"
prompt: "Enter vcenter password"
private: yes
- name: "esxi_hostname"
prompt: "Enter esxi hostname"
private: no
tasks:
- name: Testing gater facts from vSphere
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
validate_certs: no
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
guest: "{{ item }}"
from_template: yes
template_src: openSUSELeap42.1_Template
esxi:
datacenter: Lab
hostname: "{{ esxi_hostname }}"
with_items:
- server01
- server02