diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..9d132a9 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,6 @@ +--- + +- name: networkd | Reload configuration + systemd: + name: systemd-networkd + state: restarted diff --git a/tasks/deploy_configs.yml b/tasks/deploy_configs.yml new file mode 100644 index 0000000..344cfa1 --- /dev/null +++ b/tasks/deploy_configs.yml @@ -0,0 +1,34 @@ +--- + +- name: networkd | Deploy .link configs + template: + src: networkd.j2 + dest: "/etc/systemd/network/{{ item.priority }}-{{ item.name }}.link" + mode: 644 + owner: root + group: root + with_items: "{{ networkd.link | default([]) }}" + register: networkd_deployed_link + notify: networkd | Reload configuration + +- name: networkd | Deploy .netdev configs + template: + src: networkd.j2 + dest: "/etc/systemd/network/{{ item.priority }}-{{ item.name }}.netdev" + mode: 644 + owner: root + group: root + with_items: "{{ networkd.netdev | default([]) }}" + register: networkd_deployed_netdev + notify: networkd | Reload configuration + +- name: networkd | Deploy .network configs + template: + src: networkd.j2 + dest: "/etc/systemd/network/{{ item.priority }}-{{ item.name }}.network" + mode: 644 + owner: root + group: root + with_items: "{{ networkd.network | default([]) }}" + register: networkd_deployed_network + notify: networkd | Reload configuration diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..a0c84cd --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,22 @@ +--- + +- import_tasks: deploy_configs.yml + +- import_tasks: remove_unmanaged.yml + +- name: networkd | Deploy resolv.conf + template: + src: resolv.conf.j2 + dest: /etc/resolv.conf + mode: 644 + owner: root + group: root + +- name: networkd | Enable and start service + systemd: + name: systemd-networkd + state: started + enabled: yes + +- name: networkd | Run handlers now + meta: flush_handlers diff --git a/tasks/remove_unmanaged.yml b/tasks/remove_unmanaged.yml new file mode 100644 index 0000000..8f50c62 --- /dev/null +++ b/tasks/remove_unmanaged.yml @@ -0,0 +1,20 @@ +--- +- name: networkd | Collect file list in network dir + find: + path: /etc/systemd/network + hidden: yes + register: networkd_found_files + check_mode: no + changed_when: false + +- name: networkd | Remove unmanaged files + file: + path: "/etc/systemd/network/{{ item.path | basename }}" + state: absent + with_items: + - "{{ networkd_found_files.files }}" + when: + - (item.path) not in ( networkd_deployed_link | json_query('results[].invocation.module_args.dest') | default([]) ) + - (item.path) not in ( networkd_deployed_netdev | json_query('results[].invocation.module_args.dest') | default([]) ) + - (item.path) not in ( networkd_deployed_network | json_query('results[].invocation.module_args.dest') | default([]) ) + notify: networkd | Reload configuration diff --git a/templates/networkd.j2 b/templates/networkd.j2 new file mode 100644 index 0000000..2c3d14a --- /dev/null +++ b/templates/networkd.j2 @@ -0,0 +1,12 @@ +# {{ ansible_managed }} +{% for section in item.content %} +{% for section_name, section_params in section.items() %} + +[{{ section_name }}] +{% for item in section_params %} +{% for key, value in item.items() %} +{{ key }}={{ value }} +{% endfor %} +{% endfor %} +{% endfor %} +{% endfor %} diff --git a/templates/resolv.conf.j2 b/templates/resolv.conf.j2 new file mode 100644 index 0000000..f73637b --- /dev/null +++ b/templates/resolv.conf.j2 @@ -0,0 +1,4 @@ +# {{ ansible_managed }} +{% for item in networkd_resolver %} +{{ item }} +{% endfor %}