Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: restart networking on Ubuntu

If you need to restart the networking in the middle of a play on an Ubuntu server (12.04 in my case) you can't use service:

# service networking restart 
stop: Job failed while stopping
start: Job is already running: networking

The following works on the command line, but with Ansible (1.8.4) it locks you out:

command: ifdown eth0 && ifup eth0

ifdown takes down the interface, but ifup doesn't run

How to restart the interface?

like image 311
jonatan Avatar asked Feb 25 '15 09:02

jonatan


People also ask

How restart ansible service in Linux?

Use systemctl restart ansible-tower to restart services on clustered environments instead. Also you must restart each cluster node for certain changes to persist as opposed to a single node for a localhost install.


2 Answers

The solution is to run the command in a new shell:

command: bash -c "ifdown eth0 && ifup eth0"

You can also use the shell module:

shell: "ifdown eth0 && ifup eth0"
like image 55
jonatan Avatar answered Oct 22 '22 05:10

jonatan


I would recommend to wait 1 sec between ifdown and ifup and run ifup independent of exit code from ifdown. Also you probably not want run reset network everytime you run this ansible playbook, so a when condition will prevent this:

- name: Restart all network interfaces except loopback device
  shell: "ifdown --exclude=lo -a; sleep 1; ifup --exclude=lo -a"
  when: configure_lxc_bridge|changed
like image 23
panticz Avatar answered Oct 22 '22 05:10

panticz