e527b701d8aa52d5265b45ef4937a4bfbde5a7c2
[icn.git] / deploy / metal3-vm / vm-setup / roles / libvirt / tasks / network_setup_tasks.yml
1 # If virtualport_type is defined for any networks, include OVS dependencies
2 - when: networks|selectattr('virtualport_type', 'defined')|map(attribute='name')|list|length > 0
3   block:
4
5   # Install OVS dependencies
6   - name: Install OVS dependencies
7     include_role:
8       name: 'parts/ovs'
9
10   # Create any OVS Bridges that have been defined
11   - name: Create OVS Bridges
12     openvswitch_bridge:
13       bridge: "{{ item.bridge }}"
14       state: present
15     when: item.virtualport_type is defined and item.virtualport_type == "openvswitch"
16     with_items: "{{ networks }}"
17     become: true
18
19 # TODO(apuimedo) drop this back to vm tasks once we have proper DNS
20 - name: get a list of MACs to use
21   generate_macs:
22     nodes: "{{ vm_nodes }}"
23     networks: "{{ networks }}"
24   register: node_mac_map
25   when: vm_nodes
26
27
28 # Create the global, root-managed libvirt networks to which we will
29 # attach the undercoud and vm virtual machines.
30 - name: Create libvirt networks
31   virt_net:
32     command: define
33     state: present
34     name: "{{ item.name }}"
35     xml: '{{ lookup("template", "network.xml.j2") }}'
36   with_items: "{{ networks }}"
37   become: true
38
39 - name: Start libvirt networks
40   virt_net:
41     command: start
42     name: "{{ item.name }}"
43     state: active
44   with_items: "{{ networks }}"
45   become: true
46
47 - name: Mark  libvirt networks as autostarted
48   virt_net:
49     name: "{{ item.name }}"
50     autostart: "yes"
51   with_items: "{{ networks }}"
52   become: true
53   register: net_autostart
54   ignore_errors: true
55
56 # https://bugs.launchpad.net/tripleo-quickstart/+bug/1581676
57 # There is a bug w/ virt_net and RHEL where the network xml
58 # file is not written to /etc/libvirt/qemu/networks/ This causes
59 # network to be considered transient.
60 - when: not net_autostart.changed
61   block:
62
63     - name: Check if "virsh net-autostart" was successful
64       debug: msg="Some libvirt networks were not set to autostart. Please see
65              https://bugs.launchpad.net/tripleo-quickstart/+bug/1581676"
66
67     # get the network xml from the running network
68     - name: Get libvirt networks xml
69       virt_net:
70         command: get_xml
71         name: "{{ item.name }}"
72       with_items: "{{ networks }}"
73       register: net_xml
74       become: true
75
76     # copy the xml to a file
77     - name: copy network-xml to file
78       copy: content={{ item.get_xml }} dest=/tmp/network-{{ item.item.name }}.xml
79       with_items: "{{ net_xml.results }}"
80       become: true
81
82     # redefine the network w/ virsh, this will write the xml file to
83     # /etc/libvirt/qemu/networks/ and it will no longer be transient
84     - name: redefine the libvirt networks so the config is written to /etc/libvirt
85       command: virsh net-define /tmp/network-{{ item.name }}.xml
86       with_items: "{{ networks }}"
87       become: true
88
89     # Now we're ready to mark the network autostart
90     - name: Mark libvirt networks as autostarted
91       virt_net:
92         name: "{{ item.name }}"
93         autostart: "yes"
94       with_items: "{{ networks }}"
95       become: true
96
97 # Whitelist the bridges associated with these networks for
98 # access using qemu [helper networking][helper].  Later on we
99 # create virtual machines use an unprivileged `qemu://session`
100 # connection, and we connect to the networks using the bridge names.
101 #
102 # [helper]: http://wiki.qemu.org/Features-Done/HelperNetworking
103 - name: Whitelist bridges for unprivileged access on CentOS
104   lineinfile:
105     dest: '/etc/qemu-kvm/bridge.conf' # Needs to be /etc/qemu/bridge.conf if supporting Fedora
106     line: "allow {{ item.bridge }}"
107   with_items: "{{ networks }}"
108   when: 
109     - ansible_os_family == "RedHat"
110   become: true
111
112 - name: Whitelist bridges for unprivileged access on Ubuntu or Fedora
113   lineinfile:
114     dest: '/etc/qemu/bridge.conf' 
115     line: "allow {{ item.bridge }}"
116     create: yes
117   with_items: "{{ networks }}"
118   when: 
119     - ansible_facts['distribution'] == "Ubuntu"
120   become: true
121
122 # We're going to want to store things in `working_dir` so ensure it
123 # exists first.  `working_dir` is a directory on the target host.
124 - name: Ensure remote working dir exists
125   file:
126     path: "{{ working_dir }}"
127     state: directory
128   become: true