From 135847d74507215fe44a32c4c271784d5d779cad Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 23 Jun 2025 09:29:29 -0700 Subject: [PATCH] init commit --- README.md | 5 +++ defaults/main.yaml | 10 ++++++ files/interfaces | 9 ++++++ tasks/create_bridge.yaml | 70 ++++++++++++++++++++++++++++++++++++++++ tasks/main.yaml | 42 ++++++++++++++++++++++++ templates/bridge.j2 | 6 ++++ templates/inventory.j2 | 9 ++++++ 7 files changed, 151 insertions(+) create mode 100644 README.md create mode 100644 defaults/main.yaml create mode 100644 files/interfaces create mode 100644 tasks/create_bridge.yaml create mode 100644 tasks/main.yaml create mode 100644 templates/bridge.j2 create mode 100644 templates/inventory.j2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..a61e185 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +This is a playbook to set up a network bridge. + +I wrote it for the VM party. + +It will set up a bridge. \ No newline at end of file diff --git a/defaults/main.yaml b/defaults/main.yaml new file mode 100644 index 0000000..eb54b94 --- /dev/null +++ b/defaults/main.yaml @@ -0,0 +1,10 @@ +--- + + +create_inventory: false + +run_stage_two: false + +public_deploy: true + +... \ No newline at end of file diff --git a/files/interfaces b/files/interfaces new file mode 100644 index 0000000..54b3a2d --- /dev/null +++ b/files/interfaces @@ -0,0 +1,9 @@ +# This file describes the network interfaces available on your system +# and how to activate them. For more information, see interfaces(5). + +source /etc/network/interfaces.d/* + +# The loopback network interface +auto lo +iface lo inet loopback + diff --git a/tasks/create_bridge.yaml b/tasks/create_bridge.yaml new file mode 100644 index 0000000..52c0c7b --- /dev/null +++ b/tasks/create_bridge.yaml @@ -0,0 +1,70 @@ +--- + +# remove cosmos VPN +- name: remove cosmos openvpn profile + shell: | + systemctl stop openvpn-client@cosmos-client.service + systemctl disable openvpn-client@cosmos-client.service + systemctl daemon-reload + +- name: Install Packages + apt: + name: + - bridge-utils + state: present + +- name: get current interface name + shell: cat /etc/network/interfaces | grep iface | grep -v loopback | awk '{print $2}' + register: current_iface_output + +- name: set current_iface + set_fact: + current_iface: '{{ current_iface_output.stdout_lines[0] }}' + +- name: display interface name + debug: + msg: "Current interface is {{ current_iface }}" + +- name: update interfaces file + block: + + - name: backup original interfaces file + shell: "cp /etc/network/interfaces /opt/interfaces-backup" + + - name: Comment out allow-hotplug line for main interface + lineinfile: + dest: /etc/network/interfaces + regexp: '^#? *allow-hotplug {{ current_iface }}' + line: '# allow-hotplug {{ current_iface }}' + backrefs: yes + + - name: Comment out iface line for main interface + lineinfile: + dest: /etc/network/interfaces + regexp: '^#? *iface {{ current_iface }} inet dhcp' + line: '# iface {{ current_iface }} inet dhcp' + backrefs: yes + +- name: Create new bridge interface file + template: + src: bridge.j2 + dest: /etc/network/interfaces.d/{{ bridge_name }} + +- name: restart networking + shell: | + systemctl daemon-reload + systemctl restart networking + +- name: show bridge + shell: | + brctl show + bridge link + register: bridge_status + +- name: show bridge status + debug: + msg: + - "{{ bridge_status.stdout_lines }}" + + +... \ No newline at end of file diff --git a/tasks/main.yaml b/tasks/main.yaml new file mode 100644 index 0000000..48ea622 --- /dev/null +++ b/tasks/main.yaml @@ -0,0 +1,42 @@ +--- +# this role installs the bridge and reboots. +# optionally it creates a new inventory file on the jenkins host +# if the appropriate variable is declared + +# check interfaces +- name: check interfaces for vmpbr0 + shell: ip -o -4 ad + register: ip_ad_output + +- name: install bridge + when: '"vmpbr0" not in ip_ad_output.stdout' + include_tasks: create_bridge.yaml + +# Create new inventory file +- name: create inventory file + block: + - name: get bridge IP + shell: ip -o -4 ad li | grep {{ bridge_name }} | awk '{print $4}' | cut -d '/' -f 1 + register: bridge_ip_output + + - name: set bridge_address variable + set_fact: + bridge_address: "{{ bridge_ip_output.stdout_lines[0] }}" + + - name: show bridge address + debug: + msg: + - "{{ bridge_address }}" + + - name: Network Bridge - Create new inventory file on jenkins server + delegate_to: localhost + template: + src: inventory.j2 + dest: "/var/jenkins_home/ansible/.inv/inventory-{{ STAGE_TWO_HASH }}.yml" + +- name: Reboot + when: '"vmpbr0" not in ip_ad_output.stdout' + command: "reboot now" + ignore_errors: yes + +... \ No newline at end of file diff --git a/templates/bridge.j2 b/templates/bridge.j2 new file mode 100644 index 0000000..8531e06 --- /dev/null +++ b/templates/bridge.j2 @@ -0,0 +1,6 @@ +## DHCP ip config file for {{ bridge_name }} ## +auto {{ bridge_name }} + +# Bridge setup +iface {{ bridge_name }} inet dhcp + bridge_ports {{ current_iface }} \ No newline at end of file diff --git a/templates/inventory.j2 b/templates/inventory.j2 new file mode 100644 index 0000000..1137695 --- /dev/null +++ b/templates/inventory.j2 @@ -0,0 +1,9 @@ +all: + hosts: + {{ bridge_address }}: + ansible_user: root + vars: + ansible_connection: ssh + ansible_ssh_private_key_file: /var/jenkins_home/jenkins_key + +# host_ip;{{ bridge_address }}