简体   繁体   中英

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?

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM