简体   繁体   中英

Deploy massive VM guest on VmWare with ansible playbook

I wrote a playbook for ansible to create a virtual machine on my vmware infrastructure. It works very well, but I do not know how to go through a list of machines to be created, does anyone have any idea if it is possible to do?

this is my playbook:

hosts: all
gather_facts: false
vars_files:
 - variables.yml
connection: local
   tasks:
      - name: configura install cfg
        template:
          src: template_install.j2
          dest:  /root/cfg/{{ vm_name }}.cfg

      - name: configura isolinux.cfg
        template:
          src: isolinux.j2
          dest:  /root/rhel/isolinux/isolinux.cfg

      - name: copia file su satellite
        shell: scp /root/cfg/{{ vm_name }}.cfg root@10.100.64.50:/var/ftp/pub/kickstart/.

      - name: make cd iso for install red hat
          shell: /root/makecdrom.sh {{ vm_name }}

      - name: upload cdrom to library datastore
          vsphere_copy:
          host: <Myvcentername>
          login: <myvcenterusername>
          password:  <Mycenterpassword>
          validate_certs: no
          src: /root/{{ vm_name}}.iso
          datacenter: {{ vm_datacenter }}
          datastore: VMW_LIBRARY01
          path: ISO/{{ vm_name }}.iso

      - name: Crea VM 
          vmware_guest:
          hostname: <Myvcentername>
          login: <myvcenterusername>
          password:  <Mycenterpassword>
          datacenter: "{{ vm_datacenter }}"
          resource_pool: "{{ vm_resource_pool }}"
          validate_certs: no
          annotation: 'creata con script ansible ip address " {{ vm_ip_addr }} "'
          folder: "{{ vm_folder }}"
          name: "{{ vm_name }}"
          state: "{{ vm_state }}"
          guest_id: "{{ vm_guest_id }}"
          cluster: "{{ vm_cluster }}"
             cdrom:
               type: iso
               iso_path: "[VMW_LIBRARY01] ISO/'{{vm_name}}.iso'"
             disk:
               - size_gb: "{{ disk_size_gb }}"
                 type: "{{ disk_type }} "
                 datastore: "{{ disk_datastore }}"
             hardware:
                 memory_mb: "{{ hardware_memory_mb }}"
                 num_cpus: "{{ hardware_num_cpus }}"
             scsi: paravirtual
             networks:
               - name: "{{ networks_name }}"
                 device_type: vmxnet3
          delegate_to: 127.0.0.1
          register: deploy_vm

and this is variables file

vm_netmask: 255.255.252.0
vm_gateway: 10.100.8.1
vm_fqdn_name: mcdlnxevtp01.menarini.net
vm_name: MCDLNXEVTP01
vm_folder: APP - EASYVISTA
vm_cluster: MCDVMWCLU02
vm_resource_pool: 06 - PRD
networks_name: dPG_3008_PRD
disk_datastore: VMW_PRD_DS02
Vm_OS: Red Hat 7.6
vm_datacenter: MDC-METROPOOL
vm_state: poweredoff
vm_guest_id: rhel7_64Guest
disk_size_gb: 50
disk_type: thin
hardware_memory_mb: 8192
hardware_num_cpus: 4
hardware_scsi: paravirtual
vm_validate_certs: no
networks_device_type: vmxnet3

thanks for your answer... simone

Indeed that's possible. I would go for it by splitting variables into global variables for the whole inventory (like the gateway - it's the same for all or the group of servers) and for each server. You can do that by creating a file with inventory list and variables list:

inventory/
  my_vms/
    hosts
    group_vars/
      all.yml
      south-dc.yml
      north-dc.yml
    host_vars/
      my_vm_1.yml
      my_vm_2.yml

The hosts file contains list of servers (VMs in your case). It could contain something like:

[all]
my_vm_1
my_vm_2

[south-dc]
my_vm_1

[north-dc]
my_vm_2

The host_vars directory contains YAML files with variables named after hosts in hosts file ie if you name your server as my_vm_1 the file would be named my_vm_1.yml

The group_vars is similar to host_vars but you provide variables that are shared by the group of hosts. Special group called all provides global variables for all hosts.

All the global variables can be placed in all.yml . For instance you could put these in that file:

disk_size_gb: 50
disk_type: thin
hardware_memory_mb: 8192
hardware_num_cpus: 4

All the per host variables would then go to the inventory host_vars file:

vm_fqdn_name: mcdlnxevtp01.menarini.net
vm_name: MCDLNXEVTP01

This way you provide consistent way of categorising your inventory giving you opportunity to clean up your code

You can then use it by simply calling

ansible-playbook -i inventory/my_vms -l my_vm_1 playbook.yml

If you wish to run the playbook against the group of servers just launch it with -l <groupname> or -l all for all servers in the inventory.

PS: You don't have to put servers in [all] group. It's special group that contains all servers in the inventory.

I prefer to use inventories only for configuration management. You can do that way but how do you keep track of changes or how do you add new item to the stack?

From my point of view, the best way is using a yaml configuration file like this:

vmstodeploy:
- name: myvm
  vm_ip_addr: 192.168.0.15
  vm_netmask: 255.255.255.0
  vm_gateway: 192.168.0.1
  etc...

And keeping it in a git repository for change control. If you want to add more vms to the stack, just add another vm:

vmstodeploy:
- name: myvm
  vm_ip_addr: 192.168.0.15
  vm_netmask: 255.255.255.0
  vm_gateway: 192.168.0.1
- name: myvm2
  vm_ip_addr: 192.168.0.16
  vm_netmask: 255.255.255.0
  vm_gateway: 192.168.0.1

And read it with:

- name: Read My VMs
    include_vars:
      file: "MyConfigFile"
      name: mydeploy

From here, you will have a list of vm deploy information in mydeploy var.

Just create a loop inside a role like this:

  - name: Deploy VMs
    include_tasks: vmware_deploy.yml
    with_items: "{{ mydeploy|default([]) }}"
    loop_control:
      loop_var: _myvms

From there, create your vms inside the vmware_deploy.yml file:

- name: Create my VMs
  vmware_guest:

Play with with_sequence if vms have the same data, use batch() to serialize your deploys, experiment with different options, but always, starting with a configuration file in yaml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM