Automating CentOS 7 installation using Ansible and Kickstart

Automate your VMware Infrastructure using VMware modules

Abhijeet Kasurde
2 min readOct 6, 2020

Kickstart is a method for automated installation for CentOS/RHEL systems. It removes the burden of user for answering the repetitive answers for multiple installation and provides uniform installation throughout the infrastructure.

Photo by Ilya Pavlov on Unsplash

In VMware infrastructure, if you want to automate this process, you can use following scenario. This article does not cover how to create a kickstart file as there are multiple articles already available on the internet.

For this scenario, you will require -

  • HTTP server which can distribute kickstart files
  • Pre-configured Kickstart file
  • vCenter with atleast one ESXi
  • Ansible

You can install community.vmware modules from VMware collection using following command

# ansible-galaxy collection install community.vmware

Once you have Ansible and VMware modules ready, you can start by putting your kickstart file in your HTTP server. This will allow newly created virtual machine to download and automated installation process.

First we will create a normal blank virtual machine using vmware_guest module in poweredonstate and CDROM with CentOS 7 installation DVD.

- name: Boot VM with CentOS 7 installation iso in CDROM
vmware_guest:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: "{{ dc1 }}"
state: poweredon
cluster: Asia-Cluster1
disk:
- size_gb: 10
type: thin
datastore: ds_171_2
cdrom:
- controller_number: 0
unit_number: 0
state: present
type: iso
iso_path: "[ds_171_2] isos/CentOS-7.0-1406-x86_64-DVD.iso"
hardware:
memory_mb: 2048
num_cpus: 2
num_cpu_cores_per_socket: 2
name: "{{ hostname }}"
folder: /Asia-Datacenter1/vm/prod
guest_id: centos7_64Guest
networks:
- name: "privatepg"

This task will create and boot the virtual machine in powered on state. The installation will kick in since we have bootable DVD in CDROM. Now, we need to answer questions which are presented on the virtual machine’s screen. You can do this using vmware_guest_sendkey module.

- name: Handle boot parameter interactively
vmware_guest_sendkey:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ hostname }}"
keys_send:
- ESC
- ESC
string_send: 'vmlinuz initrd=initrd.img inst.ks=http://{{ httpd_ip }}:{{ httpd_port }}/{{ kickstart_file }}'
delegate_to: localhost
vars:
kickstart_file: kickstart.cfg
httpd_ip: 192.168.10.1
httpd_port: 9000

Here, the kickstart file called kicstart.cfg is hosted on HTTP server running on 192.168.10.1 port 9000 .

vmware_guest_sendkey module will send USB HID Scan codes to the given virtual machine and act as a virtual keyboard. After this you will need to press ENTER

- name: Start the process after providing boot parameter at installation prompt
vmware_guest_sendkey:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ hostname }}"
keys_send:
- ENTER
delegate_to: localhost

Once, this task is executed. Installation will start from the CDROM taking kickstart as answer file for automated installation. After this you can add a task to wait for the installation to complete with additional configurations related to virtual machine.

here is whole playbook looks like —

Thanks for reading and happy automating.

--

--