Initial commit
[ta/infra-ansible.git] / roles / baremetal_provision / templates / baremetal_nodes.j2
1 {#
2 This file is written with conditional check and for loop based indentation.
3 It improved code readability. The output json is not pretty. Run cat <output.json> | python -m json.tool
4 for pretty printing the same.
5 #}
6
7 {% macro generate_linux_bonding_options(options) -%}
8     {%- set mode_mapping = {'active-backup' : 'active-backup', 'lacp' : '802.3ad'} -%}
9     {%- set default_options = {
10                                 'active-backup' : 'miimon=100',
11                                 'lacp' : 'lacp_rate=fast miimon=100'
12                               } -%}
13     {%- for i in options.split() -%}
14         {%- set key, value = i.split('=') -%}
15         {%- if key == 'mode' -%}
16             {%- if default_options[value] -%}
17                 {{ 'mode=' ~ mode_mapping[value] ~ ' ' ~ default_options[value] }}
18             {%- else -%}
19                 {{ 'mode=' ~ mode_mapping[value] }}
20             {%- endif -%}
21         {%- endif -%}
22     {%- endfor -%}
23 {%- endmacro %}
24
25 {% macro generate_ovs_bonding_options(options) -%}
26     {%- set mode_mapping = {'active-backup' : 'active-backup', 'lacp' : 'balance-slb', 'lacp-layer34' : 'balance-tcp'} -%}
27     {%- set default_options = {
28                                 'active-backup' : '',
29                                 'lacp' : 'lacp=active other_config:lacp-time=fast other_config:bond-detect-mode=carrier',
30                                 'lacp-layer34' : 'lacp=active other_config:lacp-time=fast other_config:bond-detect-mode=carrier'
31                               } -%}
32     {%- for i in options.split() -%}
33         {%- set key, value = i.split('=') -%}
34         {%- if key == 'mode' -%}
35             {%- if default_options[value] -%}
36                 {{ 'bond_mode=' ~ mode_mapping[value] ~ ' ' ~ default_options[value] }}
37             {%- else -%}
38                 {{ 'bond_mode=' ~ mode_mapping[value] }}
39             {%- endif -%}
40         {%- endif -%}
41     {%- endfor -%}
42 {%- endmacro %}
43
44 {% macro add_static_routes(routes) -%}
45     [
46     {%- for route in routes %}
47         {
48             "ip_netmask": "{{ route.to }}", "next_hop": "{{ route.via }}"
49         } {% if not loop.last %},{% endif %}
50     {%- endfor %}
51     ]
52 {%- endmacro %}
53
54 {%- set nfs_server_ip = hostvars[installation_controller]['networking']['infra_external']['ip'] %}
55 {
56 "ironic_nodes": [
57 {% for host_key,host_value in hosts.iteritems() %}
58
59 {%- set host_networking = hostvars[host_key]['networking'] %}
60 {%- set host_network_profiles_list = hosts[host_key]['network_profiles'] %}
61 {%- set host_network_profile_value = hostvars[host_key]['network_profiles'][host_network_profiles_list[0]] %}
62 {%- set host_interface_net_mapping = host_network_profile_value['interface_net_mapping'] %}
63 {%- set host_hdd_mapping = hostvars[host_key]['by_path_disks'] %}
64
65 {%- set infra_bond = {'in_use': false} %}
66 {%- if 'bonding_interfaces' in host_network_profile_value %}
67     {%- set host_bonding_interfaces = host_network_profile_value['bonding_interfaces'] %}
68 {%- endif %}
69
70 {%- set single_nic_setup = False %}
71 {%- if 'provider_network_interfaces' in host_network_profile_value %}
72     {%- set host_provider_network_interfaces = host_network_profile_value['provider_network_interfaces'] %}
73     {%- if host_interface_net_mapping|length == 1 and host_provider_network_interfaces|length == 1 %}
74         {%- if host_interface_net_mapping.keys()[0] == host_provider_network_interfaces.keys()[0] %}
75             {%- set single_nic_setup = True %}
76         {%-endif %}
77     {%-endif %}
78 {%-endif %}
79
80 {%- set mtu = 1500 %}
81 {%- if 'mtu' in host_networking['infra_internal'] %}
82     {%- set mtu = host_networking['infra_internal']['mtu'] %}
83 {%- elif 'mtu' in host_networking %}
84     {%- set mtu = host_networking['mtu'] %}
85 {%- endif %}
86
87 {%- set phys_iface = {'mtu': 1500} %}
88 {%- if 'vlan' in host_networking['infra_internal'] %}
89     {%- for iface,infras in host_interface_net_mapping.iteritems() %}
90         {%- if "infra_internal" in infras %}
91             {%- for infra in infras %}
92                 {%- set tmp_mtu = 0 %}
93                 {%- if 'mtu' in host_networking[infra] %}
94                     {%- set tmp_mtu = host_networking[infra]['mtu'] %}
95                 {%- elif 'mtu' in host_networking %}
96                     {%- set tmp_mtu = host_networking['mtu'] %}
97                 {%- endif %}
98                 {%- if infra == 'cloud_tenant' %}
99                     {%- set tmp_mtu = tmp_mtu + 50 %}
100                 {%- endif %}
101                 {%- if tmp_mtu > phys_iface.mtu %}
102                     {%- if phys_iface.update({'mtu': tmp_mtu}) %}{%- endif %}
103                 {%- endif %}
104             {%- endfor %}
105         {%- endif %}
106     {%- endfor %}
107 {%- endif %}
108
109     {
110     "name": "{{ host_key }}",
111     "network_interface": "noop",
112     "nics":  [ {%- if 'mgmt_mac' in host_value %}
113                    {%- for mac_members in host_value['mgmt_mac'] %}
114                        {
115                        "mac": "{{ mac_members }}"
116                        }
117                        {%- if not loop.last %},{%- endif %}
118                    {%- endfor %}
119                {%- else: %}
120                    {
121                    "mac": "{{ hw_inventory_details[host_key]['mgmt_mac'] }}"
122                    }
123                {%- endif %}
124              ],
125     "driver_info": { "power": {
126         "os_net_config": {
127             "network_config": [
128             {%- if 'interface' in host_networking['infra_internal'] %}
129                 {%- if not single_nic_setup %}
130                 {%- if 'bonding_interfaces' in host_network_profile_value %}
131                     {%- for net_key,net_value in host_interface_net_mapping.iteritems() %}
132                         {%- if "bond" in net_key and "infra_internal" in net_value %}
133                         {
134                             "name": "{{net_key}}",
135                             "type": "linux_bond",
136                             "members": [
137                             {%- for member in host_bonding_interfaces[net_key] %}
138                             {
139                                 {%- if 'bond' in host_networking['infra_internal']['interface'] %}
140                                     "mtu": {{mtu}},
141                                 {%- else %}
142                                     "mtu": {{phys_iface['mtu']}},
143                                 {%- endif %}
144                                 "name": "{{ member }}",
145                                 "type": "interface",
146                                 "use_dhcp": false
147                             }    {%- if not loop.last %},{%- endif %}
148                             {% endfor %} ],
149                             {%- if 'linux_bonding_options' in host_network_profile_value %}
150                                 "bonding_options": "{{ generate_linux_bonding_options(host_network_profile_value['linux_bonding_options']) }}",
151                             {%- endif %}
152                             {%- if 'bond' in host_networking['infra_internal']['interface'] %}
153                                 "addresses": [ { "ip_netmask": "{{ host_networking['infra_internal']['ip'] }}/{{host_networking['infra_internal']['mask']}}" } ],
154                                 "mtu": {{mtu}},
155                                 {%- if 'routes' in host_networking['infra_internal'] %}
156                                     {%- set routes = host_networking['infra_internal']['routes'] %}
157                                     "routes": {{ add_static_routes(routes) }},
158                                 {%- endif %}
159                             {%- else %}
160                                 "mtu": {{phys_iface['mtu']}},
161                             {%- endif %}
162                             "use_dhcp": false
163                             {%- if infra_bond.update({'in_use': true}) %}{%- endif %}
164                         },
165                         {%- endif %}
166                     {%- endfor %}
167                 {%- endif %}
168                 {%- if 'vlan' in host_networking['infra_internal'] %}
169                     {
170                     "type": "vlan",
171                     "addresses": [ { "ip_netmask": "{{ host_networking['infra_internal']['ip'] }}/{{host_networking['infra_internal']['mask']}}" } ],
172                     "vlan_id": {{ host_networking['infra_internal']['vlan'] }},
173                     {%- for net_key,net_value in host_interface_net_mapping.iteritems() %}
174                         {%- if "infra_internal" in net_value %}
175                             "device": "{{net_key}}",
176                         {%- endif %}
177                     {%- endfor %}
178                     "mtu": {{mtu}},
179                     {%- if 'routes' in host_networking['infra_internal'] %}
180                         {%- set routes = host_networking['infra_internal']['routes'] %}
181                         "routes": {{ add_static_routes(routes) }},
182                     {%- endif %}
183                     "use_dhcp": false
184                     }
185                     {%- if not infra_bond.in_use %}
186                         ,
187                         {
188                         {%- for net_key,net_value in host_interface_net_mapping.iteritems() %}
189                             {%- if "infra_internal" in net_value %}
190                                 "name": "{{net_key}}",
191                             {%- endif %}
192                         {%- endfor %}
193                         "type": "interface",
194                         "mtu": {{phys_iface['mtu']}},
195                         "use_dhcp": false
196                         }
197                     {%- endif %}
198                 {%- elif not infra_bond.in_use %}
199                     {
200                     "name": "{{host_networking['infra_internal']['interface']}}",
201                     "type": "interface",
202                     "addresses": [ { "ip_netmask": "{{ host_networking['infra_internal']['ip'] }}/{{host_networking['infra_internal']['mask']}}" } ],
203                     "mtu": {{mtu}},
204                     {%- if 'routes' in host_networking['infra_internal'] %}
205                         {%- set routes = host_networking['infra_internal']['routes'] %}
206                         "routes": {{ add_static_routes(routes) }},
207                      {%- endif %}
208                     "use_dhcp": false
209                     }
210                 {%- endif %}
211                 {%- else %}{#- if not single_nic_setup -#}
212                     {
213                     "name": "br-pro0",
214                     "type": "ovs_bridge",
215                     "members": [
216                         {
217                         "mtu": {{phys_iface['mtu']}},
218                         {%- set iface = host_interface_net_mapping.keys()[0] %}
219                         {%- if 'bond' in iface %}
220                             {%- for bond_iface,bond_value in host_bonding_interfaces.iteritems() if bond_iface == iface %}
221                                 {%- if 'ovs_bonding_options' in  host_network_profile_value %}
222                                     "ovs_options": "{{ generate_ovs_bonding_options(host_network_profile_value['ovs_bonding_options']) }}",
223                                 {%- endif %}
224                                 "name": "{{iface}}",
225                                 "type": "ovs_bond",
226                                 "members": [ {% for member in bond_value %}
227                                     {
228                                     "name": "{{ member }}",
229                                     "type": "interface",
230                                     "mtu": {{phys_iface['mtu']}},
231                                     "use_dhcp": false
232                                     }
233                                     {% if not loop.last %},{% endif %}
234                                 {%- endfor %}
235                                 ],
236                             {%- endfor %}
237                         {%- else %}
238                             "name": "{{iface}}",
239                             "type": "interface",
240                         {%- endif %}
241                         "use_dhcp": false
242                         }
243                         ,
244                         {
245                         {%- set infra = host_networking['infra_internal'] %}
246                         "type": "vlan",
247                         "vlan_id": {{ infra['vlan'] }},
248                         "addresses": [ { "ip_netmask": "{{ infra['ip'] }}/{{infra['mask']}}" } ],
249                         "mtu": {{mtu}},
250                         {%- if 'routes' in infra %}
251                             {%- set routes = infra['routes'] %}
252                             "routes": {{ add_static_routes(routes) }},
253                         {%- endif %}
254                         "use_dhcp": false
255                         }
256                     ]
257                     }
258                 {%- endif %}{#- if not single_nic_setup -#}
259             {%- endif %}
260             ]
261         },
262     "provisioning_server": "{{nfs_server_ip}}",
263     "provisioning_server_http_port": "{{golden_image_http_port}}",
264     "virtmedia_deploy_iso": "file:///opt/images/ironic-deploy.iso",
265     {% if virtual_env == true %}
266         "ssh_address": "{{ host_value['hwmgmt']['address'] }}",
267         "ssh_key_contents": "{{ '{{' }} lookup('file', '/etc/userconfig/id_rsa') {{ '}}' }}",
268         "ssh_username": "{{ host_value['hwmgmt']['user'] }}",
269         "ipmi_address": "{{hostvars[installation_controller]['networking']['infra_internal']['ip']}}",
270         "ipmi_username": "admin",
271         "ipmi_password": "password",
272         "ipmi_port": "{{ hosts[host_key]['vbmc_port'] }}"
273         } },
274         "driver": "ssh_virtmedia",
275         "properties": { "capabilities": "boot_option:local", "cpu_arch": "x86_64", "cpus": 8, "disk_size": 40, "ram": 16384, "root_device":{ "by_path": "{{ host_hdd_mapping['os']}}" } }
276     {%- else %}
277         "ipmi_address": "{{ host_value['hwmgmt']['address'] }}",
278         "ipmi_password": "{{ host_value['hwmgmt']['password'] }}",
279         "ipmi_username": "{{ host_value['hwmgmt']['user'] }}",
280         "product_family": "{{ hw_inventory_details[host_key]['product_family'] }}",
281         "vendor": "{{ hw_inventory_details[host_key]['vendor'] }}"
282     }
283 },
284         "driver": "ipmi_virtmedia",
285         "properties": { "capabilities": "boot_option:local", "cpu_arch": "x86_64", "cpus": 8, "disk_size": 40, "ram": 16384, "root_device":{ {% if host_hdd_mapping['os'] != "/dev/sda" %} "by_path": "{{ host_hdd_mapping['os']}}" {% else %} "name": "{{ host_hdd_mapping['os']}}" {% endif %} } }
286     {% endif %}
287     }
288     {% if not loop.last %},{% endif %}
289 {%- endfor %}
290 ]
291 }