7373from ansible .module_utils .six import with_metaclass
7474from ansible .module_utils .parsing .convert_bool import boolean
7575
76+ import ansible_mitogen .utils
7677import mitogen .core
7778
7879
7980LOG = logging .getLogger (__name__ )
8081
82+ if ansible_mitogen .utils .ansible_version [:2 ] >= (2 , 19 ):
83+ _FALLBACK_INTERPRETER = ansible .executor .interpreter_discovery ._FALLBACK_INTERPRETER
84+ elif ansible_mitogen .utils .ansible_version [:2 ] >= (2 , 17 ):
85+ _FALLBACK_INTERPRETER = u'/usr/bin/python3'
86+ else :
87+ _FALLBACK_INTERPRETER = u'/usr/bin/python'
88+
8189
8290def run_interpreter_discovery_if_necessary (s , task_vars , action , rediscover_python ):
8391 """
@@ -107,7 +115,9 @@ def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_pyth
107115 # blow away the discovered_interpreter_config cache and rediscover
108116 del task_vars ['ansible_facts' ][discovered_interpreter_config ]
109117
110- if discovered_interpreter_config not in task_vars ['ansible_facts' ]:
118+ try :
119+ s = task_vars ['ansible_facts' ][discovered_interpreter_config ]
120+ except KeyError :
111121 action ._mitogen_discovering_interpreter = True
112122 # fake pipelining so discover_interpreter can be happy
113123 action ._connection .has_pipelining = True
@@ -121,8 +131,6 @@ def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_pyth
121131 # cache discovered interpreter
122132 task_vars ['ansible_facts' ][discovered_interpreter_config ] = s
123133 action ._connection .has_pipelining = False
124- else :
125- s = task_vars ['ansible_facts' ][discovered_interpreter_config ]
126134
127135 # propagate discovered interpreter as fact
128136 action ._discovered_interpreter_key = discovered_interpreter_config
@@ -144,9 +152,9 @@ def parse_python_path(s, task_vars, action, rediscover_python):
144152 s = 'auto'
145153
146154 s = run_interpreter_discovery_if_necessary (s , task_vars , action , rediscover_python )
147- # if unable to determine python_path, fallback to '/usr/bin/python'
148155 if not s :
149- s = '/usr/bin/python'
156+ s = _FALLBACK_INTERPRETER
157+ # raise ValueError("Interpreter discovery failed, got: %r", s)
150158
151159 return ansible .utils .shlex .shlex_split (s )
152160
@@ -715,9 +723,6 @@ def port(self):
715723
716724 def python_path (self , rediscover_python = False ):
717725 s = self ._host_vars .get ('ansible_python_interpreter' )
718- # #511, #536: executor/module_common.py::_get_shebang() hard-wires
719- # "/usr/bin/python" as the default interpreter path if no other
720- # interpreter is specified.
721726 return parse_python_path (
722727 s ,
723728 task_vars = self ._task_vars ,
0 commit comments