Generate network_info json using a module

We currently have a jinja template which generates the network_info
json. By using a module we can create the dictionary in python which
is much easier to understand and work with.

Depends-On: I23e902c8637e142fba23d71467225d48ee265253
Change-Id: I75936fa29ec442b28e8d0ca95332157580daaae4
This commit is contained in:
Gregory Haynes 2015-08-24 18:28:09 +00:00 committed by Julia Kreger
parent e51de22084
commit b5d9fe5bfa
3 changed files with 127 additions and 79 deletions

View File

@ -0,0 +1,116 @@
#!/usr/bin/env python
# coding: utf-8 -*-
# (c) 2015, Hewlett-Packard Development Company, L.P.
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
---
module: network_metadata
short_description: Returns a config-drive network-metadata dictionary
extends_documentation_fragment: openstack
'''
def main():
argument_spec = dict(
ipv4_address=dict(required=False),
ipv4_gateway=dict(required=False),
ipv4_interface_mac=dict(required=False),
ipv4_nameserver=dict(required=False),
ipv4_subnet_mask=dict(required=False),
network_mtu=dict(required=False),
nics=dict(required=False),
node_network_info=dict(required=False)
)
module = AnsibleModule(argument_spec)
network_metadata = module.params['node_network_info']
if not network_metadata:
links = []
networks = []
if module.params['ipv4_interface_mac']:
links.append({
'id': module.params['ipv4_interface_mac'],
'type': 'phy',
'ethernet_mac_address': module.params['ipv4_interface_mac'],
'mtu': module.params['network_mtu']
})
for nic in module.params['nics']:
if nic['mac'] == module.params['ipv4_interface_mac']:
networks.append({
'id': 'ipv4-%s' % nic['mac'],
'link': nic['mac'],
'type': 'ipv4',
'ip_address': module.params['ipv4_address'],
'netmask': module.params['ipv4_subnet_mask'],
'dns_nameservers': [
module.params['ipv4_nameserver']
],
'routes': [{
'network': '0.0.0.0',
'netmask': '0.0.0.0',
'gateway': module.params['ipv4_gateway']
}]
})
else:
for i, nic in enumerate(module.params['nics']):
links.append({
'id': nic['mac'],
'type': 'phy',
'ethernet_mac_address': nic['mac'],
'mtu': module.params['network_mtu']
})
if i == 0:
networks.append({
'id': 'ipv4-%s' % nic['mac'],
'link': nic['mac'],
'type': 'ipv4',
'ip_address': module.params['ipv4_address'],
'netmask': module.params['ipv4_subnet_mask'],
'dns_nameservers': [
module.params['ipv4_nameserver']
],
'routes': [{
'network': '0.0.0.0',
'netmask': '0.0.0.0',
'gateway': module.params['ipv4_gateway']
}]
})
else:
networks.append({
'id': 'ipv4-dhcp-%s' % nic['mac'],
'link': nic['mac'],
'type': 'ipv4_dhcp',
})
network_metadata = {
'links': links,
'networks': networks
}
facts = {'network_metadata': network_metadata}
module.exit_json(changed=False, ansible_facts=facts)
# this is magic, see lib/ansible/module_common.py
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()

View File

@ -26,6 +26,16 @@
local_action: template src=openstack_meta_data.json.j2 dest={{ variable_configdrive_location.stdout }}/{{ uuid }}/openstack/{{ metadata_version }}/meta_data.json local_action: template src=openstack_meta_data.json.j2 dest={{ variable_configdrive_location.stdout }}/{{ uuid }}/openstack/{{ metadata_version }}/meta_data.json
- name: "Place template in each openstack/latest folder" - name: "Place template in each openstack/latest folder"
local_action: template src=openstack_meta_data.json.j2 dest={{ variable_configdrive_location.stdout }}/{{ uuid }}/openstack/latest/meta_data.json local_action: template src=openstack_meta_data.json.j2 dest={{ variable_configdrive_location.stdout }}/{{ uuid }}/openstack/latest/meta_data.json
- name: "Generate network_info"
network_metadata:
ipv4_address: "{{ ipv4_address }}"
ipv4_gateway: "{{ ipv4_gateway }}"
ipv4_interface_mac: "{{ ipv4_interface_mac | default('') }}"
ipv4_nameserver: "{{ ipv4_nameserver }}"
ipv4_subnet_mask: "{{ ipv4_subnet_mask }}"
network_mtu: "{{ network_mtu }}"
nics: "{{ nics }}"
node_network_info: "{{ node_network_info | default('') }}"
- name: "Place network info template in each openstack/latest folder" - name: "Place network info template in each openstack/latest folder"
local_action: template src=network_info.json.j2 dest={{ variable_configdrive_location.stdout }}/{{ uuid }}/openstack/latest/network_info.json local_action: template src=network_info.json.j2 dest={{ variable_configdrive_location.stdout }}/{{ uuid }}/openstack/latest/network_info.json
when: addressing_mode is undefined and '"dhcp" not in addressing_mode' when: addressing_mode is undefined and '"dhcp" not in addressing_mode'

View File

@ -1,79 +1 @@
{% if node_network_info is defined %} {{ network_metadata | to_nice_json }}
{{ node_network_info }}
{% else %}
{
"links": [
{% for nic in nics %}
{
{% if ipv4_interface_mac is defined %}
"id": "{{ nic.mac }}",
{% else %}
{% if loop.first %}
"id": "{{ node_default_network_interface }}",
{% else %}
"id": "{{ nic.mac }}",
{% endif %}
"type": "phy",
"ethernet_mac_address": "{{ nic.mac }}",
"mtu":{{network_mtu}}
{% if loop.last %}
}
{% else %}
},
{% endif %}
{% endif %}
{% endfor %}
],
"networks": [
{% for nic in nics %}
{
"id": "ipv4-{{ nic.mac }}",
{% if (ipv4_interface_mac is defined) and (nic.mac == ipv4_interface_mac) %}
"link": "{{ nic.mac }}",
"type": "ipv4",
"ip_address": "{{ ipv4_address }}",
"netmask": "{{ipv4_subnet_mask}}",
"dns_nameservers": [
"{{ipv4_nameserver}}"
],
"routes": [
{
"network": "0.0.0.0",
"netmask": "0.0.0.0",
"gateway": "{{ipv4_gateway}}"
}
]
{% elif loop.first %}
"link": "{{ node_default_network_interface }}",
"type": "ipv4",
"ip_address": "{{ ipv4_address }}",
"netmask": "{{ipv4_subnet_mask}}",
"dns_nameservers": [
"{{ipv4_nameserver}}"
],
"routes": [
{
"network": "0.0.0.0",
"netmask": "0.0.0.0",
"gateway": "{{ipv4_gateway}}"
}
]
{% else %}
"type": "ipv4_dhcp",
"link": "{{ nic.mac }}"
{% endif %}
{% if loop.last %}
}
{% else %}
},
{% endif %}
{% endfor %}
],
"services": [
{
"type": "dns",
"address": "{{ipv4_nameserver}}"
}
]
}
{% endif %}