ansible重启网络服务

时间:2016-10-12 08:43:38

标签: ansible ansible-playbook

我从manual复制粘贴,但在我的剧本(版本2.0.2)中失败了:

- service: name=network state=restarted args=eth0

我收到此错误:

"msg": "Failed to stop eth0.service: Unit eth0.service not loaded.\nFailed to start eth0.service: Unit eth0.service failed to load: No such file or directory.\n"}

请问正确的语法是什么?

4 个答案:

答案 0 :(得分:2)

- command: /etc/init.d/network restart

确实很有效,但我觉得使用command有点打败了使用ansible的目的。

答案 1 :(得分:1)

根据 Ansible 2.7.8 。您必须进行以下更改才能重新启动网络。

  

注意:我在Ubuntu 16.04上尝试过此操作

方案1:仅网络重启

- name: Restarting Network to take effect new IP Address
  become: yes 
  service:
    name: networking
    state: restarted

方案2:刷新界面,然后重新启动网络

- name: Flushing Interface
  become: yes
  shell: sudo ip addr flush "{{net_interface}}"

- name: Restarting Network
  become: yes 
  service:
    name: networking
    state: restarted
  

注意:确保已配置net_interface,然后将其导入执行Ansible任务的文件中。

输出

请在下面的屏幕上找到我收到的输出。

enter image description here

答案 2 :(得分:0)

只要这样做(@nasr已经评论过):

- name: Restart network
  service:
    name: network
    state: restarted

但是,如果您在重启之前更改网络配置,则重启后ansible会挂起IP地址之类的消息,因为连接丢失(IP地址已更改)。

答案 3 :(得分:0)

有一种做对事情的方法。

  

tasks.yml

- name: net configuration step 1
  debug:
    msg: we changed some files
  notify: restart systemd-networkd

- name: net configuration step 2
  debug:
    msg: do some more work, but restart net services only once
  notify: restart systemd-networkd
  

handlers.yml

- name: restart systemd-networkd
  systemd:
    name: systemd-networkd
    state: restarted
  async: 120
  poll: 0
  register: net_restarting

- name: check restart systemd-networkd status
  async_status:
    jid: "{{ net_restarting.ansible_job_id }}"
  register: async_poll_results
  until: async_poll_results.finished
  retries: 30
  listen: restart systemd-networkd