Pages

Showing posts with label vmware. Show all posts
Showing posts with label vmware. Show all posts

Thursday, April 13, 2023

Troubleshooting Ansible and VMware: Resolving 'Failed to import PyVmomi library' Error

When working with Ansible and VMware, you may encounter an error message similar to the following:


msg: Failed to import the required Python library (PyVmomi) on <hostname>'s Python <path/to/python>. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter.

This error message indicates that Ansible is unable to import the required Python library PyVmomi when running a playbook. This library is required to interact with VMware virtualization products.

To resolve this issue, you should first ensure that the PyVmomi library is installed in the appropriate location. You can use pip to install the library:

pip install pyvmomi

Next, you should verify that Ansible is using the correct version of Python and pip. The error message may indicate that Ansible is using the wrong Python interpreter or version of pip. You can check which version of Python Ansible is using by running the ansible --version command and looking for the ansible_python_interpreter line in the output. If the interpreter is incorrect, you can set the correct interpreter path using the ansible_python_interpreter variable in your playbook or in your inventory file.
It's also important to ensure that you are using the correct version of pip to install packages. If you have multiple versions of Python installed, you may also have multiple versions of pip. You can check the version of pip you are using with the pip --version command. If you are using the wrong version of pip, you can switch to the correct version using the appropriate command for your operating system.

If the issue persists, you may need to uninstall any conflicting libraries or packages, such as pyvim, and try reinstalling PyVmomi to ensure that there are no conflicts or version mismatches.

By following these steps, you can resolve the Failed to import the required Python library error message and ensure that Ansible is using the correct Python interpreter and version of pip for installing packages.

Wednesday, April 12, 2023

Generalizing ubuntu for vmware

When you clone a virtual machine in VMware, the new machine is an exact copy of the original machine, including the network settings. This means that the new machine will have the same IP address, MAC address, and other network settings as the original machine. This can cause network conflicts and other issues, especially if you are running multiple clones of the same machine on the same network.
    
To avoid this issue, you need to ensure that each clone of the machine has a unique network configuration. One way to do this is to delete the machine-id file, which is a unique identifier for the machine. When the machine boots up, it generates a new machine-id based on its hardware configuration, which will result in a unique network configuration.

