Replace vmname with hostname
authornicoo <nicoo@realraum.at>
Sun, 17 Jun 2018 17:12:09 +0000 (19:12 +0200)
committernicoo <nicoo@realraum.at>
Sat, 24 Nov 2018 23:30:26 +0000 (00:30 +0100)
ansible/remove_known_hosts.sh
ansible/remove_known_hosts.yml
ansible/roles/preseed/templates/preseed_debian-stretch.cfg.j2
ansible/roles/preseed/templates/preseed_ubuntu-xenial.cfg.j2
ansible/roles/vm/install/meta/main.yml
ansible/roles/vm/install/tasks/main.yml
ansible/roles/vm/install/templates/libvirt-domain.xml.j2
ansible/vm-install.sh
ansible/vm-install.yml

index ee4a0fb..81d8de2 100755 (executable)
@@ -2,12 +2,12 @@
 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
index 5491342..7173334 100644 (file)
@@ -2,10 +2,10 @@
   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 }}"
index d802418..37f428b 100644 (file)
@@ -10,14 +10,14 @@ d-i keyboard-configuration/xkb-keymap select us
 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
 
 
@@ -37,7 +37,7 @@ d-i time/zone string Europe/Vienna
 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
@@ -49,22 +49,22 @@ d-i partman-auto/expert_recipe string                                   \
       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 }                            \
@@ -72,7 +72,7 @@ d-i partman-auto/expert_recipe string                                   \
                       options/noexec{ noexec }                          \
               .                                                         \
               16 20000 -1 ext4                                          \
-                      $lvmok{ } in_vg{ {{ vmname }} }                   \
+                      $lvmok{ } in_vg{ {{ hostname }} }                   \
                       method( keep } lv_name{ dummy }                   \
               .
 
@@ -92,7 +92,7 @@ d-i pkgsel/include string openssh-server python
 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
 
@@ -100,11 +100,11 @@ d-i finish-install/reboot_in_progress note
 
 
 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 %}
index aaae381..e1e3cec 100644 (file)
@@ -14,14 +14,14 @@ d-i keyboard-configuration/layoutcode string us
 
 #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
 
 
@@ -42,10 +42,10 @@ d-i time/zone string Europe/Vienna
 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
@@ -56,22 +56,22 @@ d-i partman-auto/expert_recipe string                                   \
       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 }                            \
@@ -79,7 +79,7 @@ d-i partman-auto/expert_recipe string                                   \
                       options/noexec{ noexec }                          \
               .                                                         \
               16 20000 -1 ext4                                          \
-                      $lvmok{ } in_vg{ {{ vmname }} }                   \
+                      $lvmok{ } in_vg{ {{ hostname }} }                   \
                       method( keep } lv_name{ dummy }                   \
               .
 
@@ -100,7 +100,7 @@ d-i pkgsel/upgrade select safe-upgrade
 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
 
@@ -108,7 +108,7 @@ d-i finish-install/reboot_in_progress note
 
 
 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"
index 6f6bcf8..0822937 100644 (file)
@@ -1,6 +1,6 @@
 ---
 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) }}"
index a97230f..14886d9 100644 (file)
@@ -1,6 +1,6 @@
 ---
 - 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 }}"
@@ -8,28 +8,28 @@
 
 - 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
@@ -65,7 +65,7 @@
 
 - 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
index f660336..aa45abb 100644 (file)
@@ -1,13 +1,13 @@
 <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'/>
@@ -35,8 +35,8 @@
       <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('-', '--') }}'/>
@@ -45,9 +45,9 @@
 {%   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('-', '--') }}'/>
@@ -56,8 +56,8 @@
 {%   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'/>
index 2fc101d..47291ed 100755 (executable)
@@ -16,4 +16,4 @@ echo "installing vm: $name with $distro/$codename"
 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
index 507906f..9e7f055 100644 (file)
@@ -1,6 +1,6 @@
 ---
 - name: preperations and sanity checks
-  hosts: "{{ vmname }}"
+  hosts: "{{ hostname }}"
   gather_facts: no
   tasks:
   - name: setup variables
@@ -22,7 +22,7 @@
   - 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