How do I set a timeout for an Ansible Runner task?

984 Views Asked by At

In the following hypothetical example, I'm executing sleep for five sec on the remote host via the shell module. I'd like the Ansible Runner to timeout after four seconds if the remote shell process hasn't returned. Is this possible?

    r = ansible_runner.run(inventory=ansible_inventory, host_pattern="all",
        module="shell",
        module_args=("sleep 5"),
        envvars = {
            "ansible_command_timeout": 4  # This doesn't seem to work
        }
    )
1

There are 1 best solutions below

0
gerardw On

This worked for me. envvars is assigned the return of this method:

    def runnerenv(self):
     """ansible runner environment"""
     env = os.environ.copy()
     path = f"{env['PATH']}:{self.venvpath}"
     self.logger.debug(f"Updated PATH {path}")
     env['PATH'] = path
     env['ANSIBLE_TASK_TIMEOUT'] = 60
     return env