tenks/ansible/physical_network.yml
2018-09-10 16:13:21 +00:00

103 lines
3.3 KiB
YAML

---
- name: Fail if source interface does not exist
fail:
msg: >
The interface {{ source_interface }} specified for the physical network
{{ network_name }} does not exist.
when: source_interface not in ansible_interfaces
### Firstly, some fact gathering.
# Start off by assuming the source interface is direct, unless proven
# otherwise.
- set_fact:
source_type: direct
- name: Get source interface details
# NOTE(w-miller): The `ip` command may not be in $PATH, so use the full path.
command: /usr/sbin/ip -details link show {{ source_interface }}
register: if_details
changed_when: false
- name: Register source interface as a Linux bridge
set_fact:
source_type: linux_bridge
when: if_details.stdout_lines[-1].split()[0] == 'bridge'
- block:
- name: Get list of OVS bridges
command: ovs-vsctl list-br
register: ovs_bridges
changed_when: false
- name: Register source interface as an Open vSwitch bridge
set_fact:
source_type: ovs_bridge
when: source_interface in ovs_bridges.stdout_lines
when: if_details.stdout_lines[-1].split()[0] == 'openvswitch'
### Actual configuration starts here.
- name: Ensure Open vSwitch bridge is in the correct state
openvswitch_bridge:
bridge: "{{ tenks_bridge }}"
state: "{{ state }}"
become: true
- name: Configure existing Linux bridge
when: source_type == 'linux_bridge'
include_role:
name: veth-pair
vars:
veth_pair_ovs_bridge: "{{ tenks_bridge }}"
veth_pair_ovs_link_name: >-
{{ veth_prefix + tenks_bridge + veth_bridge_ovs_suffix }}
veth_pair_source_bridge: "{{ source_interface }}"
veth_pair_source_link_name: >-
{{ veth_prefix + tenks_bridge + veth_bridge_source_suffix }}
veth_pair_plug_into_source: true
veth_pair_state: "{{ state }}"
- name: Configure existing Open vSwitch bridge
when: source_type == 'ovs_bridge'
block:
- name: Configure patch port on Tenks bridge
openvswitch_port:
bridge: "{{ tenks_bridge }}"
port: "{{ veth_prefix + tenks_bridge + veth_bridge_ovs_suffix }}"
# Despite the module documentation, `set` will happily take multiple
# properties.
set: >-
Interface {{ veth_prefix + tenks_bridge + veth_bridge_ovs_suffix }}
type=patch
options:peer={{ veth_prefix + tenks_bridge +
veth_bridge_source_suffix }}
# If 'absent', we've already deleted the bridge earlier, so no need to
# delete the port.
when: state != 'absent'
become: true
- name: Configure patch port on source bridge
openvswitch_port:
bridge: "{{ source_interface }}"
port: "{{ veth_prefix + tenks_bridge + veth_bridge_source_suffix }}"
set: >-
Interface {{ veth_prefix + tenks_bridge + veth_bridge_source_suffix }}
type=patch
options:peer={{ veth_prefix + tenks_bridge +
veth_bridge_ovs_suffix }}
state: "{{ state }}"
become: true
- name: Plug source interface into Tenks bridge
openvswitch_port:
bridge: "{{ tenks_bridge }}"
port: "{{ source_interface }}"
state: "{{ state }}"
when:
- source_type == 'direct'
# If 'absent', we've already deleted the bridge earlier, so no need to
# unplug the interface.
- state != 'absent'
become: true