Ansible. Proxmox. Create VM Almalinux 8 from template using Ansible Playbook
Links
https://docs.ansible.com/ansible/2.9/modules/proxmox_kvm_module.html
https://forum.proxmox.com/threads/provision-vm-from-template-using-ansible.130596/
https://otus.ru/nest/post/910/
https://github.com/arkalira/Deploying-VMs-to-Proxmox-using-cloud-init/blob/master/README.md
Configuring
PVE server
Install utils
apt install pip
pip3 install proxmoxer
sudo apt install libguestfs-tools -y
Create and move to dir
mkdir /root/cloud-init
cd /root/cloud-init
Download Almalinux 8 cloud-init image
wget https://repo.almalinux.org/almalinux/8/cloud/x86_64/images/AlmaLinux-8-GenericCloud-latest.x86_64.qcow2 -O almalinux8.qcow2
Set nano as default editor
EDITOR=nano; export EDITOR
Edit configurations in the al8-ci image
virt-edit -a almalinux8.qcow2 /etc/cloud/cloud.cfg
... ssh_pwauth: 1 ...
virt-edit -a almalinux8.qcow2 /etc/ssh/sshd_config
... Comment HostKey(s) Comment SyslogFacility AUTHPRIV Comment AuthorizedKeysFile UnComment PubkeyAuthentication yes Comment GSSAPIAuthentication yes Comment GSSAPICleanupCredentials no ...
virt-edit -a almalinux8.qcow2 /etc/selinux/config
... SELINUX=permissive ...
!!! Following action is not necessary. !!! Do it only if you're really sure you want to set a specific DNS into your template
virt-edit -a almalinux8.qcow2 /etc/resolv.conf
... nameserver 192.168.17.8 ...
virt-customize -a almalinux8.qcow2 --timezone "Europe/Moscow"
virt-customize -a almalinux8.qcow2 --update
virt-customize -a almalinux8.qcow2 --install qemu-guest-agent,nano,wget
virt-customize -a almalinux8.qcow2 --run-command 'sudo systemctl enable qemu-guest-agent'
Set root password
read -sp "Password for root: " pwd && virt-customize -a almalinux8.qcow2 --root-password password:$pwd
PassWord
Create VM for future template
qm create 1001 --name "al8-ci-tmplt-01" --memory 1024 --cores 1 --net0 virtio,bridge=vmbr0
Import disk
qm importdisk 1001 almalinux8.qcow2 ceph-rbd01
Add imported disk
qm set 1001 --scsihw virtio-scsi-pci --scsi0 ceph-rbd01:vm-1001-disk-0
Set boot disk
qm set 1001 --boot c --bootdisk scsi0
Add cloud-init image into the VM
qm set 1001 --ide2 ceph-rbd01:cloudinit
Set agent
qm set 1001 --agent 1
Convert VM to template
qm template 1001
Ansible server
dnf install -y ansible
Create root Ansible project dir
mkdir ./pve_create_vm
cd ./pve_create_vm
Add necessary project files
tree
. ├── ansible.cfg ├── vm_create.yml ├── inventory │ └── hosts.ini └── vars.yml
mkdir ./inventory
nano inventory/hosts.ini
[pve]
pve01 ansible_port=22 ansible_host=192.168.17.251 ansible_host_ip=192.168.17.251
[pve:vars]
ansible_user=root
ansible_ssh_pass=PassWord
nano ansible.cfg
[defaults]
inventory = inventory/hosts.ini
host_key_checking = False
stdout_callback = yaml
callback_enabled = timer
nano vars.yml
pve_api_host: 192.168.17.251
pve_api_user: root@pam
pve_api_password: PassWord
pve_node: pve01
vm_template: al8-ci-tmplt-01
#key_name: id_rsa.pub
vm_new_name: al8-autodeploy-01
vm_new_ip: 192.168.17.222/24,gw=192.168.17.8
vm_new_dns: 192.168.17.8
vm_new_id: 200
vm_new_cores: 2
vm_new_sockets: 1
vm_new_memory: 1024
nano vm_create.yml
---
- name: Initial setup VM
hosts: pve01
vars_files:
- vars.yml
tasks:
- name: Clone VM
tags:
- clone-vm
proxmox_kvm:
timeout: 3600
node: "{{ pve_node }}"
name: "{{ vm_new_name }}"
newid: "{{ vm_new_id }}"
api_user: "{{ pve_api_user }}"
api_password: "{{ pve_api_password }}"
api_host: "{{ pve_api_host }}"
clone: "{{ vm_template }}"
full: yes
- name: Set IP address
tags:
- set-ip-address
command: "qm set {{ vm_new_id }} --ipconfig0 ip={{ vm_new_ip }}"
- name: Set DNS
tags:
- set-dns
command: "qm set {{ vm_new_id }} --nameserver {{ vm_new_dns }}"
- name: Update VM
tags:
- update-vm
proxmox_kvm:
api_host: "{{ pve_api_host }}"
api_user: "{{ pve_api_user }}"
api_password: "{{ pve_api_password }}"
cores: "{{ vm_new_cores }}"
sockets: "{{ vm_new_sockets }}"
memory: "{{ vm_new_memory }}"
update: yes
vmid: "{{ vm_new_id }}"
node: "{{ pve_node }}"
name: "{{ vm_new_name }}"
- name: Start VM
tags:
- start-vm
proxmox_kvm:
api_host: "{{ pve_api_host }}"
api_password: "{{ pve_api_password }}"
api_user: "{{ pve_api_user }}"
vmid: "{{ vm_new_id }}"
node: "{{ pve_node }}"
state: started
Edit vars
nano inventory/hosts.ini
nano vars.ini
Run VM creation
ansible-playbook vm_create.yml
After that you will get the VM with corresponding parameters