Add initial config to support deployments on VMs
[kuberef.git] / playbooks / roles / configure-vms / tasks / main.yaml
1 ---
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19
20 - name: Get node names from IDF
21   set_fact:
22     node_name: "{{ item.key }}"
23   with_dict: "{{ idf.kubespray.hostnames }}"
24   when: item.value == inventory_hostname
25
26 - name: Set facts for the nodes
27   set_fact:
28     node: "{{ nodes | selectattr('name', 'equalto', node_name) | first }}"
29
30 - name: Configure modules
31   lineinfile:
32     dest: /etc/modules
33     state: present
34     create: true
35     line: "8021q"
36
37 - name: Add modules
38   modprobe:
39     name: 8021q
40     state: present
41
42 - name: Ensure interfaces.d folder is empty
43   file:
44     state: "{{ item }}"
45     path: "/etc/network/interfaces.d"
46   with_items:
47     - absent
48     - directory
49
50 - name: Ensure /etc/interfaces can source additional files
51   copy:
52     content: |
53       auto lo
54       iface lo inet loopback
55       source /etc/network/interfaces.d/*.cfg
56     dest: "/etc/network/interfaces"
57
58 - name: Compute mapping dict from mac address to device name
59   set_fact:
60     device_mac_dict: "{{ (device_mac_dict | default({})) | combine({item.macaddress: item.device}) }}"
61   loop: |-
62       {{ ansible_interfaces | map('regex_replace', '-', '_') | map('regex_replace', '^', 'ansible_') |
63       map('extract', hostvars[inventory_hostname]) |  selectattr('macaddress','defined') | list }}
64   when: "'.' not in item.device"
65
66 - name: Filter to include only configured ethernet interfaces
67   set_fact:
68     if_mac_dict: "{{ ( if_mac_dict | default({}) ) | combine({item.mac_address: device_mac_dict[item.mac_address]}) }}"
69   loop: "{{ node.interfaces }}"
70
71 - name: Configure networking for host
72   template:
73     src: "Debian.interface.j2"
74     dest: "/etc/network/interfaces.d/{{ item.value }}.cfg"
75   loop: "{{ if_mac_dict | dict2items }}"
76
77 - name: Reboot the machine
78   shell: "sleep 5 && reboot"
79   async: 1
80   poll: 0
81   changed_when: false
82
83 - name: Wait for host to come back to life
84   wait_for_connection:
85     connect_timeout: 10
86     sleep: 5
87     delay: 120
88     timeout: 300
89   register: result
90   until: result is succeeded
91   retries: 3