• No results found

VMware Resource Management

- hosts: web roles:

- base - web - php - hosts: db roles:

- base - db

Because we are placing all of our roles in /etc/ansible/roles, it does not matter where our lamp.yml file resides on the controller. Once we execute this playbook, Ansible knows to automatically check the appropriate paths for the specified roles.

VMware Resource Management

Ansible recently introduced support for managing VMware vSphere virtual machines (VMs). As the Ansible community continues to develop new features for VMware

vSphere management, we may dedicate an entire chapter of a future edition of this book to Ansible’s VMware integrations. For now, we will introduce Ansible’s vsphere_guest

module and discuss its capabilities within this chapter.

The vsphere_guest module is one of Ansible’s core modules for provisioning cloud

workloads. Standalone ESXi is supported. However, we will focus on managing VMware vSphere workloads via VMware vCenter.

The module has a dependency on the open source project pysphere. So, the server that you select to run your VMware management tasks from needs to have pysphere installed via pip, a Python package installer, either manually (sudo pip install pyshphere) or by adding prerequisite tasks to your VMware management playbook to install pysphere for you (see Listing 11-11).

Listing 11-11 VMware Virtual Machine Creation

Click here to view code image - hosts: vmware tasks:

- name: Deploy pip for Ubuntu Servers apt: pkg=python-pip state=latest when: ansible_os_family == ‘Debian’

- name: Deploy pip for Red Hat Systems yum: pkg=python-pip state=latest when: ansible_os_family == ‘RedHat’

- name: Deploy the pysphere Python package pip: name=pysphere

- name: Deploy an Ubuntu VM on VMware vSphere vsphere_guest:

vcenter_hostname: vcenter.devwidgets.io username: [email protected]

password: devopsrocks123 guest: web server 01 state: powered_off vm_extra_config:

notes: This is a web server to run Apache in production vm_disk:

disk1:

size_gb: 10 type: thin

datastore: datastore1 vm_nic:

nic1:

type: vmxnet3

network: VM Network network_type: standard vm_hardware:

memory_mb: 512 num_cpus: 1

osid: ubuntu64Guest scsi: paravirtual esxi:

datacenter: Production

hostname: vsphere01.devwidgets.io

In my Ansible inventory file (see Chapter 10 for a refresher), I create a group of hosts called vmware that I specify in Listing 11-11 as the managed nodes to run the VMware management tasks (- hosts: vmware). You will need to limit the number of hosts that run the VMware commands so that vsphere_guest does not generate an error for creating duplicate VMs. This can either be done by placing a single managed node in the vmware group or by using the limit option with the ansible-playbook command:

Click here to view code image

ansible-playbook create-vm.yml —limit vmware[0]

The vmware group is treated like a list, and you specify a list index value in brackets after the group name. Python’s conventions for the list index will apply here. (For example, 0 would indicate the first host in the group, 1 would indicate the second managed node, –1 would indicate the last managed node in the group.)

The playbook in Listing 11-11 first satisfies the vsphere_guest prerequisites: Verify that pip and pysphere are installed. As we have done with previous Ansible code, we provide tasks for the different operating systems in our test environment (Debian and Red Hat-based systems).

Finally, we specify our vsphere_guest instructions so that we can create a VM using Ansible. There are multiple parameters that can be used during VM creation, but we narrowed down our sample code to the bare minimum that would be most useful:

vcenter_hostname: The VMware vCenter server that we will use to create the VM.

username: The user with sufficient privileges to create a VM.

password: The user’s vCenter password.

guest: The name of the VM being created. It must be unique.

state: Whether the VM should be powered on after creation.

vm_extra_config: Specify notes about the VM and set options such as vCPU and memory hot-add.

vm_disk: The VM’s storage location.

vm_nic: The VM network that the VM will be connected to.

vm_hardware: Specify characteristics such as the number of vCPUs, the amount of RAM, and the OS that the VM will run.

esxi: Specify the VMware vSphere data center and host that the VM will be created on.

In the vm_hardware parameter, the osid value for your desired operating system can be obtained from the VirtualMachineGuestOsIdentifier documentation in the VMware vSphere API Reference (see the “References” section at the end of this chapter). Table 11-1 shows a listing of a few popular operating systems and their corresponding

VirtualMachineGuestOsIdentifier values.

