From e7602bd91076d658f6f32304b024d745b40c8be1 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 31 Aug 2022 12:39:41 -0500 Subject: [PATCH] Refactor network role --- roles/network/defaults/main.yml | 27 +++++++++++ roles/network/meta/main.yml | 60 ------------------------- roles/network/tasks/Ubuntu.yaml | 5 +-- roles/network/tasks/netplan.yml | 15 +++++-- roles/network/templates/netplan.yaml.j2 | 38 ++++++++++++++++ roles/network/vars/netplan.yaml | 15 ------- 6 files changed, 78 insertions(+), 82 deletions(-) delete mode 100644 roles/network/meta/main.yml create mode 100644 roles/network/templates/netplan.yaml.j2 delete mode 100644 roles/network/vars/netplan.yaml diff --git a/roles/network/defaults/main.yml b/roles/network/defaults/main.yml index ed97d53..ccafadf 100644 --- a/roles/network/defaults/main.yml +++ b/roles/network/defaults/main.yml @@ -1 +1,28 @@ --- +# network_netplan_version: 2 +# network_netplan_renderer: networkd +network_netplan_etc_path: /etc/netplan +network_netplan_config_path: "{{ network_netplan_etc_path }}/ansible.yaml" +network_netplan_default_config_path: "{{ network_netplan_etc_path }}/01-netcfg.yaml" +# network_netplan_default_config_state: absent + +network_interfaces: + - name: eth0 + inet4: + dhcp: false + address: + - "{{ ansible_default_ipv4.address }}/{{ (ansible_default_ipv4.network + '/' + ansible_default_ipv4.netmask) | ansible.utils.ipaddr('prefix') }}" + gateway: "{{ ansible_default_ipv4.gateway }}" + inet6: + dhcp: false + address: + - "{{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }}" + gateway: "{{ ansible_default_ipv6.gateway }}" + +network_dns_nameservers: + - 8.8.8.8 + - 1.1.1.1 + - 9.9.9.9 + - 8.8.4.4 + - 1.0.0.1 + - 149.112.112.112 diff --git a/roles/network/meta/main.yml b/roles/network/meta/main.yml deleted file mode 100644 index 5d50bf4..0000000 --- a/roles/network/meta/main.yml +++ /dev/null @@ -1,60 +0,0 @@ -galaxy_info: - author: your name - description: your description - company: your company (optional) - - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value - # issue_tracker_url: http://example.com/issue/tracker - - # Some suggested licenses: - # - BSD (default) - # - MIT - # - GPLv2 - # - GPLv3 - # - Apache - # - CC-BY - license: license (GPLv2, CC-BY, etc) - - min_ansible_version: 2.4 - - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # Optionally specify the branch Galaxy will use when accessing the GitHub - # repo for this role. During role install, if no tags are available, - # Galaxy will use this branch. During import Galaxy will access files on - # this branch. If Travis integration is configured, only notifications for this - # branch will be accepted. Otherwise, in all cases, the repo's default branch - # (usually master) will be used. - #github_branch: - - # - # Provide a list of supported platforms, and for each platform a list of versions. - # If you don't wish to enumerate all versions for a particular platform, use 'all'. - # To view available platforms and versions (or releases), visit: - # https://galaxy.ansible.com/api/v1/platforms/ - # - # platforms: - # - name: Fedora - # versions: - # - all - # - 25 - # - name: SomePlatform - # versions: - # - all - # - 1.0 - # - 7 - # - 99.99 - - galaxy_tags: [] - # List tags for your role here, one per line. A tag is a keyword that describes - # and categorizes the role. Users find roles by searching for tags. Be sure to - # remove the '[]' above, if you add tags to this list. - # - # NOTE: A tag is limited to a single word comprised of alphanumeric characters. - # Maximum 20 tags per role. - -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. \ No newline at end of file diff --git a/roles/network/tasks/Ubuntu.yaml b/roles/network/tasks/Ubuntu.yaml index 430a526..6c23b66 100644 --- a/roles/network/tasks/Ubuntu.yaml +++ b/roles/network/tasks/Ubuntu.yaml @@ -1,6 +1,3 @@ --- -- name: Include netplan vars - include_vars: netplan.yaml - - name: Include netplan tasks - include_tasks: netplan.yml + ansible.builtin.include_tasks: netplan.yml diff --git a/roles/network/tasks/netplan.yml b/roles/network/tasks/netplan.yml index a9a228a..9d052f9 100644 --- a/roles/network/tasks/netplan.yml +++ b/roles/network/tasks/netplan.yml @@ -1,9 +1,18 @@ --- +- name: Remove default netplan configuration + ansible.builtin.file: + path: "{{ network_netplan_default_config_path }}" + state: "{{ network_netplan_default_config_state | default('absent') }}" + owner: root + group: root + mode: 0644 + notify: netplan apply + - name: Configure netplan - copy: - dest: /etc/netplan/01-netcfg.yaml + ansible.builtin.template: + dest: "{{ network_netplan_config_path }}" + src: netplan.yaml.j2 owner: root group: root mode: '0644' - content: "{{ network_netplan_config | to_nice_yaml }}" notify: netplan apply diff --git a/roles/network/templates/netplan.yaml.j2 b/roles/network/templates/netplan.yaml.j2 new file mode 100644 index 0000000..a7ecacf --- /dev/null +++ b/roles/network/templates/netplan.yaml.j2 @@ -0,0 +1,38 @@ +--- +network: + version: "{{ network_netplan_version | default(2) }}" + renderer: "{{ network_netplan_renderer | default("networkd") }}" +{% if network_interfaces is defined and network_interfaces | length %} + ethernets: +{% for iface in network_interfaces %} + {{ iface['name'] }}: +{% if iface['inet4']['dhcp'] is defined %} + dhcp4: "{{ iface['inet4']['dhcp'] | ternary('yes', 'no') }}" +{% endif %} +{% if iface['inet4']['dhcp'] is defined %} + dhcp6: "{{ iface['inet6']['dhcp'] | ternary('yes', 'no') }}" +{% endif %} +{% if iface['inet4']['address'] is defined or iface['inet6']['address'] is defined %} + addresses: +{% for ip in iface['inet4']['address'] | default([]) %} + - {{ ip }} +{% endfor %} +{% for ip in iface['inet6']['address'] | default([]) %} + - {{ ip }} +{% endfor %} +{% endif %} +{% if iface['inet4']['gateway'] is defined %} + gateway4: "{{ iface['inet4']['gateway'] }}" +{% endif %} +{% if iface['inet6']['gateway'] is defined %} + gateway6: "{{ iface['inet6']['gateway'] }}" +{% endif %} +{% if network_dns_nameservers is defined %} + nameservers: + addresses: +{% for ip in network_dns_nameservers %} + - {{ ip }} +{% endfor %} +{% endif %} +{% endfor %} +{% endif %} diff --git a/roles/network/vars/netplan.yaml b/roles/network/vars/netplan.yaml deleted file mode 100644 index 081b47c..0000000 --- a/roles/network/vars/netplan.yaml +++ /dev/null @@ -1,15 +0,0 @@ -network_netplan_config: - network: - version: 2 - renderer: networkd - ethernets: - eth0: - dhcp4: 'no' - dhcp6: 'no' - addresses: - - "{{ ansible_default_ipv4.address }}/{{ (ansible_default_ipv4.network + '/' + ansible_default_ipv4.netmask) | ansible.utils.ipaddr('prefix') }}" - - "{{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }}" - gateway4: "{{ ansible_default_ipv4.gateway }}" - gateway6: "{{ ansible_default_ipv6.gateway }}" - nameservers: - addresses: "{{ dns_servers }}"