From 77b472fea236e38bf6b1ffe99187f850d8727962 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 24 Apr 2018 21:31:16 +0200 Subject: [PATCH] ansible: vm/network role improvments --- ansible/host_vars/testvm/main.yml | 13 +++++++------ .../roles/vm/install/templates/libvirt-domain.xml.j2 | 4 ++-- .../install/templates/preseed_debian-stretch.cfg.j2 | 4 ++-- .../vm/install/templates/preseed_ubuntu-xenial.cfg.j2 | 4 ++-- ansible/roles/vm/network/tasks/main.yml | 17 +++++++++++++++-- ansible/roles/vm/network/templates/resolv.conf.j2 | 4 ++++ ansible/roles/vm/network/templates/systemd.link.j2 | 2 +- 7 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 ansible/roles/vm/network/templates/resolv.conf.j2 diff --git a/ansible/host_vars/testvm/main.yml b/ansible/host_vars/testvm/main.yml index f894adc..0709a22 100644 --- a/ansible/host_vars/testvm/main.yml +++ b/ansible/host_vars/testvm/main.yml @@ -15,18 +15,19 @@ vm_install: lv: "{{ inventory_hostname }}" size: 10g interfaces: - - idx: 1 - bridge: "{{ hostvars[vm_install_host].vm_host.network.interface }}" - name: primary0 + - bridge: "{{ hostvars[vm_install_host].vm_host.network.interface }}" + name: mgmt0 + - bridge: "br-http" + name: http0 autostart: True vm_network: + nameservers: "{{ hostvars[vm_install_host].vm_host.network.nameservers }}" + domain: realraum.at systemd_link: interfaces: "{{ vm_install.interfaces }}" primary: - interface: primary0 + interface: mgmt0 ip: "{{ (hostvars[vm_install_host].vm_host.network.ip+'/'+hostvars[vm_install_host].vm_host.network.mask) | ipaddr(hostvars[vm_install_host].vm_host.network.indices[inventory_hostname]) | ipaddr('address') }}" mask: "{{ hostvars[vm_install_host].vm_host.network.mask }}" gateway: "{{ hostvars[vm_install_host].vm_host.network.gateway | default(hostvars[vm_install_host].vm_host.network.ip) }}" - nameservers: "{{ hostvars[vm_install_host].vm_host.network.nameservers }}" - domain: realraum.at diff --git a/ansible/roles/vm/install/templates/libvirt-domain.xml.j2 b/ansible/roles/vm/install/templates/libvirt-domain.xml.j2 index 9364a7d..2bf4b57 100644 --- a/ansible/roles/vm/install/templates/libvirt-domain.xml.j2 +++ b/ansible/roles/vm/install/templates/libvirt-domain.xml.j2 @@ -8,7 +8,7 @@ {% if run_installer %} {{ vm_host.installer.path }}/{{ vmdistro }}-{{ vmdistcodename }}/{{ hostvars[vmname].vm_install_cooked.arch | default('amd64') }}/linux {{ vm_host.installer.path }}/{{ vmdistro }}-{{ vmdistcodename }}/{{ hostvars[vmname].vm_install_cooked.arch | default('amd64') }}/initrd.gz - console=ttyS0,115200n8 auto=true interface=auto url=tftp://{{ hostvars[inventory_hostname]['ansible_' + (vm_host.installer.net_if | replace('-', '_'))].ipv4.address }}/vm-{{ vmname }}-{{ vmdistro }}-{{ vmdistcodename }}.cfg netcfg/choose_interface=enp1s1 netcfg/disable_autoconfig=true netcfg/get_ipaddress={{ hostvars[vmname].vm_network_cooked.primary.ip }} netcfg/get_netmask={{ hostvars[vmname].vm_network_cooked.primary.mask }} netcfg/get_gateway={{ hostvars[vmname].vm_network_cooked.primary.gateway }} netcfg/get_nameservers="{{ hostvars[vmname].vm_network_cooked.primary.nameservers | join(' ') }}" netcfg/confirm_static=true netcfg/get_hostname={{ vmname }} netcfg/get_domain={{ hostvars[vmname].vm_network_cooked.primary.domain }} + console=ttyS0,115200n8 auto=true interface=auto url=tftp://{{ hostvars[inventory_hostname]['ansible_' + (vm_host.installer.net_if | replace('-', '_'))].ipv4.address }}/vm-{{ vmname }}-{{ vmdistro }}-{{ vmdistcodename }}.cfg netcfg/choose_interface=enp1s1 netcfg/disable_autoconfig=true netcfg/get_ipaddress={{ hostvars[vmname].vm_network_cooked.primary.ip }} netcfg/get_netmask={{ hostvars[vmname].vm_network_cooked.primary.mask }} netcfg/get_gateway={{ hostvars[vmname].vm_network_cooked.primary.gateway }} netcfg/get_nameservers="{{ hostvars[vmname].vm_network_cooked.nameservers | join(' ') }}" netcfg/confirm_static=true netcfg/get_hostname={{ vmname }} netcfg/get_domain={{ hostvars[vmname].vm_network_cooked.domain }} {% endif %} @@ -55,7 +55,7 @@ -
+
{% endfor %} {% endif %} diff --git a/ansible/roles/vm/install/templates/preseed_debian-stretch.cfg.j2 b/ansible/roles/vm/install/templates/preseed_debian-stretch.cfg.j2 index 0e14edd..5b1b3f8 100644 --- a/ansible/roles/vm/install/templates/preseed_debian-stretch.cfg.j2 +++ b/ansible/roles/vm/install/templates/preseed_debian-stretch.cfg.j2 @@ -13,11 +13,11 @@ d-i keyboard-configuration/xkb-keymap select us #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.primary.nameservers | join(' ') }} +#d-i netcfg/get_nameservers string {{ hostvars[vmname].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.primary.domain }} +d-i netcfg/get_domain string {{ hostvars[vmname].vm_network_cooked.domain }} d-i netcfg/wireless_wep string diff --git a/ansible/roles/vm/install/templates/preseed_ubuntu-xenial.cfg.j2 b/ansible/roles/vm/install/templates/preseed_ubuntu-xenial.cfg.j2 index b1c77c1..aaae381 100644 --- a/ansible/roles/vm/install/templates/preseed_ubuntu-xenial.cfg.j2 +++ b/ansible/roles/vm/install/templates/preseed_ubuntu-xenial.cfg.j2 @@ -17,11 +17,11 @@ d-i keyboard-configuration/layoutcode string us #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.primary.nameservers | join(' ') }} +#d-i netcfg/get_nameservers string {{ hostvars[vmname].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.primary.domain }} +d-i netcfg/get_domain string {{ hostvars[vmname].vm_network_cooked.domain }} d-i netcfg/wireless_wep string diff --git a/ansible/roles/vm/network/tasks/main.yml b/ansible/roles/vm/network/tasks/main.yml index 6668a4c..3d51fff 100644 --- a/ansible/roles/vm/network/tasks/main.yml +++ b/ansible/roles/vm/network/tasks/main.yml @@ -9,10 +9,12 @@ state: absent - name: install systemd network link units + with_items: "{{ vm_network.systemd_link.interfaces }}" + loop_control: + index_var: interface_index template: src: systemd.link.j2 - dest: "/etc/systemd/network/{{ '%02d' | format(item.idx + 10) }}-{{ item.name }}.link" - with_items: "{{ vm_network.systemd_link.interfaces }}" + dest: "/etc/systemd/network/{{ '%02d' | format(interface_index + 11) }}-{{ item.name }}.link" notify: rebuild initramfs when: vm_network.systemd_link is defined @@ -22,3 +24,14 @@ src: interfaces.j2 dest: /etc/network/interfaces mode: 0644 + +- name: remove resolvconf package + apt: + name: resolvconf + state: absent + purge: yes + +- name: generate resolv.conf + template: + src: resolv.conf.j2 + dest: /etc/resolv.conf diff --git a/ansible/roles/vm/network/templates/resolv.conf.j2 b/ansible/roles/vm/network/templates/resolv.conf.j2 new file mode 100644 index 0000000..86d4201 --- /dev/null +++ b/ansible/roles/vm/network/templates/resolv.conf.j2 @@ -0,0 +1,4 @@ +{% for nsrv in vm_network.nameservers %} +nameserver {{ nsrv }} +{% endfor %} +search {{ vm_network.domain }} diff --git a/ansible/roles/vm/network/templates/systemd.link.j2 b/ansible/roles/vm/network/templates/systemd.link.j2 index 7486dfb..7093e16 100644 --- a/ansible/roles/vm/network/templates/systemd.link.j2 +++ b/ansible/roles/vm/network/templates/systemd.link.j2 @@ -1,5 +1,5 @@ [Match] -Path=*pci-0000:01:{{ "%02d" | format(item.idx) }}.0 +Path=*pci-0000:01:{{ "%02d" | format(interface_index + 1) }}.0 [Link] Name={{ item.name }} -- 1.7.10.4