Pages

Wednesday, January 19, 2022

Manage Vmware Machines With Ansible

Here we are looking on how to use Ansible to connect to vCenters and manage VMs. The focus will be on using quick playbooks for cloning and deleting a current template VM.

We can see the playbook for the creation process for cloning and deleting VMs. The playbook creation process will involve setting up variables, tasks, and handlers, and will be broken down into simple, easy-to-follow steps. Sample playbooks will be provided as a starting point for managing VMs in vCenter using Ansible.

Clone a VM in Vcenter with Ansible Playbook

- hosts: localhost
gather_facts: no
vars:
vcenter_server: "xxx.xxx.xxx.xxx"
vcenter_user: "User-Name"
vcenter_pass: "Password"
datacenter_name: "DC-Name"
cluster_name: "xxx.xxx.xxx.xxx"
resources_pool: "RG-Name"
template_vm: "Template-Name"
serv:
- vm1
- vm2

tasks:
- name: Clone the template
vmware_guest:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
resource_pool: "rahul-test"
validate_certs: False
name: "{{ item }}"
template: "{{ template_vm }}"
datacenter: "{{ datacenter_name }}"
folder: /{{ datacenter_name }}/vm
state: poweredon
wait_for_ip_address: yes
register: "r"
with_items: "{{ serv }}"
- debug :
msg: "This is the Host-IPs: {{ item.instance.ipv4 }} {{ item.instance.hw_name }}"
with_items: "{{ r.results}}"
loop_control:
label: "{{ item.instance.hw_name }}"




Shutdown a VM in Vcenter with Ansible Playbook

- hosts: localhost
gather_facts: no
vars:
vcenter_server: "xxx.xxx.xxx.xxx"
vcenter_user: "User-Name"
vcenter_pass: "Password"
datacenter_name: "DC-Name"
cluster_name: "xxx.xxx.xxx.xxx"
tasks:
- name: Shutdown the template
vmware_guest:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
validate_certs: False
name: "{{ item}}"
datacenter: "{{ datacenter_name }}"
folder: /{{ datacenter_name }}/vm
state: poweredoff
wait_for_ip_address: yes
loop:
- vm1
- vm2


Delete a VM in Vcenter with Ansible Playbook

- hosts: localhost
gather_facts: no
vars:
vcenter_server: "xxx.xxx.xxx.xxx"
vcenter_user: "User-Name"
vcenter_pass: "Password"
datacenter_name: "DC-Name"
cluster_name: "xxx.xxx.xxx.xxx"
tasks:
- name: Delete the template
vmware_guest:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
validate_certs: False
name: "{{ item}}"
datacenter: "{{ datacenter_name }}"
folder: /{{ datacenter_name }}/vm
state: absent
wait_for_ip_address: yes
loop:
- vm1
- vm2



Friday, March 12, 2021

Ansible for Vcenter Module not found error

When working with vCenter and Ansible, you may encounter an error that says "vmware_guest Module not found." This error occurs when the vmware_guest module is not installed on your system.

To resolve this error, you need to install the community.vmware collection, which contains the vmware_guest module. You can do this by running the following command:

To install it use: ansible-galaxy collection install community.vmware.

This command will download and install the community.vmware collection, which contains the vmware_guest module. Once installed, you should be able to use the module in your Ansible playbooks without encountering any errors.




Tuesday, March 9, 2021

Test WinRm connection to Windows Env

To check if WinRM (Windows Remote Management) is enabled in a Windows Server, you can use the following commands:

Test-NetConnection -ComputerName XX -Port 5986

This command tests the connection to port 5986, which is the default port used by WinRM over HTTPS.

Test-NetConnection -ComputerName XX -Port 5985

This command tests the connection to port 5985, which is the default port used by WinRM over HTTP.

If the above commands return successful results, it means that WinRM is enabled and running on the specified server.

Additionally, you can also use the following command to check if WinRM is configured properly on a remote server:

Test-WSMan -ComputerName XX

This command tests the Windows Remote Management (WinRM) service on a remote server and returns the current configuration status of WinRM.