I'm currently developping a network configuration role used by Ansible to customize our fresh new virtual machine that came from our Debian 11 template.
The following role changes from DHCP to static interface configuration and then restart the networking service.For testing purpose i set IP/netmask/gateway as extra vars (and it's dynamically changed depends on the server i have to use my role)
- name: ens3 reconfiguration
ansible.builtin.template:
src: interfaces.j2
dest: /etc/network/interfaces
- name: Restart networking.service
ansible.builtin.service:
name: networking
state: restarted
Here's the content of interfaces.j2 for understanding.
# The primary network interface
auto ens3
iface ens3 inet static
address {{ ens3_ip }}/{{ ens3_netmask }}
gateway {{ ens3_gateway }}
dns-nameservers X.X.X.X
dns-search my_domain.net
The problem here is, as the network interface is dhcp configured, it has for example 10.0.0.1, i reconfigure it with 10.0.0.50, then Ansible move to the restart networking.service task, and gets hang forever...
**From an Ansible perspective, is it possible to dynamically reconnect to the host with the new IP ? or maybe, bypass the execution result of the restart networking.service ? **