Table 11-1 VMware vSphere Operating System Identifiers Caution

If you select another operating system to use with the code in Listing 11-1, verify that the remainder of the VM’s parameters (for example,

vm_hardware) are compatible with it.

After running the playbook in Listing 11-1, the VM will be created and available in vCenter, as shown in Figure 11-1.

Figure 11-1 Ansible VMware guest deployment results

The vsphere_guest module will not install the VM’s operating system on your behalf.

However, you can gather facts about the VM, such as its MAC address. You can use this information with whichever OS deployment tool that your team uses. The system

administrator can obtain the VM’s facts by running the vsphere_guest module ad hoc and using the vmware_guest_facts parameter:

Click here to view code image

ansible vmware -m vsphere_guest -a “vcenter_hostname=vcenter.devwidgets.io [email protected] password=devopsrocks123 guest=‘new vm001’

vmware_guest_facts=yes”

VMs can be deleted by setting the state parameter to absent and setting the force parameter to yes just in case the VM is powered on (see Listing 11-12).

Listing 11-12 VMware Virtual Machine Deletion

Click here to view code image - hosts: vmware tasks:

- name: Deploy pip for Ubuntu Servers apt: pkg=python-pip state=latest when: ansible_os_family == ‘Debian’

- name: Deploy pip for Red Hat Systems yum: pkg=python-pip state=latest when: ansible_os_family == ‘RedHat’

- name: Deploy the pysphere Python package pip: name=pysphere

- name: Remove a specified VM vsphere_guest:

vcenter_hostname: vcenter.devwidgets.io

username: [email protected] password: devopsrocks123

guest: web server 01 state: absent

force: yes

The vsphere_guest module can also modify a VM’s properties, as shown in Listing 11-13.

Listing 11-13 VMware Virtual Machine Modification

Click here to view code image - hosts: vmware tasks:

- name: Deploy pip for Ubuntu Servers apt: pkg=python-pip state=latest when: ansible_os_family == ‘Debian’

- name: Deploy pip for Red Hat Systems yum: pkg=python-pip state=latest when: ansible_os_family == ‘RedHat’

- name: Deploy the pysphere Python package pip: name=pysphere

- name: Deploy an Ubuntu VM on VMware vSphere vsphere_guest:

vcenter_hostname: vcenter.devwidgets.io username: [email protected]

password: devopsrocks123 guest: web server 01 state: reconfigured vm_extra_config:

notes: This is an updated web server to run Apache in production vm_hardware:

memory_mb: 1024 num_cpus: 2 esxi:

datacenter: Production

hostname: vsphere01.devwidgets.io force: yes

The first change is to edit the VM state to the reconfigured value. If the change that I am making requires a VM shutdown and restart, I can include the force parameter to do this automatically. Finally, I update the memory (memory_mb), number of vCPUs

(num_cpus), and the VM’s description (notes) to the new values that I want the VM to have.

Summary

Ansible roles can help make application deployments easier. Your system deployment tasks are then a matter of calling the right roles with any necessary parameters. This chapter also discussed how the vsphere_guest Ansible module can be used to create, modify, delete, and obtain facts about VMware vSphere VMs. The next chapter takes a look at the desired state configuration (DSC) configuration management system that Microsoft introduced in PowerShell 4.0 for Windows servers.

References

[1] Ansible documentation: http://docs.ansible.com/

[2] VMware vSphere SDK:

https://www.vmware.com/support/developer/vsphere_mgmt_sdk/index.html

Part 5: PowerShell 4.0

Chapter 12. Introduction to PowerShell Desired State Configuration (DSC)

PowerShell has quickly grown to be the standard automation tool for both the Microsoft Windows and VMware ecosystems. Many VMware administrators have utilized

PowerCLI, VMware’s PowerShell snap-in, to perform tasks and collect information about their virtual environments. Microsoft professionals have seen tremendous investment by the company to make Microsoft Windows Server and Microsoft applications manageable with PowerShell.

The tools you have learned about so far in this book served as the precursor to Microsoft’s latest feature of PowerShell: Desired State Configuration. Commonly referred to with the acronym DSC, this feature enables Windows administrators to natively manage their environments with similar functionality to what is found with Puppet. This chapter covers the fundamentals of DSC and how it works.

Topics covered in this chapter include the following: