{"id":69,"date":"2016-07-11T16:43:26","date_gmt":"2016-07-11T14:43:26","guid":{"rendered":"https:\/\/room28.it\/?p=69"},"modified":"2016-08-03T12:56:53","modified_gmt":"2016-08-03T10:56:53","slug":"use-vmware-perl-sdk-to-manage-a-vsphere-infrastructure","status":"publish","type":"post","link":"https:\/\/room28.it\/index.php\/2016\/07\/11\/use-vmware-perl-sdk-to-manage-a-vsphere-infrastructure\/","title":{"rendered":"Use VMware Perl SDK to manage a vSphere infrastructure"},"content":{"rendered":"<p>Hello,<\/p>\n<p>Here is my first API post on this blog. I used to post some API scripts on OVH forums and I want to use this media to share more of them and show you how easy it is to run scripts on top on VMware infrastructure. I had a chance to present VMware Perl SDK\u00a0and how it helps sysadmin on VMworld a couple of years ago.<\/p>\n<p>First of all, I&#8217;m going to show you how to connect to a vSphere infrastructure and then how to list all your virtual machines. This is a quick introduction but I think it is required to understand how it works.<\/p>\n<p>Thanks for reading and I hope it will be useful for your internal needs.<\/p>\n<h4>Overview<\/h4>\n<p>I run my scripts on top on a OVH Dedicated Cloud, which is basically a vSphere infrastructure. If you need anything about VMware APIs and SDKs, you can find everything\u00a0<a href=\"https:\/\/www.vmware.com\/support\/pubs\/sdk_pubs.html\">here<\/a>. One of the best argument on this Perl SDK is that it can run on Linux box. You can also find a python SDK, but it is less powerful than the perl one, there are a lot of call missing.<\/p>\n<p>All my scripts will require one connection to a vCenter, so you need to have a login and a password with the rights you need to perform all the operations you want. For this particular use case, &#8220;read only&#8221; rights are sufficient.<\/p>\n<p><!--more--><\/p>\n<h4>vSphere infrastructure<\/h4>\n<p>My infrastructure looks like this in my WebClient :<\/p>\n<p><img loading=\"lazy\" class=\"size-medium wp-image-72 aligncenter\" src=\"https:\/\/room28.it\/wp-content\/uploads\/2016\/07\/Capture-d\u2019\u00e9cran-2016-07-11-\u00e0-10.41.21-264x300.png\" alt=\"Capture d\u2019\u00e9cran 2016-07-11 \u00e0 10.41.21\" width=\"264\" height=\"300\" srcset=\"https:\/\/room28.it\/wp-content\/uploads\/2016\/07\/Capture-d\u2019\u00e9cran-2016-07-11-\u00e0-10.41.21-264x300.png 264w, https:\/\/room28.it\/wp-content\/uploads\/2016\/07\/Capture-d\u2019\u00e9cran-2016-07-11-\u00e0-10.41.21.png 433w\" sizes=\"(max-width: 264px) 85vw, 264px\" \/><\/p>\n<p>I have 1 datacenter with 2 clusters and several VMs in my inventory. The goal of our first script is just to list all my VMs on my &#8220;Prod&#8221; cluster.<\/p>\n<h4>Install perl SDK<\/h4>\n<p>You need a login on myvmware to be able to download Perl SDK (<a href=\"https:\/\/my.vmware.com\/group\/vmware\/info\/slug\/datacenter_cloud_infrastructure\/vmware_vsphere\/6_0#drivers_tools\">here is the direct link for vSphere 6.0<\/a>). To find it you need to go &#8220;All VMware product&#8221; and on vSphere download page, you have to go on the &#8220;Automation Tools and SDKs&#8221;. You&#8217;ll find a download link for the vSphere SDK for Perl&#8221;. Download the tar.gz archive.<\/p>\n<p>Untar it and copy paste the entire directory :<\/p>\n<pre class=\"bbcode_code\">vmware-vsphere-cli-distrib\/lib\/VMware\/share\/VMware<\/pre>\n<p>in you perl lib directory.<\/p>\n<blockquote><p><span style=\"font-size: 10pt;\">Hint : to known where you perl lib directory is located :<\/span><\/p>\n<pre>$ perl -e 'print \" @INC\\n\"'<\/pre>\n<p><span style=\"font-size: 10pt;\">So my perl lib directory is :<\/span><\/p>\n<pre><span style=\"font-size: 12pt;\">\/usr\/lib\/perl5<\/span><\/pre>\n<p class=\"bbcode_code\"><span style=\"font-size: 10pt;\">To install the VMware SDK for perl, I had to copy the content of the tar.gz I downloaded from myvmware website to this directory\u00a0:<\/span><\/p>\n<pre class=\"bbcode_code\">$ cp vmware-vsphere-cli-distrib\/lib\/VMware\/share\/VMware \/usr\/lib\/perl5\/<\/pre>\n<p><span style=\"font-size: 10pt;\">The content should look like this :<\/span><\/p>\n<pre>$ ls \/usr\/lib\/perl5\/VMware\/\r\nLookupService.pm VICommon.pm VIExt.pm VIM25Runtime.pm VIM2Runtime.pm VIMRuntime.pm pyexe SSOConnection.pm VICredStore.pm VILib.pm VIM25Stub.pm VIM2Stub.pm VIRuntime.pm<\/pre>\n<\/blockquote>\n<h4>Connect to vCenter<\/h4>\n<p>First of all, I need credentials to connect to my vCenter, all calls I&#8217;ll do by scripts will be done with this user.<\/p>\n<blockquote><p><span style=\"font-size: 10pt;\">Hint : create a specific user to use API calls, you&#8217;ll be able later to know where actions and connections come from in your vSphere client\u00a0Task\/Event tab.<\/span><\/p><\/blockquote>\n<p>To connect on vCenter, it requires 3 lines, a server, a username and a password :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Opts::set_option(&#039;server&#039;, &#039;pcc-178-33-102-10.ovh.com&#039;);\nOpts::set_option(&#039;username&#039;, &#039;apiuser&#039;);\nOpts::set_option(&#039;password&#039;, &#039;XXxxXXxxXX&#039;);<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p class=\"bbcode_code\">In my case, my vCenter is my Dedicated Cloud :\u00a0pcc-178-33-102-10.ovh.com. It could be an IP, your on premise vCenter, whatever. Just a vCenter.<\/p>\n<p class=\"bbcode_code\">Then, you just have to connect with this :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Util::connect();<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p class=\"bbcode_code\">In a perl script, it looks like :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">#!\/usr\/bin\/perl\nuse strict;\nuse Data::Dumper;\n\nuse VMware::VIRuntime;\n\nOpts::set_option(&#039;server&#039;, &#039;pcc-178-33-102-10.ovh.com&#039;);\nOpts::set_option(&#039;username&#039;, &#039;apiuser&#039;);\nOpts::set_option(&#039;password&#039;, &#039;XXxxXXxxXX&#039;);\n\n\nprint &quot;Connecting \\n&quot;; \n\nUtil::connect(); \n\n## Later, we are going to make actions there\n\nUtil::disconnect(); \nprint &quot;Disconnected \\n&quot;;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>Don&#8217;t forget to disconnect at the end \ud83d\ude09<\/p>\n<h4>Listing all VMs in my Prod cluster<\/h4>\n<p>From there, we can connect to a vCenter, we just need to add on our first script, a few lines to get our VMs.<\/p>\n<h5>Getting an object view in a vCenter<\/h5>\n<p>First, you need to know how to find an object in a VMware inventory. You will need to use the &#8220;find_entity_views&#8221; call :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">my $DatacenterViews = Vim::find_entity_views(\n    &#039;view_type&#039; =&gt; &#039;Datacenter&#039;\n);\n!$DatacenterViews and die(&#039;Failed to get DatacenterViews&#039;);<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>With this call you will list all the datacenter you have in your inventory. In a perl script :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">#!\/usr\/bin\/perl\nuse strict;\nuse Data::Dumper;\n\nuse VMware::VIRuntime;\n\nOpts::set_option(&#039;server&#039;, &#039;pcc-178-33-102-10.ovh.com&#039;);\nOpts::set_option(&#039;username&#039;, &#039;apiuser&#039;);\nOpts::set_option(&#039;password&#039;, &#039;XXxxXXxxXX&#039;);\n\n\nprint &quot;Connecting \\n&quot;; \n\nUtil::connect(); \n\n\nmy $DatacenterViews = Vim::find_entity_views(\n    &#039;view_type&#039; =&gt; &#039;Datacenter&#039;\n);\n!$DatacenterViews and die(&#039;Failed to get DatacenterViews&#039;);\n\nprint Dumper $DatacenterViews;\n\nUtil::disconnect(); \nprint &quot;Disconnected \\n&quot;;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>You will dump all Datacenters with all their properties.<\/p>\n<p>To get only one specific object, you can use the &#8220;find_entity_view&#8221; call with a filter. In my case, I work on the Datacenter name in my Dedicated Cloud, which is &#8220;pcc-178-33-102-10_datacenter114&#8221;.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">my $DatacenterView = Vim::find_entity_view(\n    &#039;view_type&#039; =&gt; &#039;Datacenter&#039;,\n    &#039;filter&#039; =&gt; {\n        &#039;name&#039; =&gt; &#039;pcc-178-33-102-10_datacenter114&#039;\n    } \n);\n!$DatacenterView and die(&#039;Failed to get DatacenterView&#039;);<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h4>Getting all VMs in my &#8220;Prod&#8221; cluster<\/h4>\n<p>Now we understand how filters and search work. We can work on finding all VMs in my cluster.<\/p>\n<p>We are going to use the &#8220;find_entity_views&#8221; call and add a filter based on my cluster name.<\/p>\n<p>First we need to get the view of my &#8220;Prod&#8221; cluster with the filter we already know.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">my $ClusterView\u00a0 = Vim::find_entity_view(\n    &#039;view_type&#039; =&gt; &#039;ClusterComputeResource&#039;, \n    &#039;filter&#039;    =&gt; { &#039;name&#039; =&gt; &#039;Prod&#039; } \n);<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>I introduce a new property of the &#8220;find_entity_views&#8221; method : <em>begin_entity<\/em>.<\/p>\n<p>If you remember my inventory,\u00a0all my VMs are located under my cluster &#8220;Prod&#8221; :<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-72\" src=\"https:\/\/room28.it\/wp-content\/uploads\/2016\/07\/Capture-d\u2019\u00e9cran-2016-07-11-\u00e0-10.41.21-264x300.png\" alt=\"Capture d\u2019\u00e9cran 2016-07-11 \u00e0 10.41.21\" width=\"264\" height=\"300\" srcset=\"https:\/\/room28.it\/wp-content\/uploads\/2016\/07\/Capture-d\u2019\u00e9cran-2016-07-11-\u00e0-10.41.21-264x300.png 264w, https:\/\/room28.it\/wp-content\/uploads\/2016\/07\/Capture-d\u2019\u00e9cran-2016-07-11-\u00e0-10.41.21.png 433w\" sizes=\"(max-width: 264px) 85vw, 264px\" \/><\/p>\n<p>So I&#8217;ll ask to &#8220;find_entity_views&#8221; to search all VMs under my cluster :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">my $VMViews\u00a0 = Vim::find_entity_views(\n    &#039;view_type&#039;\u00a0 \u00a0 =&gt; &#039;VirtualMachine&#039;,\n    &#039;begin_entity&#039; =&gt; $ClusterView ,\n);<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>Here we are. Here is the full perl script :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-bash code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">#!\/usr\/bin\/perl\nuse strict;\nuse Data::Dumper;\n\nuse VMware::VIRuntime;\n\nOpts::set_option(&#039;server&#039;, &#039;pcc-178-33-102-10.ovh.com&#039;);\nOpts::set_option(&#039;username&#039;, &#039;apiuser&#039;);\nOpts::set_option(&#039;password&#039;, &#039;XXxxXXxxXX&#039;);\n\n\nprint &quot;Connecting \\n&quot;; \n\nUtil::connect();\n\nmy $ClusterView\u00a0 = Vim::find_entity_view(\n    &#039;view_type&#039; =&gt; &#039;ClusterComputeResource&#039;, \n    &#039;filter&#039;    =&gt; { &#039;name&#039; =&gt; &#039;Prod&#039; } \n);\n!$ClusterView and die (&quot;Failed to get Cluster&quot;);\n\nmy $VMViews\u00a0 = Vim::find_entity_views(\n    &#039;view_type&#039;\u00a0 \u00a0 =&gt; &#039;VirtualMachine&#039;,\n    &#039;begin_entity&#039; =&gt; $ClusterView,\n);\n!$VMViews and die (&quot;Failed to get VMs&quot;);\n\nforeach my $VM (@$VMViews)\n{\n    print $VM-&gt;name . &quot;\\n&quot;;\n}\n\n\nUtil::disconnect(); \nprint &quot;Disconnected \\n&quot;;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>Easy isn&#8217;t it ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, Here is my first API post on this blog. I used to post some API scripts on OVH forums and I want to use this media to share more of them and show you how easy it is to run scripts on top on VMware infrastructure. I had a chance to present VMware Perl &hellip; <a href=\"https:\/\/room28.it\/index.php\/2016\/07\/11\/use-vmware-perl-sdk-to-manage-a-vsphere-infrastructure\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Use VMware Perl SDK to manage a vSphere infrastructure&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,7,18],"tags":[],"_links":{"self":[{"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/posts\/69"}],"collection":[{"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":16,"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":144,"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions\/144"}],"wp:attachment":[{"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/room28.it\/index.php\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}