Last active
July 21, 2021 18:15
-
-
Save tchellomello/be20734327fc6be922b806a6265b1f68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ansible-playbook -i deploy-vm.yml -e vm_name=test | |
--- | |
- name: Deploy VM on Libvirt | |
hosts: localhost | |
vars: | |
qemu_connection: "qemu:///system" | |
vm_domain: tatu.lab | |
vm_memory: 1024 | |
vm_network: default | |
libvirt_dir: /var/lib/libvirt/images | |
staging_dir: /tmp | |
image_name: "cloud-image.iso" | |
image_url: "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2" | |
image_checksum: "sha256:3510fc7deb3e1939dbf3fe6f65a02ab1efcc763480bc352e4c06eca2e4f7c2a2" | |
tasks: | |
- name: Install deps | |
become: true | |
dnf: | |
name: | |
- virt-install | |
- qemu-img | |
- name: Download Cloud Image ISO | |
get_url: | |
url: "{{ image_url }}" | |
dest: "{{ staging_dir }}/{{ image_name }}" | |
checksum: "{{ image_checksum }}" | |
- name: Render meta-data template | |
template: | |
src: templates/cloud-vm-meta-data.j2 | |
dest: "{{ staging_dir }}/{{ vm_name }}-meta-data" | |
- name: Render user-data template | |
template: | |
src: templates/cloud-vm-user-data.j2 | |
dest: "{{ staging_dir }}/{{ vm_name }}-user-data" | |
- name: Create {{ vm_name }} qcow2 file | |
become: true | |
shell: qemu-img create -f qcow2 -o backing_file={{ staging_dir }}/{{ image_name }} -F qcow2 {{ libvirt_dir }}/{{ vm_name }}.qcow2 | |
- name: Generate {{ vm_name }} boot ISO | |
shell: genisoimage -o {{ staging_dir }}/{{ vm_name }}_config.iso -V cidata -r -J {{ staging_dir }}/{{ vm_name }}-user-data {{ staging_dir }}/{{ vm_name }}-meta-data | |
- name: Create {{ vm_start }} | |
shell: virt-install --connect {{ qemu_connection }} -n {{ vm_name }} -r {{ vm_memory }} -w network={{ vm_network }} --import --disk path={{ libvirt_dir }}/{{ vm_name }}.qcow2 --disk path={{ staging_dir }}/{{ vm_name }}_config.iso,device=cdrom --nographics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instance-id: {{ vm_name }} | |
hostname: {{ vm_name }} | |
local-hostname: {{ vm_name }} | |
dsmode: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cloud-config | |
preserve_hostname: False | |
hostname: {{ vm_name }} | |
fqdn: {{vm_name}}.{{ vm_domain }} | |
package_upgrade: true | |
disable_root: false | |
packages: | |
- vim-enhanced | |
- git | |
- net-tools | |
- wget | |
# Remove cloud-init when finished with it | |
#runcmd: | |
#- [ yum, -y, remove, cloud-init ] | |
#- [userdel, centos] | |
# Update /etc/motd | |
write_files: | |
- content: | | |
IP address: \4 | |
path: /etc/issue | |
append: true | |
# Configure where output will go | |
output: | |
all: ">> /var/log/cloud-init.log" | |
# configure interaction with ssh server | |
ssh_svcname: ssh | |
ssh_deletekeys: True | |
ssh_genkeytypes: ['rsa', 'ecdsa'] | |
users: | |
- default | |
- name: foo | |
shell: /bin/bash | |
passwd: $6$l1CK47VrjvO54CvV$I2NxEC68mDzs4P5dYcsJlmCQDumWFUB/Ik4DD67rqzFBToYswmQIS2CYnDQFn.bJP7ePHGa8igEElV7QdOgU21 | |
sudo: ALL=(ALL) NOPASSWD:ALL | |
groups: users, wheel | |
ssh_authorized_keys: | |
- ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqHnJBxfuSqiRkNcmakVyjkAHZF9BgcQVePOM48vXzJzrOUqrw/aeTQnnuupc/ibo2O3T1R0Wf3KzDM+0TA2PbwGwiuqSl01vemm94wRuvlleQ7zNqkMiDdkwDKnkJiEWwMxjqsctXdtJlSlW+SivQMPuXBUBrbQgYNQXgtiNQwfOhzSX1RezKFzBd4YX98ZMxjNiyPQ6LKF8skkKy8HJ1DQiYFeV4zuvRsqIgl0MPG2dD+BlGEASFs+3YfJWb+ir1l15tuzwF2/SzzR/9vrHTmCdcWuWD3quteSOQS7HW5MwHboLKGJCcDaVfUuUxaxvpCsFdJEAvxbDQl4tRWn4Pw== [email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment