[Python] How do I set Ansible python interpreter on for remote Windows host?

Discussão em 'Python' iniciado por Stack, Agosto 16, 2025.

  1. Stack

    Stack Membro Participativo

    Ansible fails to find a python interpreter on my remote Windows host. To try to avoid this problem, I want to dynamically tell Ansible where the interpreter on the remote host is.

    Here is the code I have tried:

    ---
    - name: My playbook
    hosts: all

    tasks:
    - name: Verify python installed
    win_shell: python --version
    register: result

    - debug:
    msg: "{{ result.stdout }}"

    - name: Find python binary
    win_shell: (get-command python).Source
    register: result

    - debug:
    msg: "{{ result.stdout }}"

    - name: Set where python interpreter is
    set_fact:
    ansible_python_interpretor: "{{ result.stdout_lines[0] }}"

    - name: Create tmp directory
    file:
    path: C:\tmp
    state: directory


    When I run this playbook, I get the following output:

    TASK [Verify python installed] ************************************************************************************************************
    changed: [<remote IP>]

    TASK [debug] ******************************************************************************************************************************
    ok: [<remote IP>] => {
    "msg": "Python 3.10.4\r\n"
    }

    TASK [Find python binary] *****************************************************************************************************************
    changed: [<remote IP>]

    TASK [debug] ******************************************************************************************************************************
    ok: [<remote IP>] => {
    "msg": "C:\\Python310\\python.exe\r\n"
    }

    TASK [Set where python interpreter is] ****************************************************************************************************
    ok: [<remote IP>]

    TASK [Create tmp directory] ***************************************************************************************************************
    [WARNING]: No python interpreters found for host <remote IP> (tried ['python3.10', 'python3.9', 'python3.8', 'python3.7', 'python3.6',
    'python3.5', '/usr/bin/python3', '/usr/libexec/platform-python', 'python2.7', 'python2.6', '/usr/bin/python', 'python'])
    fatal: [<remote IP>]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "module_stderr": "Exception calling \"Create\" with \"1\" argument(s): \"At line:4 char:21\r\n+ def _ansiballz_main():\r\n+ ~\r\nAn expression was expected after '('.\r\nAt line:8 char:19\r\n+ os.getcwd()\r\n+ ~\r\nAn expression was expected after '('.\r\nAt line:20 char:27\r\n+ except (AttributeError, OSError):\r\n+ ~\r\nMissing argument in parameter list.\r\nAt line:22 char:29\r\n+ excludes = set(('', '.', scriptdir))\r\n+ ~\r\nMissing expression after ','.\r\nAt line:22 char:30\r\n+ excludes = set(('', '.', scriptdir))\r\n+ ~~~~~~~~~\r\nUnexpected token 'scriptdir' in expression or statement.\r\nAt line:22 char:29\r\n+ excludes = set(('', '.', scriptdir))\r\n+ ~\r\nMissing closing ')' in expression.\r\nAt line:22 char:39\r\n+ excludes = set(('', '.', scriptdir))\r\n+ ~\r\nUnexpected token ')' in expression or statement.\r\nAt line:22 char:40\r\n+ excludes = set(('', '.', scriptdir))\r\n+ ~\r\nUnexpected token ')' in expression or statement.\r\nAt line:29 char:7\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nMissing '(' after 'if' in if statement.\r\nAt line:29 char:30\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nMissing expression after ','.\r\nNot all parse errors were reported. Correct the reported errors and try again.\"\r\nAt line:10 char:1\r\n+ $exec_wrapper = [ScriptBlock]::Create($split_parts[0])\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : NotSpecified: :)) [], MethodInvocationException\r\n + FullyQualifiedErrorId : ParseException\r\n \r\nThe expression after '&' in a pipeline element produced an object that was not valid. It must result in a command \r\nname, a script block, or a CommandInfo object.\r\nAt line:11 char:2\r\n+ &$exec_wrapper\r\n+ ~~~~~~~~~~~~~\r\n + CategoryInfo : InvalidOperation: :)) [], RuntimeException\r\n + FullyQualifiedErrorId : BadExpression\r\n ", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

    PLAY RECAP ********************************************************************************************************************************
    <remote IP> : ok=9 changed=4 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0


    I'm not sure that the interpreter's location on the remote Windows host will always be the same, so I am not sure if it is safe for me to hardcode this location.

    How can I correctly tell Ansible where it can find the python interpreter on the remote Windows host?

    Continue reading...

Compartilhe esta Página