4

Hi i am checking the version of python using ansible task However i am getting output value in stderr variable instead of stdout.

Here is my ansible task to check pthon version.

---
- shell: "python --version"
  register: python_installed

Here is the output of the task:

changed: [172.17.0.3] => {
    "changed": true,
    "cmd": "python --version",
    "delta": "0:00:00.259578",
    "end": "2017-06-30 03:43:44.341772",
    "invocation": {
        "module_args": {
            "_raw_params": "python --version",
            "_uses_shell": true,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2017-06-30 03:43:44.082194",
    "stderr": "Python 2.6.6",
    "stderr_lines": [
        "Python 2.6.6"
    ],
    "stdout": "",
    "stdout_lines": []
}

I tried to add python path in executable arguement, it also failed. I also tried using command module insted of shell module. Still failing

4

I see no error here. Command has been executed successfully (rc=0).

Python's -v switch is supposed to write version number to stderr. From source:

fprintf(stderr, "Python %s\n", PY_VERSION);

If (for some specific reason) you need to see it in stdout instead, use descriptor redirection:

- shell: python --version 2>&1
1
  • 1
    Note that python 3 has changed behaviour from python 2. Python 3 outputs version info to stdout (python 2 outputs to stderr, as pointed out in the answer). – mozz100 Jun 7 '18 at 9:42
13

Just as a note:

Python version is already gathered by the setup_module and can be accessed by ansible_python_version

ansible -m setup localhost | grep ansible_python_version
       "ansible_python_version": "3.6.1",

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.