{"id":3963,"date":"2023-11-04T12:24:30","date_gmt":"2023-11-04T12:24:30","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=3963"},"modified":"2023-11-04T12:24:30","modified_gmt":"2023-11-04T12:24:30","slug":"vmware-ansible-getting-started-examples","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=3963","title":{"rendered":"VMware Ansible &#8211; Getting Started Examples"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Infrastructure as Code (IaC) is managing infrastructure using a DevOps type methodology. Where the current configuration of your infrastructure is represented in code, any additions, changes or deletions are also made through this code. There are various methods to codify your infrastructure, some proprietary like AWS Cloudformation, others are generic like Ansible or Terraform.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The introduction below just gives some basic examples of how you can use Ansible with the VMware Community module to manage your infrastructure. It is recommended to deploy a test vCenter and test ESXi host and play around, rather than going straight to your production machines. Also be aware that Ansible should be idempotent, there are however certain aspects of configuration where there is a need to make changes in an order, which when re-run can cause actions to be taken again, although there are ways to resolve these within Ansible, something you should test and be aware of.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing the Environment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;m assuming you already have grounding in Ansible, but in this case we&#8217;ll setup Ansible add the various dependencies and then add the VMware Community modules on your Developer machine. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python -m venv venv\r\n$ source venv\/bin\/activate\r\n$ pip install --upgrade pip\r\n\nInstall Ansible as per: https:\/\/docs.ansible.com\/ansible\/latest\/installation_guide\/intro_installation.html\r\n\n$ ansible-galaxy collection install community.vmware\r\n$ pip install requests\r\n$ pip install pyVim\r\n$ pip install pyvmomi\r<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You may also want to install the vSphere Automation SDK, this can be helpful and may be required by some modules.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip install --upgrade git+https:\/\/github.com\/vmware\/vsphere-automation-sdk-python.git<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Full documentation of the VMware Community module can be found <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/community\/vmware\/index.html\">https:\/\/docs.ansible.com\/ansible\/latest\/collections\/community\/vmware\/index.html<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Foreword<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can structure your Ansible however you wish, there are various ways to build the structure and add roles etc. In this example, we&#8217;ll keep it very simple essentially the structure will consist of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>vmware.yml<\/li>\n\n\n\n<li>dc_dc1.yml<\/li>\n\n\n\n<li>cluster_cluster1.yml<\/li>\n\n\n\n<li>dvswitch_dvswitch1.yml<\/li>\n\n\n\n<li>host-host1.yml<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It will also be simple in that the credentials will be requested from the user at the point of running the Ansible playbook, there are various methods to manage and store credentials that are beyond the scope of this document, in this case the credentials are stored in &#8220;facts&#8221; to allow them to be reused across the plays.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The other thing to bear in mind about the VMware Community module is that it runs as &#8220;localhost&#8221;, i.e. you&#8217;re not connecting to the ESXi host to configure it, its essentially a wrapper around the VMware API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The example is also fairly rough, there are many ways to ensure that the Ansible play is as reusable as possible, by use of variables so there is more that can be done to streamline it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Main File (vmware.yml)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Site.yml all playbooks included here\r\n\r\n  - hosts: localhost\r\n    become: false\r\n    pre_tasks:\r\n      - name: Check Ansible Version\r\n        assert:\r\n          that:\r\n            - ansible_version.major == 2\r\n            - ( ansible_version.minor == 7 or ansible_version.minor == 9)\r\n          msg: \"Our Ansible requires Ansible 2.7 or 2.9\"\r\n    vars_prompt:\r\n      - name: \"vcenter_hostname\"\r\n        prompt: \"vCenter FQDN\"\r\n        private: no\r\n        default: \"vcenter1.domain.com\"\r\n      - name: \"vcenter_username\"\r\n        prompt: \"vCenter Username\"\r\n        private: no\r\n        default: \"administrator@vsphere.local\"\r\n      - name: \"vcenter_password\"\r\n        prompt: \"vCenter Password\"\r\n        default: \"&lt;password>\"\r\n        private: yes\r\n      - name: \"esxi_password\"\r\n        prompt: \"ESXi Password\"\r\n        default: \"&lt;password>\"\r\n        private: yes\r\n    tasks:\r\n      - name: \"Set captured variables\"\r\n        set_fact:\r\n          vcenter_hostname: \"{{ vcenter_hostname }}\"\r\n          vcenter_username: \"{{ vcenter_username }}\"\r\n          vcenter_password: \"{{ vcenter_password }}\"\r\n          esxi_password: \"{{ esxi_password }}\"\r\n\r\n# Datacenter Configuration\r\n   - import_playbook: dc_dc1.yml\r\n\r\n# # Cluster Configuration\r\n   - import_playbook: cluster_cluster1.yml\r\r\n\r\n# # Distributed Switch Configuration\r\n  - import_playbook: dvswitch_dvswitch1.yml \r\n\r\n# Host Configuration\r\n  - import_playbook: host_host1.yml<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Datacentre (dc_dc1.yml)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>---\r\n  - name: \"DC1 Configuration\"\r\n    hosts: localhost\r\n    gather_facts: false\r\n\r\n    # Configuration is per the default unless explicitly defined.\r\n\r\n    vars:\r\n      - datacenter_name: \"DC1\"\r\n      - validate_certs: \"false\"\r\n  \r\n    tasks:    \r\n      - name: Create Datacenter\r\n        community.vmware.vmware_datacenter:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          datacenter_name: '{{ datacenter_name }}'\r\n          validate_certs: '{{ validate_certs }}'\r\n          # Specific Configuration\r\n          state: present\r\n        delegate_to: localhost<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Cluster 1 (cluster_cluster1.yml)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>---\r\n  - name: \"Cluster1 Configuration\"\r\n    hosts: localhost\r\n    gather_facts: false\r\n\r\n    # Configuration is per the default unless explicitly defined.\r\n\r\n    vars:\r\n      - datacenter_name: \"DC1\"\r\n      - cluster_name: \"Cluster1\"\r\n      - validate_certs: \"false\"\r\n\r\n    tasks:\r\n        - name: Create Cluster\r\n          community.vmware.vmware_cluster:\r\n            hostname: '{{ vcenter_hostname }}'\r\n            username: '{{ vcenter_username }}'\r\n            password: '{{ vcenter_password }}'\r\n            validate_certs: '{{ validate_certs }}'\r\n            # Specific Configuration\r\n            datacenter_name: '{{ datacenter_name }}'\r\n            cluster_name: '{{ cluster_name }}'\r\n          delegate_to: localhost\r\n\r\n        - name: \"DRS Configuration\"\r\n          community.vmware.vmware_cluster_drs:\r\n            hostname: '{{ vcenter_hostname }}'\r\n            username: '{{ vcenter_username }}'\r\n            password: '{{ vcenter_password }}'\r\n            datacenter_name: '{{ datacenter_name }}'\r\n            cluster_name: '{{ cluster_name }}'\r\n            validate_certs: '{{ validate_certs }}'\r\n            # Specific Configuration\r\n            drs_default_vm_behavior: \"fullyAutomated\"\r\n            enable: true\r\n            drs_vmotion_rate: 3\r\n            predictive_drs: false\r\n          delegate_to: localhost\r\n          register: drs_msg\r\n\r\n        - name: \"HA Configuration\"\r\n          community.vmware.vmware_cluster_ha:\r\n            hostname: '{{ vcenter_hostname }}'\r\n            username: '{{ vcenter_username }}'\r\n            password: '{{ vcenter_password }}'\r\n            datacenter_name: '{{ datacenter_name }}'\r\n            cluster_name: '{{ cluster_name }}'\r\n            validate_certs: '{{ validate_certs }}'\r\n            # Specific Configuration\r\n            enable: true\r\n          delegate_to: localhost\r\n          register: ha_msg\r\n\r\n        - name: \"EVC Mode Configuration\"\r\n          community.vmware.vmware_evc_mode:\r\n            hostname: '{{ vcenter_hostname }}'\r\n            username: '{{ vcenter_username }}'\r\n            password: '{{ vcenter_password }}'\r\n            datacenter_name: '{{ datacenter_name }}'\r\n            cluster_name: '{{ cluster_name }}'\r\n            validate_certs: '{{ validate_certs }}'\r\n            # Specific Configuration\r\n            evc_mode: \"intel-sandybridge\"\r\n            state: present\r\n          delegate_to: localhost\r\n          register: evc_msg\r\n\r\n        # Debug\r\n        # - debug:\r\n        #     var: drs_msg\r\n        # - debug:\r\n        #     var: ha_msg\r\n        # - debug:\r\n        #     var: evc_msg.msg\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Distributed vSwitch (dvswitch_dvswitch1.yml)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>---\r\n  - name: \"dvSwitch1 Configuration\"\r\n    hosts: localhost\r\n    gather_facts: false\r\n\r\n    # Configuration is per the default unless explicitly defined.\r\n\r\n    vars:\r\n      - datacenter_name: \"DC1\"\r\n      - validate_certs: \"false\"\r\n    \r\n    tasks:\r\n      - name: Create dvSwitch\r\n        community.vmware.vmware_dvswitch:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          datacenter: '{{ datacenter_name }}'\r\n          validate_certs: '{{ validate_certs }}'\r\n          # Specific Configuration\r\n          switch: dvSwitch1\r\n          version: 7.0.3\r\n          mtu: 1500\r\n          uplink_quantity: 2\r\n          uplink_prefix: 'Uplink'\r\n          discovery_protocol: lldp\r\n          discovery_operation: both\r\n          state: present\r\n        delegate_to: localhost\r\n      \r\n      - name: Create Management Network Port Group\r\n        community.vmware.vmware_dvs_portgroup:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          validate_certs: '{{ validate_certs }}'\r\n          # Specific Configuration\r\n          portgroup_name: \"Management Network\"\r\n          switch_name: \"dvSwitch1\"\r\n          vlan_id: 0\r\n          num_ports: 120\r\n          port_binding: static\r\n          state: present\r\n        delegate_to: localhost\r\n      \r\n      - name: Create vmotion Port Group\r\n        community.vmware.vmware_dvs_portgroup:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          validate_certs: '{{ validate_certs }}'\r\n          # Specific Configuration\r\n          portgroup_name: \"vmotion\"\r\n          switch_name: \"dvSwitch1\"\r\n          vlan_id: 3806\r\n          num_ports: 120\r\n          port_binding: static\r\n          state: present\r\n        delegate_to: localhost<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">ESXi Host (host_host1.yml)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The VMware ESXi Host configuration is substantial, the example below assumes you have an ESXi host that has been built, but is currently standalone (i.e. not joined to vCenter), it is also has basic network configuration in place, so it can be reached across the network.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The play below, will join the ESXi host to the vCenter, place it in Cluster 1, perform some basic configuration such as DNS and NTP, then add the host to the Distributed vSwitch, prepare the VMKernel ports, add two NICs for iSCSI, setup a IQN and add an iSCSI target. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Essentially, a whole configuration of a VMware ESXi host, so you can then put it into production. In my case I needed the host to connect to a HPE Nimble Storage array, and this has some recommended iSCSI timeout tweaks, although these can be applied via VMware Host Profiles, in this example, i&#8217;m showing you how you could punch the settings into the host via ESXCLI. Why? you might ask, this is because the VMware Community Ansible module does not yet have functionality to do this within the Ansible itself.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\r\n  - name: \"host1 Host Configuration\"\r\n    hosts: localhost\r\n    gather_facts: true\r\n\r\n    vars:\r\n      - datacenter_name: \"DC1\"\r\n      - cluster_name: \"Cluster1\"\r\n      - validate_certs: \"false\"\r\n      - host_name: \"host1\"\r\n      - esxi_hostname: \"host1.domain.com\"\r\n\r\n    tasks:\r\n      - name: \"Join ESXi Host to vCenter\"\r\n        community.vmware.vmware_host:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          datacenter: \"{{ datacenter_name }}\"\r\n          cluster: \"{{ cluster_name }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          esxi_username: \"root\"\r\n          esxi_password: \"{{ esxi_password }}\"\r\n          state: present\r\n        delegate_to: localhost\r\n      \r\n      - name: \"DNS Configuration\"\r\n        community.vmware.vmware_host_dns:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          host_name: \"{{ host_name }}\"\r\n          type: static\r\n          domain: domain.com\r\n          dns_servers:\r\n            - 192.168.100.10\r\n            - 192.168.100.11\r\n          search_domains:\r\n            - domain.com\r\n            - domain2.com\r\n        delegate_to: localhost\r\n      \r\n      - name: \"NTP Configuration\"\r\n        community.vmware.vmware_host_ntp:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          # Specific Configuration\r\n          state: present\r\n          ntp_servers:\r\n              - ntp0.domain.com\r\n              - ntp1.domain.com\r\n        delegate_to: localhost\r\n      \r\n      - name: Start ntpd setting for an ESXi Host\r\n        community.vmware.vmware_host_service_manager:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          esxi_hostname: '{{ esxi_hostname }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          service_name: ntpd\r\n          state: present\r\n        delegate_to: localhost\r\n\r\n      # Apply VMware Licence Key\r\n      #\r\n      - name: Add ESXi license and assign to the ESXi host\r\n        community.vmware.vcenter_license:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          esxi_hostname: '{{ esxi_hostname }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          license: &lt;KEY HERE>\r\n          state: present\r\n        delegate_to: localhost\r\n\r\n      # Network Configuration\r\n      # An ordered workflow of stanzas. Assumes the host's management VMKernel is attached to a VSS with two uplinks.\r\n      # Ordered workflow detaches one vmnic1 and attaches it to the DVS, then migrates the VMkernel port from the VSS to DVS,\r\n      # then removes the VSS, and finally moves the other pNIC.\r\n      # It is not indempotent, because running the playbook for a second time removes a vmnic from the DVS and readds it.\r\n\r\n      - name: Add Host to dvSwitch - Step 1 (Add vmnic1)\r\n        community.vmware.vmware_dvs_host:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          esxi_hostname: '{{ esxi_hostname }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          switch_name: dvSwitch1\r\n          vmnics:\r\n              - vmnic1\r\n          state: present\r\n        delegate_to: localhost\r\n\r\n      - name: Add Host to dvSwitch - Step 2 (Migrate vmkernel)\r\n        community.vmware.vmware_vmkernel:\r\n            hostname: '{{ vcenter_hostname }}'\r\n            username: '{{ vcenter_username }}'\r\n            password: '{{ vcenter_password }}'\r\n            esxi_hostname: '{{ esxi_hostname }}'\r\n            validate_certs: \"{{ validate_certs }}\"\r\n            # Specific Configuration\r\n            dvswitch_name: \"dvSwitch1\"\r\n            portgroup_name: \"Management Network\"\r\n            network:\r\n              type: 'static'\r\n              ip_address: 192.168.0.27\r\n              subnet_mask: 255.255.255.0\r\n            state: present\r\n            enable_mgmt: true\r\n        delegate_to: localhost\r\n      \r\n      - name: Add Host to dvSwitch - Step 3 (Remove Standard Switch)\r\n        community.vmware.vmware_vswitch:\r\n          hostname: '{{ esxi_hostname }}'\r\n          username: \"root\"\r\n          password: '{{ esxi_password }}'\r\n          esxi_hostname: '{{ esxi_hostname }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          switch_name: \"vSwitch0\"\r\n          state: absent\r\n        delegate_to: localhost\r\n      \r\n      - name: Add Host to dvSwitch - Step 4 (Add vmnic0)\r\n        community.vmware.vmware_dvs_host:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          esxi_hostname: '{{ esxi_hostname }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          switch_name: dvSwitch1\r\n          vmnics:\r\n              - vmnic0\r\n              - vmnic1\r\n          state: present\r\n        delegate_to: localhost\r\n\r\n      - name: Update the TCP\/IP stack configuration of the default\r\n        community.vmware.vmware_host_tcpip_stacks:\r\n          hostname: \"{{ vcenter_hostname }}\"\r\n          username: \"{{ vcenter_username }}\"\r\n          password: \"{{ vcenter_password }}\"\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          default:\r\n            hostname: \"{{ host_name }}\"\r\n            domain: domain.com\r\n            preferred_dns: 192.168.100.10\r\n            alternate_dns: 192.168.100.11\r\n            search_domains:\r\n              - domain.com\r\n              - domain2.com\r\n            gateway: 192.168.0.1\r\n            #congestion_algorithm: cubic\r\n            #max_num_connections: 12000\r\n          \r\n      - name: Add vMotion vmkernel port with vMotion TCP\/IP stack\r\n        community.vmware.vmware_vmkernel:\r\n            hostname: '{{ vcenter_hostname }}'\r\n            username: '{{ vcenter_username }}'\r\n            password: '{{ vcenter_password }}'\r\n            esxi_hostname: '{{ esxi_hostname }}'\r\n            validate_certs: \"{{ validate_certs }}\"\r\n            # Specific Configuration\r\n            dvswitch_name: \"dvSwitch1\"\r\n            portgroup_name: \"vmotion\"\r\n            network:\r\n              type: 'static'\r\n              ip_address: 192.168.1.27\r\n              subnet_mask: 255.255.255.0\r\n              tcpip_stack: vmotion\r\n            state: present\r\n        delegate_to: localhost\r\n\r\n      - name: Update the TCP\/IP stack configuration of the vmotion TCP\/IP Stack\r\n        community.vmware.vmware_host_tcpip_stacks:\r\n          hostname: \"{{ vcenter_hostname }}\"\r\n          username: \"{{ vcenter_username }}\"\r\n          password: \"{{ vcenter_password }}\"\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          vmotion:\r\n            gateway: 192.168.1.1\r\n\r\n      # ISCSI Configuration\r\n\r\n      - name: Add iSCSI vSwitch 1\r\n        community.vmware.vmware_vswitch:\r\n          hostname: '{{ esxi_hostname }}'\r\n          username: \"root\"\r\n          password: '{{ esxi_password }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          switch: \"vSwitch1\"\r\n          nics:\r\n          - vmnic2\r\n        delegate_to: localhost\r\n\r\n      - name: Add iSCSI 1 Port Group\r\n        community.vmware.vmware_portgroup:\r\n          hostname: \"{{ esxi_hostname }}\"\r\n          username: \"root\"\r\n          password: \"{{ esxi_password }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          hosts: &#91;\"{{ esxi_hostname }}\"]\r\n          switch: \"vSwitch1\"\r\n          portgroup: \"iSCSI 1\"\r\n          vlan_id: 0\r\n        delegate_to: localhost\r\n\r\n      - name: Add iSCSI 1 VMKernel Port\r\n        community.vmware.vmware_vmkernel:\r\n            hostname: '{{ esxi_hostname }}'\r\n            username: \"root\"\r\n            password: '{{ esxi_password }}'\r\n            esxi_hostname: '{{ esxi_hostname }}'\r\n            validate_certs: \"{{ validate_certs }}\"\r\n            # Specific Configuration\r\n            vswitch_name: \"vSwitch1\"\r\n            portgroup_name: \"iSCSI 1\"\r\n            network:\r\n              type: 'static'\r\n              ip_address: 192.168.10.27\r\n              subnet_mask: 255.255.255.0\r\n              tcpip_stack: default\r\n            state: present\r\n        delegate_to: localhost\r\n\r\n      - name: Add iSCSI vSwitch 2\r\n        community.vmware.vmware_vswitch:\r\n          hostname: '{{ esxi_hostname }}'\r\n          username: \"root\"\r\n          password: '{{ esxi_password }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          switch: \"vSwitch2\"\r\n          nics:\r\n          - vmnic3\r\n        delegate_to: localhost\r\n\r\n      - name: Add iSCSI 2 Port Group\r\n        community.vmware.vmware_portgroup:\r\n          hostname: \"{{ esxi_hostname }}\"\r\n          username: \"root\"\r\n          password: \"{{ esxi_password }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          hosts: &#91;\"{{ esxi_hostname }}\"]\r\n          switch: \"vSwitch2\"\r\n          portgroup: \"iSCSI 2\"\r\n          vlan_id: 0\r\n        delegate_to: localhost\r\n\r\n      - name: Add iSCSI 2 VMKernel Port\r\n        community.vmware.vmware_vmkernel:\r\n            hostname: '{{ esxi_hostname }}'\r\n            username: \"root\"\r\n            password: '{{ esxi_password }}'\r\n            esxi_hostname: '{{ esxi_hostname }}'\r\n            validate_certs: \"{{ validate_certs }}\"\r\n            # Specific Configuration\r\n            vswitch_name: \"vSwitch2\"\r\n            portgroup_name: \"iSCSI 2\"\r\n            network:\r\n              type: 'static'\r\n              ip_address: 192.168.10.227\r\n              subnet_mask: 255.255.255.0\r\n              tcpip_stack: default\r\n            state: present\r\n        delegate_to: localhost\r\n\r\n      - name: Enable iSCSI of ESXi\r\n        community.vmware.vmware_host_iscsi:\r\n          hostname: \"{{ vcenter_hostname }}\"\r\n          username: \"{{ vcenter_username }}\"\r\n          password: \"{{ vcenter_password }}\"\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          state: enabled\r\n        delegate_to: localhost\r\n      \r\n      - name: Add VMKernels to iSCSI config of ESXi\r\n        community.vmware.vmware_host_iscsi:\r\n          hostname: \"{{ vcenter_hostname }}\"\r\n          username: \"{{ vcenter_username }}\"\r\n          password: \"{{ vcenter_password }}\"\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          iscsi_config:\r\n            vmhba_name: \"vmhba65\"\r\n            port_bind:\r\n              - vmk2\r\n              - vmk3\r\n          state: present\r\n        delegate_to: localhost\r\n\r\n      - name: Add a dynamic target to iSCSI config of ESXi\r\n        community.vmware.vmware_host_iscsi:\r\n          hostname: \"{{ vcenter_hostname }}\"\r\n          username: \"{{ vcenter_username }}\"\r\n          password: \"{{ vcenter_password }}\"\r\n          esxi_hostname: \"{{ esxi_hostname }}\"\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          iscsi_config:\r\n            vmhba_name: vmhba65\r\n            send_target:\r\n              address: 192.168.10.50\r\n          state: present\r\n\r\n      # Configure Local Storage\r\n      # Requires the \"vSphere Automation SDK for Python\" to be installed.\r\n      - name: Rename a datastore\r\n        community.vmware.vmware_object_rename:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          new_name: \"host1:datastore1\"\r\n          object_name: datastore1\r\n          object_type: Datastore\r\n        delegate_to: localhost\r\n\r\n      # iSCSI Advanced Configuration\r\n      # Ansible modules don't exist for this (yet), so we have to perform via SSH.\r\n\r\n      - name: Start ntpd setting for an ESXi Host\r\n        community.vmware.vmware_host_service_manager:\r\n          hostname: '{{ vcenter_hostname }}'\r\n          username: '{{ vcenter_username }}'\r\n          password: '{{ vcenter_password }}'\r\n          esxi_hostname: '{{ esxi_hostname }}'\r\n          validate_certs: \"{{ validate_certs }}\"\r\n          # Specific Configuration\r\n          service_name: ntpd\r\n          state: present\r\n        delegate_to: localhost\r\n\r\n      \r\n\r\n      - name: \"Advanced Configuration - iSCSI LoginTimeout\"\r\n        shell: esxcli iscsi adapter param set -A vmhba65 -k \"LoginTimeout\" -v \"30\"\r\n        register: result\r\n        delegate_to: \"{{ esxi_hostname }}\"\r\n      \r\n      - name: \"Advanced Configuration - iSCSI NoopOutInterval\"\r\n        shell: esxcli iscsi adapter param set -A vmhba65 -k \"NoopOutInterval\" -v \"30\"\r\n        register: result\r\n        delegate_to: \"{{ esxi_hostname }}\"\r\n\r\n      - name: \"Advanced Configuration - iSCSI NoopOutTimeout\"\r\n        shell: esxcli iscsi adapter param set -A vmhba65 -k \"NoopOutTimeout\" -v \"30\"\r\n        register: result\r\n        delegate_to: \"{{ esxi_hostname }}\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Running the Playbook<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You may also want to create a hosts file e.g.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;esxiservers]\r\nhost1.internal.sanger.ac.uk<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To run the play you can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible-playbook vmware.yml -i hosts -u root -k\r<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You are then prompted for the vCenter hostname, the vCenter Username, vCenter Password and finally the SSH password for the host. Once collected the playbook configures the host as per the instructions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hopefully this has been a useful albeit brief introduction into Ansible for VMware. As you can see it is very adaptable and can be used to manage the Virtual Machines as well (although beyond the scope of this article).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/unsplash.com\/@adigold1\" data-type=\"link\" data-id=\"https:\/\/unsplash.com\/@adigold1\">Image Attribution<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Infrastructure as Code (IaC) is managing infrastructure using a DevOps type methodology. Where the current configuration of your infrastructure is represented in code, any additions, changes or deletions are also made through this code. There are various methods to codify your infrastructure, some proprietary like AWS Cloudformation, others are generic like Ansible or Terraform. The &#8230; <a title=\"VMware Ansible &#8211; Getting Started Examples\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=3963\" aria-label=\"Read more about VMware Ansible &#8211; Getting Started Examples\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3535,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,42,25],"tags":[],"class_list":["post-3963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","category-devops","category-vmware"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3963"}],"version-history":[{"count":2,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3963\/revisions"}],"predecessor-version":[{"id":3965,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3963\/revisions\/3965"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/3535"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}