Add 'teardown' action to veth-pair role
This commit is contained in:
parent
56b0019258
commit
24f47ca36a
@ -2,7 +2,11 @@ Veth Pair
|
|||||||
=========
|
=========
|
||||||
|
|
||||||
This role creates a veth pair. It will plug one end into the specified OVS
|
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
|
Requirements
|
||||||
------------
|
------------
|
||||||
@ -24,3 +28,5 @@ Role Variables
|
|||||||
- `veth_pair_plug_into_source`: Whether or not to plug the source end of the
|
- `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
|
veth pair into a Linux bridge. If enabled, `veth_pair_source_bridge` must
|
||||||
also be specified. Default is `false`.
|
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`.
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
---
|
---
|
||||||
# Whether or not to plug the source end of the veth pair into a Linux bridge.
|
# Whether or not to plug the source end of the veth pair into a Linux bridge.
|
||||||
veth_pair_plug_into_source: false
|
veth_pair_plug_into_source: false
|
||||||
|
# Whether the veth pair should be present or absent.
|
||||||
|
veth_pair_state: present
|
||||||
|
33
ansible/roles/veth-pair/tasks/absent.yml
Normal file
33
ansible/roles/veth-pair/tasks/absent.yml
Normal file
@ -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
|
@ -1,29 +1,8 @@
|
|||||||
---
|
---
|
||||||
- name: Create veth pair
|
- name: Ensure veth pair is absent
|
||||||
command: >-
|
include_tasks: absent.yml
|
||||||
ip link add dev {{ veth_pair_ovs_link_name }}
|
when: veth_pair_state == 'absent'
|
||||||
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
|
- name: Ensure veth pair is present
|
||||||
openvswitch_port:
|
include_tasks: present.yml
|
||||||
bridge: "{{ veth_pair_ovs_bridge }}"
|
when: veth_pair_state != 'absent'
|
||||||
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
|
|
||||||
|
29
ansible/roles/veth-pair/tasks/present.yml
Normal file
29
ansible/roles/veth-pair/tasks/present.yml
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user