Gova
Gova

Reputation: 245

How to run a command that will reset the network interfaces in ansible?

I want to run a command on remote machine. The command will reset the network interfaces. How to run this in ansible playbook

- name: Execute config command
  sudo: yes
  shell: "mycommand"
  async: 0
  poll: 0
  ignore_errors: true

The above task is not working consistently. Even I tried with async: 300, the same inconsistency is being observed.

Upvotes: 1

Views: 1883

Answers (1)

Bruce P
Bruce P

Reputation: 20769

You're likely running into a situation similar to the one I describe in this question. Depending on the command you are running (mycommand) the network connection is likely dropping very quickly, causing Ansible to think that the connection was dropped unexpectedly. When this happens it will cause Ansible to treat it as an error.

You likely want to modify mycommand to include a sleep for a few seconds before the reset occurs, and continue using async:0 and poll:0. This will give Ansible enough time to launch mycommand into the background and cleanly disconnect from the server without error before the server resets the network connection.

Depending on what your next task is you may also want to include a wait_for task that runs via local_action to ensure Ansible waits for this network reset to complete before attempting any other tasks.

Upvotes: 1

Related Questions