0

SYNOPSIS

I was getting the following error when trying to run both the standard ansible all -m ping and ansible all -a "pwd" (The second command is just in the case the ping module was the issue):

..."module_stdout": "  File \"/tmp/ansible_C2IrV6/ansible_module_command.py\", line 183\r\n    out = b''\r\n            ^\r\nSyntaxError: invalid syntax\r\n",...

My issue was that I was somehow running an unreleased Ansible version (2.4.0) due to installation via pip. This was conflicting with my yum installation (2.3.1.0), compounded by an incompatibility with my current Python version (2.6.6).

My solution was to uninstall both versions to ensure I no longer had ansible on my system. From there, I used yum to reinstall ansible to a version that I knew was compatible (2.3.1.0). I have also read that it's possible to use pip to specify the version:

pip install ansible==<version>

There are more details on installing different versions here


ORIGINAL POST

I've seen many instances where people seem to have my exact issue, but it always ends up being something slightly different. Regardless, I attempt the solutions to no avail.

I'm running Ansible 2.4.0 on what I believe is RHEL6:

$ uname -a
Linux <server address> 2.6.32-642.11.1.el6.x86_64 #1 SMP Wed Oct 26 10:25:23 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux

The host I'm communicating with is running RHEL5.

When I run this command:

$ sudo ansible all -a "pwd" -vvvv

I get the following result:

Verbose Ansible Output

Extracting the ssh command from the above output:

$ ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/root/.ssh/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath=~/.ansible/cp' user@hostDestination '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''

Result:

Extracted Ansible SSH Command Output

Based on the result above, it seems like the command makes a successful connection, so I don't see why Ansible is giving me the Permission Denied

EDIT: Thanks to @KonstantinSuvorov, I was able to more precisely pin down my issue – I seem to have attributed a module failure to the command's result of Permission Denied because I got the best response using sudo. See his post for why that was an issue.


Some Additional Information

On the server with Ansible installed, I have super user privileges. On the destination host I do not.

Normal SSHing into the destination host works perfectly fine and vice versa.

The following is what we see upon successfully logging in to any of our servers here at work, so don't be alarmed when it isn't familiar:

    ***************************************************************
    *                                                             *
    * Do not attempt to log on unless you are an authorized user. *
    *                                                             *
    * You must have a valid Network account.                      *
    *                                                             *
    ***************************************************************

If it's necessary, I'll provide my ansible.cfg when I get back to the office tomorrow.

Forgive me if this belongs in SuperUser, ServerFault, or somewhere else.


UPDATE

Running the command without sudo:

ansible all -a "/bin/pwd" -vvvv

Result:

Verbose Ansible Command Without Sudo


UPDATE 2

After placing ssh_args= under [ssh_connection] in my ansible.cfg:

ansible all -a "pwd"

Result:

hostDestination | FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "\n***************************************************************\n*                                                             *\n* Do not attempt to log on unless you are an authorized user. *\n*                                                             *\n* You must have a valid Network account.                      *\n*                                                             *\n***************************************************************\n\nConnection to hostDestination closed.\r\n",
    "module_stdout": "  File \"/tmp/ansible_C2IrV6/ansible_module_command.py\", line 183\r\n    out = b''\r\n            ^\r\nSyntaxError: invalid syntax\r\n",
    "msg": "MODULE FAILURE",
    "rc": 0
}

NOTE: Running the command with Ansible's raw module is successful:

ansible all -m raw -a "pwd"

Result:

hostDestination | SUCCESS | rc=0 >>
/home/user

***************************************************************
*                                                             *
* Do not attempt to log on unless you are an authorized user. *
*                                                             *
* You must have a valid Network account.                      *
*                                                             *
***************************************************************

Shared connection to hostDestination closed.

When I look at the verbose output (-vvvv) of the regular command I see the following modules being used:

Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/basic.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/_text.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/parsing/__init__.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/pycompat24.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/six/__init__.py
Using module file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/modules/commands/command.py

I feel like one or more modules may be the issue seeing as the last line in the output above is similar to the following line in the failed command's output:

... /tmp/ansible_C2IrV6/ansible_module_command.py\ ...

It looks as if the Python interpreter is having an issue with the syntax of the empty binary string in this file. Unfortunantely, Ansible deletes the file immediately after running the command – preventing me from looking at line 183 to make my own assessment.

Darrel Holt
  • 870
  • 1
  • 15
  • 39

1 Answers1

1

You run ansible with sudo under root account and it tries to use ssh-keys from root account (which are absent?).
When you try ssh command, you run it under your current user account and use another access key.

I believe you have no reasons to use sudo here.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • Thank you for your reply. It does not work when I run it without sudo either. When I get back to the office in the morning, I will post an update with the result of the command without sudo. – Darrel Holt Aug 16 '17 at 06:57
  • 1
    The error is completely different to `Permission Denied` in this case. You should try to disable `ControlMaster`: add section `[ssh_connection]` to ansible.cfg and add empty `ssh_args =` in there. – Konstantin Suvorov Aug 16 '17 at 15:48
  • See update 2 for the result of your recommendation and some additional information – Darrel Holt Aug 16 '17 at 17:57
  • To keep remote files in place you can call ansible with `ANSIBLE_KEEP_REMOTE_FILES=1` env set. But this is for sure another question... I think my answer with the comments should be accepted. – Konstantin Suvorov Aug 16 '17 at 18:37
  • I beg you pardon, but your question is titled `Permission Denied When Running Ansible`, I gave you an answer why you have this error – _don't use sudo_. Then gave you advice how to overcome `ssh_connect: needpriv` error you had without `sudo` – _don't use SSH multiplexing_. Now you have new problem with, I guess, outdated Python and unreleased Ansible version. This is not continuous troubleshooting site after all, but question-answer site. – Konstantin Suvorov Aug 16 '17 at 18:56
  • Thank you Konstantin, I accepted your answer because it led me to the solution. Your last statement: "...outdated Python and unreleased Ansible version." brought it together for me. The issue was that `pip` somehow installed Ansible 2.4.0, which as you said hasn't been released and isn't compatible with my *outdated* version of Python. When I used `yum` to install 2.3.1.0, the system was still pointing where the `pip` installation was, even after uninstalling, making me think there wasn't an installation present anymore at all. Some tweaking to get the system to point to 2.3.1 made it work. – Darrel Holt Aug 16 '17 at 21:42