diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e0e446 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Ansible related +*.retry + +# Junk files +*.swp +*.DS_Store +[Tt]humbs.db + +# Generated templates +generated/ +build/** + +.vagrant diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6c3c229 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2019 Lucas Harms + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..20b8768 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +netplan +========= + +Install and configure netplan. + +Requirements +------------ + +None + +Role Variables +-------------- + +``` +netplan_file: "60_ansible.yaml" +netplan_path: "/etc/netplan" +netplan_config: {} +netplan_enabled: true +netplan_packages: + - netplan.io + - nplan +netplan_renderer: networkd +netplan_version: 2 +netplan_wipe: false +``` + +Dependencies +------------ + +None + +Example Playbook +---------------- + + - hosts: some_servers + vars: + netplan_config: + network: + ethernets: + eno1: + addresses: + - 192.168.1.10/24 + gateway4: 192.168.1.1 + nameservers: + search: [example.com] + addresses: [1.1.1.1, 8.8.8.8] + eno2: + dhcp4: false + dhcp6: false + optional: true + netplan_wipe: true + roles: + - {{ lmickh.netplan }} + +License +------- + +MIT diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..8962352 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,19 @@ +--- +netplan_file: "60_ansible.yaml" +netplan_path: "/etc/netplan" + +# Yaml formatted netplan config (https://netplan.io/design#network-config-format) +netplan_config: {} + +netplan_enabled: true + +netplan_packages: + - netplan.io + - nplan + +# Set defaults for when not defined in the netplan_config +netplan_renderer: networkd +netplan_version: 2 + +# This removes any config not managed by this role +netplan_wipe: false diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..b5b4c64 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,11 @@ +--- +- name: Apply netplan config + become: true + command: netplan apply + listen: netplan apply config + +- name: Generate netplan config + become: true + command: netplan generate + listen: netplan generate config + notify: netplan apply config diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..76b41cc --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,15 @@ +dependencies: [] +galaxy_info: + author: lmickh + description: Install and configure netplan + license: MIT + min_ansible_version: 2.4 + platforms: + - name: Debian + versions: + - stretch + - name: Ubuntu + versions: + - bionic + galaxy_tags: + - networking diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..87fc560 --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,8 @@ +--- +- name: Configure netplan + become: true + template: + src: templates/etc/netplan/config.yaml.j2 + dest: "{{ netplan_path }}/{{ netplan_file}}" + when: netplan_config != {} + notify: netplan generate config diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..a17c265 --- /dev/null +++ b/tasks/install.yml @@ -0,0 +1,8 @@ +--- +- name: Install netplan packages + become: true + apt: + name: netplan_packages + state: present + when: + - ansible_os_family == 'Debain' diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..b7b8c16 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,11 @@ +--- +- import_tasks: install.yml + when: netplan_enabled + +- import_tasks: wipe.yml + when: + - netplan_enabled + - netplan_wipe + +- import_tasks: configure.yml + when: netplan_enabled diff --git a/tasks/wipe.yml b/tasks/wipe.yml new file mode 100644 index 0000000..daa0c9f --- /dev/null +++ b/tasks/wipe.yml @@ -0,0 +1,15 @@ +--- +- name: Collect existing netplan configs + find: + paths: "{{ netplan_path }}" + patterns: "*.yaml,*.yml" + register: netplan_existing_configs + +- name: Wipe existing netplan configs + become: true + file: + path: "{{ item['path'] }}" + state: absent + with_items: "{{ netplan_existing_configs['files'] }}" + when: + - item['path'] != netplan_path + "/" + netplan_file diff --git a/templates/etc/netplan/config.yaml.j2 b/templates/etc/netplan/config.yaml.j2 new file mode 100644 index 0000000..8ee05eb --- /dev/null +++ b/templates/etc/netplan/config.yaml.j2 @@ -0,0 +1,32 @@ +--- +network: + version: {{ netplan_config['network']['version'] | default(netplan_version) }} + renderer: {{ netplan_config['network']['renderer'] | default(netplan_renderer) }} +{% if netplan_config['network']['ethernets'] is defined %} + ethernets: +{{ netplan_config['network']['ethernets'] | to_nice_yaml | indent(width=4,first=true) }} +{% endif %} +{% if netplan_config['network']['wifis'] is defined %} + wifis: +{{ netplan_config['network']['wifis'] | to_nice_yaml | indent(width=4,first=true) }} +{% endif %} +{% if netplan_config['network']['bonds'] is defined %} + bonds: +{{ netplan_config['network']['bonds'] | to_nice_yaml | indent(width=4,first=true) }} +{% endif %} +{% if netplan_config['network']['bridges'] is defined %} + bridges: +{{ netplan_config['network']['bridges'] | to_nice_yaml | indent(width=4,first=true) }} +{% endif %} +{% if netplan_config['network']['tunnels'] is defined %} + tunnels: +{{ netplan_config['network']['tunnels'] | to_nice_yaml | indent(width=4,first=true) }} +{% endif %} +{% if netplan_config['network']['veths'] is defined %} + veths: +{{ netplan_config['network']['veths'] | to_nice_yaml | indent(width=4,first=true) }} +{% endif %} +{% if netplan_config['network']['vlans'] is defined %} + vlans: +{{ netplan_config['network']['vlans'] | to_nice_yaml | indent(width=4,first=true) }} +{% endif %} \ No newline at end of file