Ansible command module can't execute arguments - ansible

an Ansible noobie here.
The issue i'm having is the when I used "command" module to execute a command, it will fail. Tried this on a remote host and localhost as well.
BASIC INFO:
Version: ansible 2.0.0 (devel 2c9d1257ba)
Core: (detached HEAD 5983d64d77) last updated 2015/05/30 07:22:33 (GMT +800)
Extras: (detached HEAD 1276420a3a) last updated 2015/05/30 07:22:41 (GMT +800)
Ansible Host file
local ansible_ssh_host=127.0.0.1 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=a
Command
ansible -i ansible_hosts -m command -a "/usr/bin/ls" local
Actual Result
local | FAILED! => {u'msg': u'Traceback (most recent call last):\r\n
File
"/root/.ansible/tmp/ansible-tmp-1436165888.5-23845581569171/command",
line 2139, in <module>\r\n main()\r\n File
"/root/.ansible/tmp/ansible-tmp-1436165888.5-23845581569171/command",
line 158, in main\r\n module =
CommandModule(argument_spec=dict())\r\n File
"/root/.ansible/tmp/ansible-tmp-1436165888.5-23845581569171/command",
line 606, in __init__\r\n self._check_for_check_mode()\r\n File
"/root/.ansible/tmp/ansible-tmp-1436165888.5-23845581569171/command",
line 1142, in _check_for_check_mode\r\n for (k,v) in
self.params.iteritems():\r\nAttributeError: \'tuple\' object has no
attribute \'iteritems\'\r\n', u'failed': True, u'changed': False,
u'parsed': False, u'invocation': {u'module_name': u'command',
u'module_args': {u'_raw_params': u'/usr/bin/ls'}}}
Was there something wrong in my config?

It sounds like this question was resolved by running git submodule update --init --recursive in the checkout of Ansible that was being used.
If others run into the same problem, the easiest fix would be to install Ansible from the system package manager of the host from which it's being run, or pip install Ansible in a virtualenv.

Related

Ansible cannot pickle 'module' object

