├── LICENSE ├── README.md ├── deploy_vmware_guest.yml ├── group_vars └── example_group.yml ├── hosts_to_deploy └── roles └── deploy_vmware_guest └── tasks └── main.yml /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Denis GERMAIN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ansible-deploy-vmware-guest 2 | Playbook and role to deploy VMware virtual machine from templates using Ansible 3 | 4 | # Installation 5 | You can add this role by simple a simple git clone 6 | 7 | ``` 8 | git clone https://github.com/zwindler/ansible-deploy-vmware-guest 9 | cp -rp ansible-deploy-vmware-guest/deploy_vmware_guest.yml ansible-deploy-vmware-guest/roles /etc/ansible 10 | ``` 11 | 12 | # Configuration 13 | 14 | You should create a group_var file in order to set the variables that don't usually change in order to simplify playbook file, such as 15 | * **vsphere_host**, the address/hostname of the vcenter server 16 | * **vsphere_user**, a username with enough vSphere privileges to create VMs from templates 17 | * **vsphere_datacenter**, the vSphere datacenter name 18 | * **vsphere_folder**, the folder where to drop VM ("/TO_DEPLOY" for ex.) 19 | * **guest_network**, **guest_netmask**, **guest_gateway**, **guest_dns_server**, **guest_domain_name**, the usual network informations for your VMs 20 | * **guest_id** depending on the OS you want to deploy 21 | * **guest_memory** and **guest_vcpu** for your VM sizing 22 | * **guest_template**, the template name to copy the VM from 23 | * **vsphere_host**, **vsphere_user** (usualy I use a **group_var/mygroup/main.yml** file, group being declared in **hosts_to_deploy** inventory file) 24 | 25 | Then you should create a **hosts_to_deploy** file that will be a temporary inventory file to store the hostname to deploy, and add them inside this group. 26 | 27 | Examples of theses files can be found inside this repository, under **group_vars/example_group.yml** and **hosts_to_deploy**. 28 | 29 | # Execution 30 | 31 | Don't forget the vmware_guest variables that already are playbook variables such as 32 | * **notes**, **vsphere_password**, **esxi_host** (prompted by playbook) 33 | * **creationdate** (automatically created) 34 | * **vsphere_datastore**, **guest_custom_ip** (usualy I put these as variables of the host in a **hosts_to_deploy** file) 35 | -------------------------------------------------------------------------------- /deploy_vmware_guest.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: all 3 | gather_facts: false 4 | 5 | vars_prompt: 6 | - name: "vsphere_password" 7 | prompt: "vSphere Password" 8 | - name: "esxi_host" 9 | prompt: "ESXi host" 10 | private: no 11 | default: "my_esxi_default_server" 12 | - name: "notes" 13 | prompt: "VM notes" 14 | private: no 15 | default: "Deployed with ansible" 16 | 17 | roles: 18 | - deploy_vmware_guest 19 | #- other roles to execute directly inside VM after deployment 20 | #- ... 21 | -------------------------------------------------------------------------------- /group_vars/example_group.yml: -------------------------------------------------------------------------------- 1 | --- 2 | #Usual VMware guest deployment variables for example_group group 3 | vsphere_host: "my-vcenter-host.zwindler.fr" 4 | vsphere_user: "poweruser" 5 | vsphere_datacenter: "myDC" 6 | vsphere_folder: "/TO_DEPLOY" 7 | guest_network: VM_Network 8 | guest_netmask: 255.255.255.0 9 | guest_gateway: 192.168.1.1 10 | guest_dns_server: 192.168.1.10 11 | guest_domain_name: zwindler.fr 12 | guest_id: rhel7_64Guest 13 | guest_memory: 2048 14 | guest_vcpu: 2 15 | guest_template: my_template 16 | -------------------------------------------------------------------------------- /hosts_to_deploy: -------------------------------------------------------------------------------- 1 | [example_group] 2 | zwindler01 guest_custom_ip='192.168.1.101' vsphere_datastore='VMFS01' ansible_host_group='example_group' 3 | zwindler02 guest_custom_ip='192.168.1.102' vsphere_datastore='VMFS01' ansible_host_group='example_group' 4 | zwindler03 guest_custom_ip='192.168.1.103' vsphere_datastore='VMFS01' ansible_host_group='example_group' 5 | zwindler04 guest_custom_ip='192.168.1.104' vsphere_datastore='VMFS01' ansible_host_group='example_group' 6 | -------------------------------------------------------------------------------- /roles/deploy_vmware_guest/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # get date 3 | - set_fact: creationdate="{{lookup('pipe','date "+%Y/%m/%d %H:%M"')}}" 4 | 5 | # Create a VM from a template 6 | - name: create the VM 7 | vmware_guest: 8 | hostname: '{{ vsphere_host }}' 9 | username: '{{ vsphere_user }}' 10 | password: '{{ vsphere_password }}' 11 | validate_certs: no 12 | esxi_hostname: '{{ esxi_host }}' 13 | datacenter: '{{ vsphere_datacenter }}' 14 | folder: '{{ vsphere_folder }}' 15 | name: '{{ inventory_hostname }}' 16 | state: poweredon 17 | guest_id: '{{ guest_id }}' 18 | annotation: "{{ notes }} - {{ creationdate }}" 19 | disk: 20 | - size_gb: 150 21 | type: thin 22 | datastore: '{{ vsphere_datastore }}' 23 | networks: 24 | - name: '{{ guest_network }}' 25 | ip: '{{ guest_custom_ip }}' 26 | netmask: '{{ guest_netmask }}' 27 | gateway: '{{ guest_gateway }}' 28 | dns_servers: 29 | - '{{ guest_dns_server }}' 30 | hardware: 31 | memory_mb: '{{ guest_memory }}' 32 | num_cpus: '{{ guest_vcpu }}' 33 | customization: 34 | dns_servers: 35 | - '{{ guest_dns_server }}' 36 | domain : '{{ guest_domain_name }}' 37 | hostname: '{{ inventory_hostname }}' 38 | template: '{{ guest_template }}' 39 | wait_for_ip_address: yes 40 | delegate_to: localhost 41 | 42 | - name: add to ansible hosts file 43 | lineinfile: 44 | dest: /etc/ansible/hosts 45 | insertafter: '^\[{{ ansible_host_group }}\]' 46 | line: '{{ item }}' 47 | with_items: '{{play_hosts}}' 48 | run_once: true 49 | delegate_to: localhost 50 | 51 | - name: add to /etc/hosts file 52 | lineinfile: 53 | dest: /etc/hosts 54 | line: '{{ guest_custom_ip }} {{ inventory_hostname }}' 55 | with_items: '{{play_hosts}}' 56 | when: "hostvars[item].inventory_hostname == inventory_hostname" 57 | delegate_to: localhost 58 | 59 | - name: accept new ssh fingerprints 60 | shell: ssh-keyscan {{ guest_custom_ip }},{{inventory_hostname}} >> ~/.ssh/known_hosts 61 | with_items: '{{play_hosts}}' 62 | when: "hostvars[item].inventory_hostname == inventory_hostname" 63 | delegate_to: localhost 64 | 65 | - setup: 66 | --------------------------------------------------------------------------------