diff --git a/ansible-set-static-ip.yml b/ansible-set-static-ip.yml index 60d76b6..f20a42b 100644 --- a/ansible-set-static-ip.yml +++ b/ansible-set-static-ip.yml @@ -28,20 +28,36 @@ - name: Setze statische IP wenn NetworkManager NICHT installiert ist block: - - name: Sichere die aktuelle Netzwerkkonfiguration - copy: - src: /etc/network/interfaces - dest: /etc/network/interfaces.backup - remote_src: true + - name: Get the current DHCP interface name + shell: grep -Po 'iface \K[^ ]+' /etc/network/interfaces | grep -v 'lo' + register: dhcp_interface + changed_when: false + + - name: Set interface name variable + set_fact: + interface_name: "{{ dhcp_interface.stdout_lines[0] }}" + + - name: Configure interface for static IP (rest of the tasks) + ansible.builtin.lineinfile: + path: /etc/network/interfaces + regexp: '^iface {{ interface_name }} inet dhcp' + line: 'iface {{ interface_name }} inet static' + backup: yes + +# - name: Sichere die aktuelle Netzwerkkonfiguration +# copy: +# src: /etc/network/interfaces +# dest: /etc/network/interfaces.backup +# remote_src: true - name: Setze statische IP in /etc/network/interfaces blockinfile: path: /etc/network/interfaces block: | iface {{ interface_name }} inet static - address {{ static_ip }} - netmask {{ netmask }} - gateway {{ gateway }} - dns-nameservers {{ dns_servers | join(' ') }} + address {{ static_ip }} + netmask {{ netmask }} + gateway {{ gateway }} + dns-nameservers {{ dns_servers | join(' ') }} - name: Starte Netzwerk neu command: systemctl restart networking when: dpkg_query_result.rc != 0