Allow VMs to access internet 03/52203/9
authorJohn O Loughlin <john.oloughlin@intel.com>
Thu, 15 Feb 2018 18:24:52 +0000 (18:24 +0000)
committerJohn O'Loughlin <john.oloughlin@intel.com>
Tue, 6 Mar 2018 08:52:20 +0000 (08:52 +0000)
Add option to the config file for default network.
Check to see if 1 default network is defined.
Enable nat on all defined networks.
Add default gateway to VM

JIRA: YARDSTICK-1016

Change-Id: Ie943a7c7b4f1a9673774cf8fedaba0af352cd7f4
Signed-off-by: John O Loughlin <john.oloughlin@intel.com>
ansible/roles/infra_check_requirements/tasks/main.yml
ansible/roles/infra_create_network/tasks/create_xml.yml [moved from ansible/roles/infra_create_network/tasks/create_xml.yaml with 85% similarity]
ansible/roles/infra_create_network/tasks/main.yml
ansible/roles/infra_create_vms/tasks/configure_vm.yml
ansible/roles/infra_create_vms/tasks/create_interfaces.yml
ansible/roles/infra_create_vms/tasks/main.yml
etc/infra/infra_deploy.yaml.sample

index 8d05bbd..a11bc56 100644 (file)
     msg: "Failed, not enough CPU, required: {{ vcpu_t }}, available {{ ansible_processor_vcpus }}"
   when: ansible_processor_vcpus < vcpu_t|int
 
+- name: Define default network counter
+  set_fact:
+    num_default_network_detected: 0
+
+- name: Increment counter for every default network detected
+  set_fact:
+    num_default_network_detected: "{{ num_default_network_detected|int + 1 }}"
+  when:
+    - item.default_gateway is defined
+    - item.default_gateway == True
+  with_items: "{{infra_deploy_vars.networks}}"
+
+- name: Fail if more than 1 or 0 default networks
+  fail:
+    msg: "Failed, there must be 1 default network: {{ num_default_network_detected }} detected"
+  when: num_default_network_detected|int != 1
 
 - name: Fail if not enough Disk space
   set_fact:
     xpath: /network
     add_children:
       - name: "{{ item.name }}"
+      - forward:
+          mode: nat
       - bridge
       - ip
     pretty_print: yes
 
+- name: Add new children nodes to "forward" node
+  xml:
+    path: "{{ xml_file }}"
+    xpath: /network/forward
+    add_children:
+      - nat
+    pretty_print: yes
+
+- name: Add new children nodes to "nat" node
+  xml:
+    path: "{{ xml_file }}"
+    xpath: /network/forward/nat
+    add_children:
+      - port:
+          start: "1024"
+          end: "65535"
+    pretty_print: yes
+
 - name: Add "name" attribute to "bridge" node
   xml:
     path: "{{ xml_file }}"
index eba4a3a..025a775 100644 (file)
@@ -18,5 +18,5 @@
     name: infra_deploy_vars
 
 - name: Create XML file
-  include_tasks: create_xml.yaml
+  include_tasks: create_xml.yml
   with_items: "{{ infra_deploy_vars.networks }}"
index 10201cf..94f6f13 100644 (file)
 
 - name: Populate network-config and add interface to xml file
   include_tasks: create_interfaces.yml
-  extra_vars: "{{ network_config, xml_file , slot_address, mac_address_counter }}"
+  extra_vars: "{{ network_config xml_file slot_address mac_address_counter }}"
   loop_control:
     loop_var: interface_item
   with_items: "{{ node_item.interfaces }}"
   shell: >
     qemu-img convert -O qcow2 {{ node_item.image }} {{ image_dir+node_item.hostname+'.qcow2' }}
 
-- name: Copy and convert the ubuntu image
+- name: Resize image
   shell: >
     qemu-img resize {{ image_dir+node_item.hostname+'.qcow2' }} {{ node_item.disk }}MB
 
index 124421b..34bfd1b 100644 (file)
           addresses:
             - {{ interface_item.ip }}/{{ interface_item.netmask }}
 
+- name: Add default gateway
+  blockinfile:
+    path: "{{ network_config }}"
+    insertafter: "{{ interface_item.ip }}"
+    marker: "MARKER"
+    block: |2
+          gateway4: {{ default_bridge.host_ip }}
+  when:
+    - default_bridge is defined
+    - default_bridge.name == interface_item.network
 
 - name: Remove the marker introduced in network-data
   lineinfile:
index 62a023e..4d47f44 100644 (file)
 - set_fact:
     mac_address_counter: 0
 
+- set_fact:
+    default_bridge: "{{ item }}"
+  when:
+    - item.default_gateway is defined
+    - item.default_gateway == True
+  with_items: "{{ infra_deploy_vars.networks }}"
+
 - name: Create XML file
   include_tasks: configure_vm.yml
-  extra_vars: "{{ mac_address_counter }}"
   loop_control:
     loop_var: node_item
   with_items: "{{ infra_deploy_vars.nodes }}"
index bf07a01..8ed7936 100644 (file)
@@ -31,5 +31,11 @@ nodes:
 
 networks:
   - name: management
+    default_gateway: True
     host_ip: 192.168.1.1
     netmask: 255.255.255.0
+
+  - name: traffic
+    default_gateway: False  # This parameter is not mandatory, default value: False
+    host_ip: 192.20.1.1
+    netmask: 255.255.255.0