Ansible:在Ubuntu上重启网络

时间:2015-02-25 09:08:05

标签: ubuntu networking ubuntu-12.04 ansible

如果您需要在Ubuntu服务器上播放中间重启网络(在我的情况下为12.04),则无法使用service

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

以下适用于命令行,但是使用Ansible(1.8.4)它会将您锁定:

command: ifdown eth0 && ifup eth0

ifdown关闭界面,但ifup无法运行

如何重启界面?

2 个答案:

答案 0 :(得分:8)

解决方案是在新shell中运行命令:

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

您也可以使用shell模块:

shell: "ifdown eth0 && ifup eth0"

答案 1 :(得分:4)

我建议在ifdown和ifup之间等待1秒,然后独立于ifdown的退出代码运行ifup。此外,您可能不希望每次运行此ansible playbook时都运行重置网络,因此when条件将阻止此操作:

- name: Restart all network interfaces except loopback device
  shell: "ifdown --exclude=lo -a; sleep 1; ifup --exclude=lo -a"
  when: configure_lxc_bridge|changed
相关问题