diff --git a/ansible/roles/veth-pair/README.md b/ansible/roles/veth-pair/README.md index e9cd171..6c1cc26 100644 --- a/ansible/roles/veth-pair/README.md +++ b/ansible/roles/veth-pair/README.md @@ -2,7 +2,11 @@ Veth Pair ========= This role creates a veth pair. It will plug one end into the specified OVS -bridge and, optionally, can plug the other end into a source Linux bridge. +bridge and, optionally, can plug the other end into a source Linux bridge. If +`veth_pair_state` is `absent`, it will ensure the veth pair is not plugged into +the OVS bridge; if `veth_pair_plug_into_source` is enabled, it will ensure the +veth pair is not plugged into the source bridge; finally, it will ensure the +veth pair itself does not exist. Requirements ------------ @@ -24,3 +28,5 @@ Role Variables - `veth_pair_plug_into_source`: Whether or not to plug the source end of the veth pair into a Linux bridge. If enabled, `veth_pair_source_bridge` must also be specified. Default is `false`. +- `veth_pair_state`: Whether or not the veth pair should exist. Choose from + `present` or `absent`. Default is `present`. diff --git a/ansible/roles/veth-pair/defaults/main.yml b/ansible/roles/veth-pair/defaults/main.yml index 0010742..319cb25 100644 --- a/ansible/roles/veth-pair/defaults/main.yml +++ b/ansible/roles/veth-pair/defaults/main.yml @@ -1,3 +1,5 @@ --- # Whether or not to plug the source end of the veth pair into a Linux bridge. veth_pair_plug_into_source: false +# Whether the veth pair should be present or absent. +veth_pair_state: present diff --git a/ansible/roles/veth-pair/tasks/absent.yml b/ansible/roles/veth-pair/tasks/absent.yml new file mode 100644 index 0000000..6e4e19e --- /dev/null +++ b/ansible/roles/veth-pair/tasks/absent.yml @@ -0,0 +1,33 @@ +--- +- name: Unplug veth from OVS bridge + openvswitch_port: + bridge: "{{ veth_pair_ovs_bridge }}" + port: "{{ veth_pair_ovs_link_name }}" + state: absent + become: true + +- name: Unplug veth from source bridge + command: >- + brctl delif {{ veth_pair_source_bridge }} + {{ veth_pair_source_link_name }} + register: res + failed_when: + - res.rc != 0 + # Case where veth is already unplugged. + - not (res.rc == 1 and 'does not exist' in res.stderr) + changed_when: res.rc == 0 + when: veth_pair_plug_into_source | bool + become: true + +- name: Delete veth pair + command: >- + ip link del dev {{ veth_pair_ovs_link_name }} + type veth + peer name {{ veth_pair_source_link_name }} + register: res + failed_when: + - res.rc != 0 + # Case where veth pair is already absent. + - not (res.rc == 1 and 'Cannot find device' in res.stderr) + changed_when: res.rc == 0 + become: true diff --git a/ansible/roles/veth-pair/tasks/main.yml b/ansible/roles/veth-pair/tasks/main.yml index 9944eef..ab9de44 100644 --- a/ansible/roles/veth-pair/tasks/main.yml +++ b/ansible/roles/veth-pair/tasks/main.yml @@ -1,29 +1,8 @@ --- -- name: Create veth pair - command: >- - ip link add dev {{ veth_pair_ovs_link_name }} - type veth - peer name {{ veth_pair_source_link_name }} - register: res - changed_when: res.rc == 0 - # Return code 2 means the veth pair already exists - failed_when: res.rc not in [0, 2] - become: true +- name: Ensure veth pair is absent + include_tasks: absent.yml + when: veth_pair_state == 'absent' -- name: Plug veth into OVS bridge - openvswitch_port: - bridge: "{{ veth_pair_ovs_bridge }}" - port: "{{ veth_pair_ovs_link_name }}" - become: true - -- name: Plug veth into source bridge - command: >- - brctl addif {{ veth_pair_source_bridge }} - {{ veth_pair_source_link_name }} - register: res - failed_when: - - res.rc != 0 - - "'already a member of a bridge' not in res.stderr" - changed_when: "'already a member of a bridge' not in res.stderr" - when: veth_pair_plug_into_source | bool - become: true +- name: Ensure veth pair is present + include_tasks: present.yml + when: veth_pair_state != 'absent' diff --git a/ansible/roles/veth-pair/tasks/present.yml b/ansible/roles/veth-pair/tasks/present.yml new file mode 100644 index 0000000..9944eef --- /dev/null +++ b/ansible/roles/veth-pair/tasks/present.yml @@ -0,0 +1,29 @@ +--- +- name: Create veth pair + command: >- + ip link add dev {{ veth_pair_ovs_link_name }} + type veth + peer name {{ veth_pair_source_link_name }} + register: res + changed_when: res.rc == 0 + # Return code 2 means the veth pair already exists + failed_when: res.rc not in [0, 2] + become: true + +- name: Plug veth into OVS bridge + openvswitch_port: + bridge: "{{ veth_pair_ovs_bridge }}" + port: "{{ veth_pair_ovs_link_name }}" + become: true + +- name: Plug veth into source bridge + command: >- + brctl addif {{ veth_pair_source_bridge }} + {{ veth_pair_source_link_name }} + register: res + failed_when: + - res.rc != 0 + - "'already a member of a bridge' not in res.stderr" + changed_when: "'already a member of a bridge' not in res.stderr" + when: veth_pair_plug_into_source | bool + become: true