set -eu
if [ $# -eq 0 ]; then
- echo "Usage: $0 vmname [vmname ...]" >&2
+ echo "Usage: $0 hostname [hostname ...]" >&2
exit 1
fi
cd "$(dirname "$0")"
-for vmname in "$@"; do
- ansible-playbook -e vmname="${vmname}" remove_known_hosts.yml
+for hostname in "$@"; do
+ ansible-playbook -e hostname="${hostname}" remove_known_hosts.yml
done
hosts: localhost
gather_facts: no
tasks:
- - command: ssh-keygen -f ~/.ssh/known_hosts -R [{{ item }}]:{{ hostvars[vmname].ansible_port }}
+ - command: ssh-keygen -f ~/.ssh/known_hosts -R [{{ item }}]:{{ hostvars[hostname].ansible_port }}
with_items:
- - "{{ hostvars[vmname].ansible_host }}"
- - r3-{{ vmname }}
- - r3g-{{ vmname }}
- - r3e-{{ vmname }}
- - "{{ hostvars[vmname].vm_network_cooked.primary.ip }}"
+ - "{{ hostvars[hostname].ansible_host }}"
+ - r3-{{ hostname }}
+ - r3g-{{ hostname }}
+ - r3e-{{ hostname }}
+ - "{{ hostvars[hostname].vm_network_cooked.primary.ip }}"
d-i netcfg/disable_dhcp boolean true
d-i netcfg/choose_interface select enp1s1
d-i netcfg/disable_autoconfig boolean false
-d-i netcfg/get_ipaddress string {{ hostvars[vmname].vm_network_cooked.primary.ip }}
-d-i netcfg/get_netmask string {{ hostvars[vmname].vm_network_cooked.primary.mask }}
-d-i netcfg/get_gateway string {{ hostvars[vmname].vm_network_cooked.primary.gateway }}
-d-i netcfg/get_nameservers string {{ hostvars[vmname].vm_network_cooked.nameservers | join(' ') }}
+d-i netcfg/get_ipaddress string {{ hostvars[hostname].vm_network_cooked.primary.ip }}
+d-i netcfg/get_netmask string {{ hostvars[hostname].vm_network_cooked.primary.mask }}
+d-i netcfg/get_gateway string {{ hostvars[hostname].vm_network_cooked.primary.gateway }}
+d-i netcfg/get_nameservers string {{ hostvars[hostname].vm_network_cooked.nameservers | join(' ') }}
d-i netcfg/confirm_static boolean true
-d-i netcfg/get_hostname string {{ vmname }}
-d-i netcfg/get_domain string {{ hostvars[vmname].vm_network_cooked.domain }}
+d-i netcfg/get_hostname string {{ hostname }}
+d-i netcfg/get_domain string {{ hostvars[hostname].vm_network_cooked.domain }}
d-i netcfg/wireless_wep string
d-i clock-setup/ntp boolean false
-d-i partman-auto/disk string /dev/{{ hostvars[vmname].vm_install_cooked.disks.primary }}
+d-i partman-auto/disk string /dev/{{ hostvars[hostname].vm_install_cooked.disks.primary }}
d-i partman-auto/method string lvm
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
boot-root :: \
1000 10000 -1 ext4 \
$defaultignore{ } $primary{ } $bootable{ } \
- method{ lvm } vg_name{ {{ vmname }} } \
+ method{ lvm } vg_name{ {{ hostname }} } \
. \
2048 10000 2560 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ / } \
. \
1024 11000 1280 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /var } \
. \
768 10000 768 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /var/log } \
options/noexec{ noexec } \
. \
16 20000 -1 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method( keep } lv_name{ dummy } \
.
d-i pkgsel/upgrade select safe-upgrade
popularity-contest popularity-contest/participate boolean false
-d-i grub-installer/choose_bootdev string /dev/{{ hostvars[vmname].vm_install_cooked.disks.primary }}
+d-i grub-installer/choose_bootdev string /dev/{{ hostvars[hostname].vm_install_cooked.disks.primary }}
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean false
d-i preseed/late_command string \
- lvremove -f {{ vmname }}/dummy; \
+ lvremove -f {{ hostname }}/dummy; \
in-target bash -c "apt-get update -q && apt-get full-upgrade -y -q"; \
in-target bash -c "passwd -d root && passwd -l root"; \
mkdir -p -m 0700 /target/root/.ssh; \
cp /authorized_keys /target/root/.ssh/; \
-{% if hostvars[vmname].ansible_port is defined %}
- in-target bash -c "sed -e 's/^\(\s*#*\s*Port.*\)/Port {{ hostvars[vmname].ansible_port }}/' -i /etc/ssh/sshd_config"
+{% if hostvars[hostname].ansible_port is defined %}
+ in-target bash -c "sed -e 's/^\(\s*#*\s*Port.*\)/Port {{ hostvars[hostname].ansible_port }}/' -i /etc/ssh/sshd_config"
{% endif %}
#d-i netcfg/choose_interface select enp1s1
#d-i netcfg/disable_autoconfig boolean false
-#d-i netcfg/get_ipaddress string {{ hostvars[vmname].vm_network_cooked.primary.ip }}
-#d-i netcfg/get_netmask string {{ hostvars[vmname].vm_network_cooked.primary.mask }}
-#d-i netcfg/get_gateway string {{ hostvars[vmname].vm_network_cooked.primary.gateway }}
-#d-i netcfg/get_nameservers string {{ hostvars[vmname].vm_network_cooked.nameservers | join(' ') }}
+#d-i netcfg/get_ipaddress string {{ hostvars[hostname].vm_network_cooked.primary.ip }}
+#d-i netcfg/get_netmask string {{ hostvars[hostname].vm_network_cooked.primary.mask }}
+#d-i netcfg/get_gateway string {{ hostvars[hostname].vm_network_cooked.primary.gateway }}
+#d-i netcfg/get_nameservers string {{ hostvars[hostname].vm_network_cooked.nameservers | join(' ') }}
#d-i netcfg/confirm_static boolean true
-d-i netcfg/get_hostname string {{ vmname }}
-d-i netcfg/get_domain string {{ hostvars[vmname].vm_network_cooked.domain }}
+d-i netcfg/get_hostname string {{ hostname }}
+d-i netcfg/get_domain string {{ hostvars[hostname].vm_network_cooked.domain }}
d-i netcfg/wireless_wep string
d-i clock-setup/ntp boolean false
-d-i partman-auto/disk string /dev/{{ hostvars[vmname].vm_install_cooked.disks.primary }}
+d-i partman-auto/disk string /dev/{{ hostvars[hostname].vm_install_cooked.disks.primary }}
d-i partman-auto/method string lvm
d-i partman-auto/purge_lvm_from_device boolean true
-d-i partman-auto-lvm/new_vg_name string {{ vmname }}
+d-i partman-auto-lvm/new_vg_name string {{ hostname }}
d-i partman-auto-lvm/guided_size string max
d-i partman-lvm/device_remove_lvm boolean true
boot-root :: \
1000 10000 -1 ext4 \
$defaultignore{ } $primary{ } $bootable{ } \
- method{ lvm } vg_name{ {{ vmname }} } \
+ method{ lvm } vg_name{ {{ hostname }} } \
. \
2048 10000 2560 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ / } \
. \
1024 11000 1280 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /var } \
. \
768 10000 768 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /var/log } \
options/noexec{ noexec } \
. \
16 20000 -1 ext4 \
- $lvmok{ } in_vg{ {{ vmname }} } \
+ $lvmok{ } in_vg{ {{ hostname }} } \
method( keep } lv_name{ dummy } \
.
popularity-contest popularity-contest/participate boolean false
d-i pkgsel/update-policy select none
-d-i grub-installer/choose_bootdev string /dev/{{ hostvars[vmname].vm_install_cooked.disks.primary }}
+d-i grub-installer/choose_bootdev string /dev/{{ hostvars[hostname].vm_install_cooked.disks.primary }}
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean false
d-i preseed/late_command string \
- lvremove -f {{ vmname }}/dummy; \
+ lvremove -f {{ hostname }}/dummy; \
in-target bash -c "apt-get update -q && apt-get full-upgrade -y -q"; \
in-target bash -c "passwd -d root; passwd -l root; umask 077; mkdir -p /root/.ssh/; echo -e '{{ noc_ssh_keys | join('\\n') }}' > /root/.ssh/authorized_keys"; \
in-target bash -c "sed -e 's/^\(\s*#*\s*Port.*\)/Port 22000/' -i /etc/ssh/sshd_config"
---
dependencies:
- role: preseed
- hostname: "{{ vmname }}"
+ hostname: "{{ hostname }}"
# TODO: Find a way to eliminate the duplicate defaults.
- ssh_keys: "{{ hostvars[vmname].ssh_keys | default(noc_ssh_keys) }}"
+ ssh_keys: "{{ hostvars[hostname].ssh_keys | default(noc_ssh_keys) }}"
---
- name: create disks for vm
- with_dict: "{{ hostvars[vmname].vm_install_cooked.disks.virtio | default({}) | combine(hostvars[vmname].vm_install_cooked.disks.scsi | default({})) }}"
+ with_dict: "{{ hostvars[hostname].vm_install_cooked.disks.virtio | default({}) | combine(hostvars[hostname].vm_install_cooked.disks.scsi | default({})) }}"
lvol:
vg: "{{ item.value.vg }}"
lv: "{{ item.value.lv }}"
- name: check if vm already exists
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
command: info
register: vmhost_info
- block:
- name: destroy exisiting vm
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
state: destroyed
- name: wait for vm to be destroyed
wait_for_virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
states: shutdown,crashed
timeout: 5
- name: undefining exisiting vm
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
command: undefine
- when: vmname in vmhost_info
+ when: hostname in vmhost_info
- name: enable installer in VM config
set_fact:
- name: define new installer vm
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
command: define
xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}"
- name: start vm
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
state: running
- name: wait for installer to start
wait_for_virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
states: running
timeout: 10
- debug:
- msg: "you can check on the status of the installer running this command 'virsh console {{ vmname }}' on host {{ inventory_hostname }}."
+ msg: "you can check on the status of the installer running this command 'virsh console {{ hostname }}' on host {{ inventory_hostname }}."
- name: wait for installer to finish or crash
wait_for_virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
states: shutdown,crashed
timeout: 1800
register: installer_result
- name: undefining installer vm
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
command: undefine
- name: disable installer in VM config
- name: define new production vm
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
command: define
xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}"
- name: start vm
virt:
- name: "{{ vmname }}"
+ name: "{{ hostname }}"
state: running
- name: mark vm as autostarted
virt:
- name: "{{ vmname }}"
- autostart: "{{ hostvars[vmname].vm_install_cooked.autostart }}"
+ name: "{{ hostname }}"
+ autostart: "{{ hostvars[hostname].vm_install_cooked.autostart }}"
command: info ## virt module needs either command or state
- when: hostvars[vmname].vm_install_cooked.autostart is defined
+ when: hostvars[hostname].vm_install_cooked.autostart is defined
<domain type='kvm'>
- <name>{{ vmname }}</name>
- <memory>{{ hostvars[vmname].vm_install_cooked.mem * 1024 }}</memory>
- <currentMemory>{{ hostvars[vmname].vm_install_cooked.mem * 1024 }}</currentMemory>
- <vcpu>{{ hostvars[vmname].vm_install_cooked.numcpu }}</vcpu>
+ <name>{{ hostname }}</name>
+ <memory>{{ hostvars[hostname].vm_install_cooked.mem * 1024 }}</memory>
+ <currentMemory>{{ hostvars[hostname].vm_install_cooked.mem * 1024 }}</currentMemory>
+ <vcpu>{{ hostvars[hostname].vm_install_cooked.numcpu }}</vcpu>
<os>
<type arch='x86_64' machine='pc-0.12'>hvm</type>
{% if run_installer %}
- <kernel>{{ debian_installer_path }}/{{ distro }}-{{ distcodename }}/{{ hostvars[vmname].vm_install_cooked.arch | default('amd64') }}/linux</kernel>
- <initrd>{{ preseed_path }}/{{ vmname }}-{{ distro }}-{{ distcodename }}.initrd.gz</initrd>
+ <kernel>{{ debian_installer_path }}/{{ distro }}-{{ distcodename }}/{{ hostvars[hostname].vm_install_cooked.arch | default('amd64') }}/linux</kernel>
+ <initrd>{{ preseed_path }}/{{ hostname }}-{{ distro }}-{{ distcodename }}.initrd.gz</initrd>
<cmdline>console=ttyS0,115200n8</cmdline>
{% endif %}
<boot dev='hd'/>
<backend model='random'>/dev/urandom</backend>
</rng>
-{% if 'virtio' in hostvars[vmname].vm_install_cooked.disks %}
-{% for device, lv in hostvars[vmname].vm_install_cooked.disks.virtio.items() %}
+{% if 'virtio' in hostvars[hostname].vm_install_cooked.disks %}
+{% for device, lv in hostvars[hostname].vm_install_cooked.disks.virtio.items() %}
<disk type='block' device='disk'>
<driver name='qemu' type='raw' cache='none' discard='unmap'/>
<source dev='/dev/mapper/{{ lv.vg | replace('-', '--') }}-{{ lv.lv | replace('-', '--') }}'/>
{% endfor %}
{% endif %}
-{% if 'scsi' in hostvars[vmname].vm_install_cooked.disks %}
+{% if 'scsi' in hostvars[hostname].vm_install_cooked.disks %}
<controller type='scsi' index='0' model='virtio-scsi'/>
-{% for device, lv in hostvars[vmname].vm_install_cooked.disks.scsi.items() %}
+{% for device, lv in hostvars[hostname].vm_install_cooked.disks.scsi.items() %}
<disk type='block' device='disk'>
<driver name='qemu' type='raw' cache='none' discard='unmap'/>
<source dev='/dev/mapper/{{ lv.vg | replace('-', '--') }}-{{ lv.lv | replace('-', '--') }}'/>
{% endfor %}
{% endif %}
-{% if hostvars[vmname].vm_install_cooked.interfaces %}
-{% for if in hostvars[vmname].vm_install_cooked.interfaces %}
+{% if hostvars[hostname].vm_install_cooked.interfaces %}
+{% for if in hostvars[hostname].vm_install_cooked.interfaces %}
<interface type='bridge'>
<source bridge='{{ if.bridge }}'/>
<model type='virtio'/>
echo ""
echo "######## running the install playbook ########"
-exec ansible-playbook -e "vmname=$name" -e "distro=$distro" -e "distcodename=$codename" $@ vm-install.yml
+exec ansible-playbook -e "hostname=$name" -e "distro=$distro" -e "distcodename=$codename" $@ vm-install.yml
---
- name: preperations and sanity checks
- hosts: "{{ vmname }}"
+ hosts: "{{ hostname }}"
gather_facts: no
tasks:
- name: setup variables
- role: vm/install
- name: wait for new vm to start up
- hosts: "{{ vmname }}"
+ hosts: "{{ hostname }}"
gather_facts: no
tasks:
## TODO: find a better way to fetch host key of new VMs
ansible_ssh_extra_args: ""
- name: Apply VM configuration roles
- hosts: "{{ vmname }}"
+ hosts: "{{ hostname }}"
roles:
- role: vm/grub
- role: vm/network
- role: vm/guest
-- import_playbook: "host_playbooks/{{ vmname }}.yml"
+- import_playbook: "host_playbooks/{{ hostname }}.yml"
- name: reboot and wait for VM come back
- hosts: "{{ vmname }}"
+ hosts: "{{ hostname }}"
gather_facts: no
roles:
- role: reboot-and-wait