diff --git a/ansible/plugins/action/onie.py b/ansible/plugins/action/onie.py index bedaaa70833..ad060f56bed 100644 --- a/ansible/plugins/action/onie.py +++ b/ansible/plugins/action/onie.py @@ -22,6 +22,7 @@ def run(self, tmp=None, task_vars=None): _install = boolean(self._task.args.get('install', 'no')) _url = self._task.args.get('url', None) _timeout = self._task.args.get('timeout', None) + _nretry = int(self._task.args.get('retry', 3)) if _timeout is None: _timeout = 300 @@ -43,7 +44,8 @@ def run(self, tmp=None, task_vars=None): host=_host, url=_url, install=_install, - timeout=_timeout) + timeout=_timeout, + retry=_nretry) return result diff --git a/ansible/plugins/connection/onie.py b/ansible/plugins/connection/onie.py index 3cf430fe7ae..1f8136d3f0d 100644 --- a/ansible/plugins/connection/onie.py +++ b/ansible/plugins/connection/onie.py @@ -73,6 +73,7 @@ def exec_command(self, *args, **kwargs): self.host = kwargs['host'] self.url = kwargs['url'] self.install = kwargs['install'] + self.nretry = kwargs['retry'] self._build_command() @@ -101,11 +102,18 @@ def exec_command(self, *args, **kwargs): client.sendline('onie-discovery-stop') client.expect(prompts) stdout += client.before - client.sendline("onie-nos-install %s" % self.url) - i = client.expect(["Installed SONiC base image SONiC-OS successfully"] + prompts) - stdout += client.before - if i != 0: - raise AnsibleError("Failed to install sonic image. %s" % stdout) + attempt = 0 + while attempt < self.nretry: + client.sendline("onie-nos-install %s" % self.url) + i = client.expect(["Installed SONiC base image SONiC-OS successfully"] + prompts) + stdout += client.before + if i == 0: + break + elif i == 1: + attempt += 1 + self._display.vvv("Installation fails, retry %d..." % attempt, host=self.host) + else: + raise AnsibleError("Failed to install sonic image. %s" % stdout) self._display.vvv("SONiC installed.", host=self.host) # for some platform, e.g., DELL S6000, it will do hard reboot, # which will not give EOF