--- - name: Update default gateway hosts: all become: true vars: new_gateway: "192.168.1.254" tasks: - name: Replace gateway line in interfaces file lineinfile: path: /etc/network/interfaces regexp: '^(\s*)gateway\s+' line: " gateway {{ new_gateway }}" backrefs: yes - name: Apply new default route immediately command: ip route replace default via {{ new_gateway }}