ansible execute python target with different interpreter

Viewed 27

I have a playbook that has a task remote calling a python program in the name of another user on a remote system. This fails due python interpreter mismatch.

- name: Run another python script on the other host
  become: yes
  become_method: sudo
  become_user: "{{ the_other_user }}" 
  become_flags: " -i"
  shell: |
    Python_Program argument
  register: PythonResult

Unfortunately this fails with (I sanitized IPs and other info)

fatal: [X.X.X.X]: FAILED! => {"changed": false, "module_stderr": "Shared connection to X.X.X.X closed.\r\n", "module_stdout": "Fatal Python error: Py_Initialize: Unable to get the locale encoding\r\n  File \"/path/to/python/libs/for/user/encodings/__init__.py\", line 123\r\n    raise CodecRegistryError,\\\r\n                            ^\r\nSyntaxError: invalid syntax\r\n\r\nCurrent thread 0x00007f9db943b280 (most recent call first):\r\n/bin/sh: line 1: 63510 Aborted                 (core dumped) /usr/libexec/platform-python /var/tmp/ansible-tmp-1663627270.43-7901-177480210535145/AnsiballZ_command.py\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 134}

If I open a session with the "the_other_user" on the remote host I can run the "Python_Program argument" without no issues. I assume there is something to do with different python interpreters from the source ansible vs the remote command but I am not sure how to fix it.

0 Answers
Related