Copy #Ins Hauptverzeichnis von Ansible navigieren
cd /etc/ansible
Um eine korrekte und saubere Struktur des Codes beizubehalten, folgende Verzeichnisstruktur erstellen:
Copy ---
#file: site.yml
- hosts : all
name : Create a virtual machine
gather_facts : false
roles :
- role : vmware_guest
tags : vmware_guest
Copy ---
#file: inventories/inventory.yml
all :
hosts :
lnx55200 :
ansible_host : 10.100.1.186
vcores : 4
lnx55410 :
#Select guest OS (https://code.vmware.com/apis/358/doc/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html)
os_type : 'ubuntu64Guest'
#Select storage (IBM-Storage-01, IBM-Storage-02, IBM-Storage-03)
datastore : 'IBM-Storage-02'
#Customize settings
memory : '8192'
disk_size : '50'
network : 'Zentrale_Dienste'
iso : '[IBM-Storage-01] ISO/ubuntu-20.04.2-live-server-amd64.iso'
Copy ---
#file: roles/vmware_guest/vars/main.yml
esxi_hostname : vmh94520.css.ch
esxi_username : root
esxi_password : Root123+
Copy ---
#file: roles/vmware_guest/tasks/main.yml
- name : Create a virtual machine
vmware_guest :
hostname : "{{ esxi_hostname }}"
username : "{{ esxi_username }}"
password : "{{ esxi_password }}"
validate_certs : no
folder : /ha-datacenter/vm/
name : "{{ inventory_hostname }}"
state : poweredon
guest_id : "{{ os_type | default('ubuntu64Guest') }}"
esxi_hostname : "{{ esxi_hostname }}"
disk :
- size_gb : "{{ disk_size | default('50') }}"
type : thin
datastore : "{{ datastore | default('IBM-Storage-01') }}"
hardware :
memory_mb : "{{ memory | default('8192') }}"
num_cpus : "{{ vcores | default('2') }}"
scsi : paravirtual
cdrom :
type : iso
iso_path : "{{ iso }}"
networks :
- name : "{{ network | default('Zentrale_Dienste') }}"
#mac: aa:bb:dd:aa:00:14
ip : 10.100.1.230
netmask : 255.255.255.0
device_type : vmxnet3
wait_for_ip_address : yes
delegate_to : localhost
register : deploy_vm
- debug :
var : deploy_vm
- debug :
msg :
- "The Host: {{ inventory_hostname }} was successfully created."
- "With the following macadress: {{ deploy_vm['instance']['hw_eth0']['macaddress'] }}"
- "And the following ipv4-address: {{ deploy_vm['instance']['hw_eth0']['ipaddresses'][0] }}"
Copy #Playbook anschliessend ausführen [/etc/ansible]
ansible-playbook -i /etc/ansible/inventories/inventory.yml
--limit lnx55410 site.yml