Checking python version through ansible results into error

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


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

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",