Ansible Vsphere
Introduction
Here we are creating the new machine in Vsphere which is stored in a template by using ansible.
Pre-request
Before creating a playbook directly to provision Vsphere we need to follow some pre requesting steps that are needed for this process,
Install Python-pip and PyVmomi,
Host details to be entered as,
Then, Generate SSH Key,
Copy the SSH Key to the local host,
Edit the ssh_config file for the authentication,
And make the changes as follows,
The centos_test machine which in the template having the static IP 192.168.1.55 and here we are going the use that template.
Playbook
Create the directory and the file,
delete_instance.yml: This file is used to delete the machine if there any machine in the name testvm
---
- hosts: localhost
gather_facts: no
tasks:
- name: Delete the template
vmware_guest:
hostname:
username:
password:
validate_certs: False
name: testvm
state: absent
force: yes
new_instance.yml: This is used to create the new machine clone from the template
---
- name: Create a VM from a template
hosts: localhost
gather_facts: no
tasks:
- name: Clone the template
vmware_guest:
hostname:
username:
password:
validate_certs: False
name: testvm
template: centos_test
datacenter: Datacenter
folder: /Datacenter/vm/
state: poweredon
esxi_hostname: 192.168.1.210
hardware:
memory_mb: 3048
num_cpus: 2
scsi: paravirtual
disk:
- size_gb: 50
type: thin
datastore: datastore1
- pause:
seconds: 10
ssh.yml: This file is used to sent the ssh copy to the new machine created.
---
- name: Create a VM from a template
hosts: localhost
gather_facts: no
tasks:
- name: transfer ssh-key
shell: sshpass -p "
httpd.yml: This file is used to install the apache server in the new machine.
---
- name: Create an apache
hosts: centos
gather_facts: no
tasks:
- name: Allow Network Ports in Firewalld
firewalld:
port: 80/tcp
state: enabled
permanent: yes
immediate: yes
- name: install apache server
yum:
name: httpd
state: latest
- name: Starting and Enabling the required services
service:
name: httpd
state: started
enabled: yes
Setting_up.yml
---
- import_playbook: playbook/delete_instance.yml
- import_playbook: playbook/new_instance.yml
- import_playbook: playbook/ssh.yml
- import_playbook: playbook/httpd.yml
run the yaml file,
output
After the execution of the yaml file in the sphere, the new machine gets created with the installation of the apache server.
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post