Running the playbook with
$ ansible-playbook test-playbook.yml
Getting error response
ERROR! Unexpected Exception, this is probably a bug: cannot pickle 'module' object
Also have tried shell: "echo text", same error.
Ansible version 2.9, Python 3.8, running on Macos.
test-playbook.yml:
---
- name: run on localhost
hosts: localhost
gather_facts: false
connection: local
tasks:
- name: some name
debug:
msg: "some text"
...
Full output:
$ ansible-playbook test-playbook.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
PLAY [run on localhost] *************************************************************************************
TASK [some name] ********************************************************************************************
ERROR! Unexpected Exception, this is probably a bug: cannot pickle 'module' object
to see the full traceback, use -vvv
Verbose output is not helpful:
$ ansible-playbook test-playbook.yml -vvv
ansible-playbook 2.9.0
config file = None
configured module search path = ['/Users/jprusakova/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible
executable location = /Library/Frameworks/Python.framework/Versions/3.8/bin/ansible-playbook
python version = 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) [Clang 6.0 (clang-600.0.57)]
No config file found; using defaults
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
toml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
PLAYBOOK: test-playbook.yml *********************************************************************************
1 plays in test-playbook.yml
PLAY [run on localhost] *************************************************************************************
META: ran handlers
TASK [some name] ********************************************************************************************
task path: /Users/jprusakova/ViaControl/temp-ansible/test-playbook.yml:9
ERROR! Unexpected Exception, this is probably a bug: cannot pickle 'module' object
the full traceback was:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/bin/ansible-playbook", line 123, in <module>
exit_code = cli.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible/cli/playbook.py", line 127, in run
results = pbex.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible/executor/playbook_executor.py", line 169, in run
result = self._tqm.run(play=play)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible/executor/task_queue_manager.py", line 240, in run
play_return = strategy.run(iterator, play_context)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible/plugins/strategy/linear.py", line 310, in run
self._queue_task(host, task, task_vars, play_context)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible/plugins/strategy/__init__.py", line 343, in _queue_task
worker_prc.start()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible/executor/process/worker.py", line 96, in start
return super(WorkerProcess, self).start()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 121, in start
self._popen = self._Popen(self)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/context.py", line 283, in _Popen
return Popen(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
super().__init__(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
self._launch(process_obj)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 47, in _launch
reduction.dump(process_obj, fp)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle 'module' object
Ansible version
$ ansible --version
ansible 2.9.0
config file = None
configured module search path = [...]
ansible python module location = /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ansible
executable location = /Library/Frameworks/Python.framework/Versions/3.8/bin/ansible
python version = 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) [Clang 6.0 (clang-600.0.57)]
I had the same issue today, my install details:
$ ansible --version
ansible 2.9.0
config file = None
configured module search path = ['/Users/mark/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/mark/.pyvenv/ansible/lib/python3.9/site-packages/ansible
executable location = /Users/mark/.pyvenv/ansible/bin/ansible
python version = 3.9.6 (default, Jun 29 2021, 05:25:02) [Clang 12.0.5 (clang-1205.0.22.9)]
$ ansible app -m ping
ERROR! Unexpected Exception, this is probably a bug: cannot pickle 'module' object
to see the full traceback, use -vvv
I was unable to resolve with Python3, but using a virtual environment for Python2 it did work:
$ ansible --version
/Users/mark/.pyenv/py2ansible/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:41: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
from cryptography.exceptions import InvalidSignature
ansible 2.9.0
config file = None
configured module search path = [u'/Users/mark/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /Users/mark/.pyenv/py2ansible/lib/python2.7/site-packages/ansible
executable location = /Users/mark/.pyenv/py2ansible/bin/ansible
python version = 2.7.16 (default, May 8 2021, 11:48:02) [GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy
$ ansible app -m ping
/Users/mark/.pyenv/py2ansible/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:41: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
from cryptography.exceptions import InvalidSignature
m1-base | SUCCESS => {
"changed": false,
"ping": "pong"
}
Maybe not ideal, but at least I can carry on. If you did manage to solve, please post your solution
To create the virtual env on MacBookPro:
pip install virtualenv
python -m virtualenv ~/.pyenv/py2ansible
source ~/.pyenv/py2ansible/bin/activate
I had the same issue on Mac (Python 3.10.4)
I was able to resolve it by installing ansible v 2.9.8
ansible 2.9.8
config file = None
configured module search path = ['/Users/smanciot/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/ansible
executable location = /Library/Frameworks/Python.framework/Versions/3.10/bin/ansible
python version = 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)]
UPDATE: Using the latest version of Ansible in my venv fixed the problem (replacing ansible==2.9 with ansible in requirements.txt)
$ ansible all -vvv -a 'uptime' -i hosts.yaml
ansible [core 2.12.1]
config file = None
configured module search path = ['/Users/jsherman/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/jsherman/Desktop/Code/ansible-plays/ansible-env/lib/python3.9/site-packages/ansible
ansible collection location = /Users/jsherman/.ansible/collections:/usr/share/ansible/collections
executable location = /Users/jsherman/Desktop/Code/ansible-plays/ansible-env/bin/ansible
python version = 3.9.9 (main, Nov 21 2021, 03:22:47) [Clang 12.0.0 (clang-1200.0.32.29)]
jinja version = 3.0.3
libyaml = True
I'm seeing the same issue, also on macOS. I was able to reproduce the issue using python 3.9.9 and 3.10.1 (both from Homebrew), and the OS-installed python3 binary (3.8.2)
Using python2 on macOS fixes it for me, as does using python3 on Ubuntu instead.
I'm wondering if there's something wrong with python3 on macOS specifically that's causing this

ansible gathering facts fails to retrieve ansible_distribution_release ("module invocation has junk after the json data")

running:
ansible -m setup <hostname>
(hostname being an ubuntu 20.04 desktop)
returns the warning:
[WARNING]: Module invocation had junk after the JSON data: AttributeError("module 'platform' has no attribute 'dist'")
and the output does not show any field "ansible_distribution_release".
As expected, tasks using this variable fail:
'ansible_distribution_release' is undefined
anything I may be missing ?
edit: the issue occurred with ansible 2.5.1, and was solved by running a newer version of ansible, see accepted answer.
I think you're running an older version of Ansible.
The use of platform.dist() in Ansible was removed in commit 61b1da, dated Dec. 10 2018. Python itself dropped support for platform.dist() in Python 3.8.
With Ansible 2.9.16, running the setup module against an Ubuntu 20.04 system yields:
$ ansible all -i 192.168.122.212, -u root -m setup | grep ansible_distribution
"ansible_distribution": "Ubuntu",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/os-release",
"ansible_distribution_file_variety": "Debian",
"ansible_distribution_major_version": "20",
"ansible_distribution_release": "focal",
"ansible_distribution_version": "20.04",

Shared Connection to host Closed on running Ansible playbook as unprivileged user?

I am using ansible v2.9.2 and recently I am facing issues using the npm ansible module as it is giving me shared connection to host closed errors. I have tried using both python2 and 3 and the results were the same. Below is the doc containing my error and playbook as well please have a look.
link: https://docs.google.com/document/d/1iaNMIjR3EVFYVvSoJEPTmjhSrDsnfZc5VCvUUamdKps/edit?usp=sharing
fatal: [1.0.3.99]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 1.0.3.99 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/var/tmp/ansible-tmp-1577345183.7290096-173113890020428/AnsiballZ_npm.py\", line 114, in \r\n _ansiballz_main()\r\n File \"/var/tmp/ansible-tmp-1577345183.7290096-173113890020428/AnsiballZ_npm.py\", line 106, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/var/tmp/ansible-tmp-1577345183.7290096-173113890020428/AnsiballZ_npm.py\", line 49, in invoke_module\r\n imp.load_module('main', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_npm_payload_6EJdAk/main.py\", line 310, in \r\n File \"/tmp/ansible_npm_payload_6EJdAk/main.py\", line 287, in main\r\n File \"/tmp/ansible_npm_payload_6EJdAk/main.py\", line 200, in list\r\n File \"/usr/lib/python2.7/json/init.py\", line 339, in loads\r\n return _default_decoder.decode(s)\r\n File \"/usr/lib…
Ansible Playbook: 
- hosts: all
  remote_user: abhinav
  become: yes
  tasks:
   - name: npm command
     npm:
     path: /data/codebase/test/api
     executable: /home/test/.nvm/versions/node/v8.15.0/bin/npm
     state: present
     become_user: test
     become: yes
The problem is becoming an unprivileged user
When both the connection user and the become_user are unprivileged, the module file is written as the user that Ansible connects as, but the file needs to be readable by the user Ansible is set to become. In this case, Ansible makes the module file world-readable ... Starting in Ansible 2.1, Ansible defaults to issuing an error if it cannot execute securely with become."
See Ways to resolve this include:
Use pipelining pipelining = true
Install POSIX.1e filesystem acl support on the managed host.
Avoid becoming an unprivileged user.

Unable to ping server using ansible

I'm unable to ping the another server. And i have SSH communication also authenticated.
Please find the below error
ansible all -m ping -u ubuntu -i inventory
WARNING]: Unhandled error in Python interpreter discovery for host 10.0.3.128: No JSON object could be decoded
[WARNING]: Unhandled error in Python interpreter discovery for host 10.0.3.53: No JSON object could be decoded
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: No module named zipfile
[WARNING]: Platform linux on host 10.0.3.128 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
10.0.3.128 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"module_stderr": "Shared connection to 10.0.3.128 closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1574742993.99-111761674559704/AnsiballZ_ping.py\", line 102, in <module>\r\n _ansiballz_main()\r\n File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1574742993.99-111761674559704/AnsiballZ_ping.py\", line 21, in _ansiballz_main\r\n import zipfile\r\nImportError: No module named zipfile\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: No module named zipfile
[WARNING]: Platform linux on host 10.0.3.53 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
10.0.3.53 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"module_stderr": "Shared connection to 10.0.3.53 closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1574742994.0-31022973006547/AnsiballZ_ping.py\", line 102, in <module>\r\n _ansiballz_main()\r\n File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1574742994.0-31022973006547/AnsiballZ_ping.py\", line 21, in _ansiballz_main\r\n import zipfile\r\nImportError: No module named zipfile\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
Prasanna, go to 10.0.3.128 run python, and try to :
import zipfile
It seems like either it wasn't compiled with it or it can't find the library so maybe an environment issue. try it as the ansible user
I have got the similar error and resolved it.
After creating the container I used the following command to install python on it:
apt-get install python-minimal --no-install-recommends
But, when i got the "ImportError: No module named zipfile" error with ansible ping command, I just got into the container (with lxc-attach) and used:
apt-get install python
And then my ansible ping command worked as expected.
Seems like ansible is not able to find python distribution in the client.
The simplest solution is to tell ansible where to find it in ansible.cfg. Just add:
[defaults]
...
interpreter_python = <pyhton path in client>
the python path in the client can be found by running:
which python
or for newer versions (recommended for ansible):
which python3
like so:
in most modern Debian based distributions, python is located at /usr/bin/python3
that makes the final solution:
[defaults]
...
interpreter_python = /usr/bin/python3

Adding a PPA using Ansible fails

I'm using Ansible 2.2.0 to provision a Ubuntu 16.04 server, but it keeps failing at the step where I'm adding a new apt repository:
fatal: [the.server.ip.address]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n File \"/tmp/ansible_pctla3gj/ansible_module_apt_repository.py\", line 528, in <module>\r\n main()\r\n File \"/tmp/ansible_pctla3gj/ansible_module_apt_repository.py\", line 492, in main\r\n sourceslist.add_source(repo)\r\n File \"/tmp/ansible_pctla3gj/ansible_module_apt_repository.py\", line 403, in add_source\r\n info = self._get_ppa_info(ppa_owner, ppa_name)\r\n File \"/tmp/ansible_pctla3gj/ansible_module_apt_repository.py\", line 377, in _get_ppa_info\r\n return json.load(response)\r\n File \"/usr/lib/python3.5/json/__init__.py\", line 268, in load\r\n parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)\r\n File \"/usr/lib/python3.5/json/__init__.py\", line 312, in loads\r\n s.__class__.__name__))\r\nTypeError: the JSON object must be str, not 'bytes'\r\n", "msg": "MODULE FAILURE"}
I have the following in my playbook:
- name: add the ffmpeg PPA
apt_repository:
repo: 'ppa:jonathonf/ffmpeg-3'
The docs seemed pretty straightforward, so I'm not sure what the problem is here. I tried adding it manually (sudo add-apt-repository ppa:jonathonf/ffmpeg-3) and it worked just fine.
Found this answer which had a different error, but the answer worked; add become: yes to the task and it will work.

Resources