Automation with Ansible (10) - Create VMs from VCenter with Multi Environment Architecture
- Get link
- X
- Other Apps
Introduction
From this series, all folder structure will be followed from the previous Multi environment stucture.
The steps to create VMs are;
- Create Inventory file
- Create Variables
- Shared variables
- Group variables
- Create Ansible Playbook
- Creating VMs from CentOS Template
- Set Network interface
- Add MAC Address to dhcpd.conf
- Reboot
Reference
Folders/Files list
\---ansible
| CreateVMs.yaml
|
+---inventory
| +---shared_vars
| | shared-secrets.yml (empty)
| | shared-vars.yml (empty)
| |
| \---test
| +---dmz
| | | vm_hosts
| | |
| | \---group_vars
| | vcenter.yml
| |
| \---internal
| | vm_hosts
| |
| \---group_vars
| secrets.yml (empty)
| vcenter.yml
|
\---tasks
load-vars.yml
Create Inventory file
filename: vm_hosts
[docker-vms]
docker01 ipaddr="10.200.0.150"
docker02 ipaddr="10.200.0.151"
docker03 ipaddr="10.200.0.152"
docker04 ipaddr="10.200.0.153"
docker05 ipaddr="10.200.0.154"
Add all computers as many as you want to have
Create Variables
Loading Shared and Group Variables
Loading variables from Shared and Group. "{{ inventory_dir }}" is the directory specified from ansible-playbook command line.
---
- include_vars: "inventory/shared_vars/shared-secrets.yml"
- include_vars: "inventory/shared_vars/shared-vars.yml"
- include_vars: "{{ inventory_dir }}/group_vars/secrets.yml"
- include_vars: "{{ inventory_dir }}/group_vars/vcenter.yml"
- include_vars: "{{ inventory_dir }}/group_vars/docker.yml"
Shared Variables
There are no common data for this task
filename: inventory/shared_vars/shared-vars.yaml
Individual Variables
filename: inventory/{Environment}/{Internal or DMZ}/group_vars/vcenter.yaml
---
vcenter_hostname: 10.200.0.11
vcenter_validate_certs: no
vcenter_username: administrator@vsphere.local
vcenter_password: vcenter_password
vcenter_template: CentOS_Template
vcenter_cluster: VSphere_Cluster_Name
vcenter_resource: /Resources
vcenter_vm_folder: Docker
vcenter_vm_network: "Test-LAN"
Please check the Series 2 for the detail information.
Create Ansible Playbook
The connection will be local from ns01 node which provides following services
- DNS and DHCP
- Ansible playbook
- Docker Image build
- Maven and Java compile
---
- hosts: all
pre_tasks:
- include_tasks: tasks/load-vars.yml
connection: local
become: yes
tasks:
- name: createVM from Template
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
validate_certs: "{{ vcenter_validate_certs }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
guest: "{{ inventory_hostname }}"
from_template: yes
template_src: "{{ vcenter_template }}"
cluster: "{{ vcenter_cluster }}"
resource_pool: "{{ vcenter_resource }}"
vm_extra_config:
folder: "{{ vcenter_vm_folder }}"
- name: Reconfigre network card
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
validate_certs: "{{ vcenter_validate_certs }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
guest: "{{ inventory_hostname }}"
state: reconfigured
vm_nic:
nic1:
type: vmxnet3
network: "{{ vcenter_vm_network }}"
network_type: standard
esxi:
datacenter: FlairTest
- name: Retrieve Cloned VM
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
validate_certs: "{{ vcenter_validate_certs }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
guest: "{{ inventory_hostname }}"
vmware_guest_facts: yes
register: clonedvm
- name: Update DHCPD Entry
blockinfile:
path: /etc/dhcp/dhcpd.conf
marker: ""
state: present
insertafter: EOF
block: |
host {{ inventory_hostname }} {
option host-name "{{ inventory_hostname }}";
ddns-hostname "{{ inventory_hostname }}";
hardware ethernet {{ clonedvm.ansible_facts.hw_eth0.macaddress }};
fixed-address {{ hostvars[inventory_hostname]['ipaddr'] }};
}
- name: Restart DHCPD from ansible host
become: yes
shell: 'systemctl restart dhcpd'
async: 30
poll: 0
ignore_errors: true
- name: Boot Cloned VM
vsphere_guest:
vcenter_hostname: "{{ vcenter_hostname }}"
validate_certs: "{{ vcenter_validate_certs }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
guest: "{{ inventory_hostname }}"
state: powered_on
- debug:
var: clonedvm
Running command
For Internal Docker VMs
[ansible@ns01 ansible]$ ansible-playbook -i inventory/test/internal/vm_hosts Vsphere-CreateVM.yaml --extra-vars "ansible_sudo_pass=root_password" --ask-pass
For DMZ Docker VMs
[ansible@ns01 ansible]$ ansible-playbook -i inventory/test/dmz/vm_hosts Vsphere-CreateVM.yaml --extra-vars "ansible_sudo_pass=root" --ask-pass
"-i" is the option for the inventory. "{{ inventory_dir}}" will be the folder from this option. In this case, "{{ inventory_dir }}" will return "inventory/test/dmz".
"--ask-pass" is not necessary. This is to run command from remote connection with root permmission. However, for the consistency for the rest command, just used from the command
Result
Summary
Next tutorial will show you how to build docker cluster which created from this tutorial
- Get link
- X
- Other Apps
Comments
Post a Comment