diff --git a/defaults/main.yml b/defaults/main.yml index 7a3445e..b088e67 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -25,6 +25,11 @@ sysctl_custom_options: [] # sysctl_reload: '{{ sysctl_opts_reload }}' # sysctlfile_state: '{{ sysctl_custom_file_state }}' +ubuntu_configure_additional_interfaces: true +ubuntu_netplan_interfaces: + - name: "enp8s0" + dhcp4: true + mtu: "{{ ansible_enp8s0.mtu }}" disable_ipv6: false ipv6_sysctl_value: 1 ipv6_sysctl_file: /etc/sysctl.d/10-ipv6-disable.conf diff --git a/handlers/main.yml b/handlers/main.yml index 27474e0..c2b5383 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,4 @@ --- -# handlers file for ansible-role-template \ No newline at end of file +- name: Netplan Apply + ansible.builtin.command: + cmd: netplan Apply diff --git a/tasks/main.yml b/tasks/main.yml index 2d741f0..41ded33 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -9,6 +9,9 @@ ansible.builtin.import_tasks: timezone.yml - name: Sysctl kernel parameters ansible.builtin.import_tasks: sysctl.yml +- name: Additional network interface(s) + ansible.builtin.import_tasks: + - network-interfaces.yml - name: Create a directory that will contain the local generated certificates ansible.builtin.import_tasks: pki_dir.yml - name: Self signed certificates waiting for the letsencrypt ones diff --git a/tasks/network-interfaces.yml b/tasks/network-interfaces.yml new file mode 100644 index 0000000..d07fe7a --- /dev/null +++ b/tasks/network-interfaces.yml @@ -0,0 +1,30 @@ +--- +- name: network-interfaces | Manage additional network interfaces, Ubuntu style + tags: + - network_interface + - networking + when: + - ansible_distribution == 'Ubuntu' + - ubuntu_configure_additional_interfaces + block: + - name: network-interfaces | Check if netplan is in use + ansible.builtin.stat: + path: /etc/netplan + register: netplan_in_use + - name: network-interfaces | Check if additional interfaces have been defined + ansible.builtin.set_fact: + net_ints: "{% for i in ansible_interfaces %}{% if i != ansible_lo.device or i != ansible_default_ipv4.interface %}{{ i }}{% if not loop.last %},{% endif %}{% endfor %}" + when: netplan_in_use.stat.isdir + - name: network-interfaces | Create a dictionary of additional interfaces + ansible.builtin.set_fact: + new_ints: "[{% for i in ansible_interfaces %}{% if i != ansible_lo.device or i != ansible_default_ipv4.interface %}{{ i }}{% if not loop.last %},{% endif %}{% endfor %}]" + when: net_ints is not defined or net_ints == None or net_ints | length == 0 + - name: network-interfaces | Install the network interface file + ansible.builtin.template: + src: 70-ansible.yaml.j2 + dest: /etc/netplan/70-ansible.yaml + owner: root + group: root + mode: "0644" + when: net_ints is not defined or net_ints == None or net_ints | length == 0 + notify: Netplan Apply diff --git a/templates/70-ansible.yaml.j2 b/templates/70-ansible.yaml.j2 new file mode 100644 index 0000000..fa1a437 --- /dev/null +++ b/templates/70-ansible.yaml.j2 @@ -0,0 +1,7 @@ +network: + version: 2 + ethernets: +{% for int in ubuntu_netplan_interfaces %} + {{ int.name }}: + dhcp4: {{ int.dhcp4 }} + mtu: {{ int.mtu }}