Deploy VM to vCenter with Ansible

2017-03-21 15:19:51

ready work

  • install ansible
    1
    2
    3
    4
    5
    6
    # yum install epel-release -y
    # yum install python2-pip -y
    # pip install pysphere
    # yum install ansible -y
    [root@localhost ~]# ansible --version
    ansible 2.2.1.0

ansible config

  • create our inventory list

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@localhost vm-playbook]# cat create_hosts 
    [dev-vms]
    ans-dev[127:128] disk='50' datastore='datastore1' network='VM Network' memory='256' cpucount='2' osid='centos64Guest'

    ans-dev[127:128]= - actual VM names that we will be creating
    disk= — is the size in GB to create for each VM
    datastore= — name of the datastore to create the VMs on
    network= — name of network (portgroup) to place the VMs on
    memory= — the amount of memory in MB to allocate to the VMs
    cpucount= — the number of vCPU’s to allocate to the VMs
    osid= — the OS Identifier to specify for the OS type…. reference this guide for OSID’s
  • create the actual ansible playbook to run to create the VMs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    [root@localhost vm-playbook]# cat create_vms.yml 
    ---
    - hosts: all
    gather_facts: false
    connection: local
    user: root
    sudo: false
    serial: 1

    vars_prompt:
    - name: "vcenter_hostname"
    prompt: "Enter vcenter hostname"
    private: no
    default: "vcsa"
    - name: "vcenter_user"
    prompt: "Enter vcenter username"
    private: no
    - name: "vcenter_pass"
    prompt: "Enter vcenter password"
    private: yes

    vars:
    datacenter: 'whistle'
    esxi_host: '192.168.100.120'
    notes: 'Created by Ansible'

    tasks:
    - vsphere_guest:
    vcenter_hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_user }}"
    password: "{{ vcenter_pass }}"
    guest: "{{ inventory_hostname }}"
    state: present
    vm_extra_config:
    notes: "{{ notes }}"
    vcpu.hotadd: yes
    mem.hotadd: yes
    #folder: ansible_test
    vm_disk:
    disk1:
    size_gb: "{{ disk }}"
    type: thin
    datastore: "{{ datastore }}"
    # VMs can be put into folders. The value given here is either the full path
    # to the folder (e.g. production/customerA/lamp) or just the last component
    # of the path (e.g. lamp):
    #folder: production/customerA/lamp
    vm_nic:
    nic1:
    type: e1000
    network: "{{ network }}"
    network_type: standard
    vm_hardware:
    memory_mb: "{{ memory }}"
    num_cpus: "{{ cpucount }}"
    osid: "{{ osid }}"
    scsi: paravirtual
    vm_cdrom:
    type: "iso"
    iso_path: "datastore1/iso/CentOS-Basic7.3-01071715.iso"
    esxi:
    datacenter: "{{ datacenter }}"
    hostname: "{{ esxi_host }}"
  • now ready to run your playbook

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [root@localhost vm-playbook]# ansible-playbook -i create_hosts create_vms.yml 
    SSH password:
    [DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default).
    This feature will be removed in a future release. Deprecation
    warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
    Enter vcenter hostname [vcsa]: 192.168.100.120
    Enter vcenter username: administrator
    Enter vcenter password:

    PLAY [all] *********************************************************************

    TASK [vsphere_guest] ***********************************************************
    changed: [ans-dev127]

    PLAY [all] *********************************************************************

    TASK [vsphere_guest] ***********************************************************
    changed: [ans-dev128]

    PLAY RECAP *********************************************************************
    ans-dev127 : ok=1 changed=1 unreachable=0 failed=0
    ans-dev128 : ok=1 changed=1 unreachable=0 failed=0

ref

vsphere_guest - Create/delete/manage a guest VM through VMware vSphere
Creating vSphere VM’s using Ansible
Create multiple VMware guests with vsphere_guest module
Managing VMware vSphere guests with Ansible Tower


您的鼓励是我写作最大的动力

俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。