Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible to restart network service

I copy-pasted this from the manual and it fails in my playbook (version 2.0.2):

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

I am getting this error:

"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"}

What is the correct syntax, please?

like image 420
JC- Avatar asked Oct 12 '16 08:10

JC-


People also ask

Why does Ansible hang when I restart the system?

But if you change network configuration before restart, something like IP address, after restart ansible hangs because connection is lost (IP address changed). Won't work if system does not have the "network" service. That applies to recent Fedora and RHEL 8, in particular. There, you can do the equivalent of systemctl restart NetworkManager.

What is the ansible service module?

By default, any Ansible installation comes with the service module, and it is also part of the Ansible-base set of modules. Using the service module, we can manage and control the following service states: We can also configure the service to auto-start or be in a stop state when the server comes up using the enable key.

What is wait_for_connection in Ansible?

If you are a network engineer that has operational Ansible Playbooks that need to reboot devices or take them offline, this module will help you make more programmatic playbooks to handle disconnects. By leveraging wait_for_connection network automation playbooks can look and behave more like playbooks for Linux or Windows hosts.

How do I find the ansible module for Red Hat Ansible?

Red Hat subscribers, select 2.9 in the version selection to the left for the most recent Red Hat release. This module is part of ansible-core and included in all Ansible installations.


2 Answers

Just do like this (@nasr already commented it):

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

But if you change network configuration before restart, something like IP address, after restart ansible hangs because connection is lost (IP address changed).

like image 62
Ikrom Avatar answered Sep 22 '22 03:09

Ikrom


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

does work wonderfully but I feel that using command kinda defeats the purpose of using ansible.

like image 21
JC- Avatar answered Sep 23 '22 03:09

JC-