The command rm -rf /var/log/* removes all logs from the /var/log directory, which can help to free up disk space and reduce clutter. However, it is important to note that this command will permanently delete all log files, which can make troubleshooting more difficult if there are issues with the system.

To delete the value in the machine-id file, you can use the following command:

echo "" > /etc/machine-id

** Don't rm -rf the machine-id file, the system might get stuck at the start. 

This will clear the value in the file, effectively resetting the machine ID and generating a new ID on boot.

In addition to deleting the machine-id file, you may also want to clear the SSH keys and other sensitive information from the virtual machine. This can help to ensure that each clone of the machine is unique and secure.


Thursday, January 20, 2022

Kubernetes(k8s) With Ansible Over Ubuntu Machines with Docker

Kubernetes(k8s) is a popular container orchestration system that provides a powerful platform for managing containerized applications. Docker is a lightweight, yet powerful container runtime that provides the underlying infrastructure for many Kubernetes deployments. In this, we can see how to set up Kubernetes with Docker using Ansible over Ubuntu machines.

Environment

  • Ubuntu VM's running on Vmware
  • K8s with Docker Runtime
** Important Notice: Following Settings are not working since the new release of 1.27. Please make use of the deployment with Containers.

https://www.adminz.in/2022/01/kubernetes-with-containerd-using-ansible.html

User Creation

  • Asks for the User Name which has to be created
  • Create's the user
  • Adds a dedicated Sudo entry 
  • Setting up Password less sudo for user
  • Copy the local uses ssh key to server for password less auth
  • Print the details
  • Updates the System

- hosts: all
become: yes

vars_prompt:
- name: "new_user"
prompt: "Account need to be create in remote server."
private: no

tasks:
- name: creating the user {{ new_user }}.
user:
name: "{{ new_user }}"
createhome: yes
shell: /bin/bash
append: yes
state: present

- name: Create a dedicated sudo entry file for the user.
file:
path: "/etc/sudoers.d/{{ new_user }}"
state: touch
mode: '0600'
- name: "Setting up Sudo without Password for user {{ new_user }}."
lineinfile:
dest: "/etc/sudoers.d/{{ new_user }}"
line: '{{ new_user }} ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'

- name: Set authorized key for user copying it from current {{ new_user }} user.
authorized_key:
user: "{{ new_user }}"
state: present
key: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"

- name: Print the created user.
shell: id "{{ new_user }}"
register: new_user_created
- debug:
msg: "{{ new_user_created.stdout_lines[0] }}"

- name: "Update cache & Full system update"
apt:
update_cache: true
upgrade: dist
cache_valid_time: 3600
force_apt_get: true


Package Installation in Master and Worker Nodes

  • Copy the local host files to all the server for name resolution
  • update the hostnames of the machines based on the names in host file
  • Temporary Swap off
  • Swap off in fstab
  • Installing Kubernetes Pre-requisites packages
  • Adding Docker Packages Keys
  • Adding Docker Respository
  • Install Docker packages
  • Enables Docker Services
  • Add Google repositories keys
  • Create Directory for Docker deamon file
  • Create the docker deamon file with Overlay details
  • Restart Docker Services
  • Install Kubernetes Packages
  • Enabled K8s Services
  • Reboot the Servers

- hosts: "master, workers"
remote_user: ansible
become: yes
become_method: sudo
become_user: root
gather_facts: yes
connection: ssh
tasks:
- name: Copying the host file
copy:
src: /etc/hosts
dest: /etc/hosts
owner: root
group: root

- name: "Updating hostnames"
hostname:
name: "{{ new_hostname }}"

- name: Make the Swap inactive
command: swapoff -a

- name: Remove Swap entry from /etc/fstab.
lineinfile:
dest: /etc/fstab
regexp: swap
state: absent

- name: Installing Prerequisites for Kubernetes
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- vim
- software-properties-common
state: present

- name: Add Docker’s official GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add Docker Repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
state: present
filename: docker
mode: 0600

- name: Install Docker Engine.
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
state: present

- name: Enable service docker, and enable persistently
service:
name: docker
enabled: yes

- name: Add Google official GPG key
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present


- name: Creates directory
file:
path: /etc/docker/
state: directory

- name: Creating a file with content
copy:
dest: "/etc/docker/daemon.json"
content: |
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}

- name: restart docker
service:
name: docker
state: restarted
enabled: yes

- name: Add Kubernetes Repository
apt_repository:
repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
state: present
filename: kubernetes
mode: 0600

- name: Installing Kubernetes Cluster Packages.
apt:
name:
- kubeadm
- kubectl
- kubelet
state: present

- name: Enable service kubelet, and enable persistently
service:
name: kubelet
enabled: yes

- name: Reboot all the kubernetes nodes.
reboot:
msg: "Reboot initiated by Ansible"
connect_timeout: 5
reboot_timeout: 3600
pre_reboot_delay: 0
post_reboot_delay: 30
test_command: whoami





Master Configuration

  • Pulls all needed images
  • Reset Kubeadm if its already configured
  • Initialize K8s cluster
  • Create Directory for Kube config file in master
  • Create a local kube config file in master
  • Copy the kube config file to ansible local server
  • Genarates the Kube toke for workers and stores it
  • Copy the token to master's tmp directory
  • Copy the toke to ansible local tmp direcotry
  • Initialize the pod network with fannel
  • Copy the output to mater file
  • Copy the output to ansible local server


- hosts: master
remote_user: ansible
become: yes
become_method: sudo
become_user: root
gather_facts: yes
connection: ssh
tasks:

- name: Pulling images required for setting up a Kubernetes cluster
shell: kubeadm config images pull

- name: Resetting kubeadm
shell: kubeadm reset -f
register: output

- name: Initializing Kubernetes cluster
shell: kubeadm init --apiserver-advertise-address=$(ip a |grep ens160| grep 'inet ' | awk '{print $2}' | cut -f1 -d'/') --pod-network-cidr 10.244.0.0/16 --v=5
register: myshell_output

- debug: msg="{{ myshell_output.stdout }}"

- name: Create .kube to home directory of master server
file:
path: $HOME/.kube
state: directory
mode: 0755

- name: Copy admin.conf to user's kube config to master server
copy:
src: /etc/kubernetes/admin.conf
dest: $HOME/.kube/config
remote_src: yes

- name: Copy admin.conf to user's kube config to ansible local server
become: yes
become_method: sudo
become_user: root
fetch:
src: /etc/kubernetes/admin.conf
dest: /Users/rahulraj/.kube/config
flat: yes
- name: Get the token for joining the nodes with Kuberentes master.
shell: kubeadm token create --print-join-command
register: kubernetes_join_command
- debug:
msg: "{{ kubernetes_join_command.stdout_lines }}"

- name: Copy K8s Join command to file in master
copy:
content: "{{ kubernetes_join_command.stdout_lines[0] }}"
dest: "/tmp/kubernetes_join_command"

- name: Copy join command from master to local ansible server
fetch:
src: "/tmp/kubernetes_join_command"
dest: "/tmp/kubernetes_join_command"
flat: yes

- name: Install Pod network
shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
register: myshell_output

- name: Copy the output to master file
copy:
content: "{{ myshell_output.stdout }}"
dest: "/tmp/pod_network_setup.txt"

- name: Copy network output from master to local ansible server
fetch:
src: "/tmp/pod_network_setup.txt"
dest: "/tmp/pod_network_setup.txt"
flat: yes


Worker Configuration

  • Copy the token from ansible local file to worker nodes
  • Reset the kubeadm 
  • Join the Worker node to Master by running the command

- hosts: workers
remote_user: ansible
become: yes
become_method: sudo
become_user: root
gather_facts: yes
connection: ssh
tasks:

- name: Copy token to worker nodes.
become: yes
become_method: sudo
become_user: root
copy:
src: /tmp/kubernetes_join_command
dest: /tmp/kubernetes_join_command
mode: 0777
- name: Resetting kubeadm
shell: kubeadm reset -f
register: output

- name: Join the Worker nodes with the master.
become: yes
become_method: sudo
become_user: root
command: sh /tmp/kubernetes_join_command
register: joined_or_not
- debug:
msg: "{{ joined_or_not.stdout }}"


K8s should be up with the worker nodes now. 


Saturday, October 11, 2014

CentOS 7 network install on VMWare Workstation network problem


If we install Centos  7 on Vmware workstation 10 , you can see that the network is not detected. Its because that the v7 3.10 kernel no longer supports the Ethernet Controller device. So once we install the Centos 7Os in Vmware workstation there will not be Network inside the Vm.

I found at https://access.redhat.com/discussions/722093 that its a Bug and can be fixed by editing the vmx file of Vmware Workstation.

I added the following line to my .vmx file:

CODE: SELECT ALL
ethernet0.virtualDev = "e1000"

Tuesday, September 16, 2014

Adding License for Vmware Esxi

Following Command allow us to add the Vmware license through the ssh access into the Esxi Server.



vim-cmd vimsvc/license --set *********************