Ansible cannot pickle 'module' object - ansible

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

Related

Ansible AWX on K3s - Galaxy install problem "Failed to download collection tar from 'server0': <urlopen error [Errno -2] Name or service not known"

I have a problem with the Ansible AWX instance on K3s.
Used guide: https://computingforgeeks.com/how-to-install-ansible-awx-on-ubuntu-linux/
I want to use the Ansible collections install option when syncing my project to git but that doesn't work.
I am using collections/requirements.yml file which during execution expects on a random collection (I want to install 7 collections from)
My collections:
community.general
community.mysql
community.docker
ansible.posix
community.vmware
community.zabbix
awx.awx
Error with job details (Standard Error):
ERROR! Failed to download collection tar from 'server0': <urlopen error [Errno -2] Name or service not known>
Error with manual installation in a container with -vvv and -c flags
bash-4.4$ ansible-galaxy collection install -r requirements.yml --collections-path /var/lib/awx/projects/.__awx_cache/_15__gitlab_ansible_awx_devops/stage/requirements_collections -vvv -c
ansible-galaxy [core 2.12.5.post0]
config file = None
configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible-galaxy
python version = 3.8.12 (default, Sep 21 2021, 00:10:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 2.10.3
libyaml = True
No config file found; using defaults
Reading requirement file at '/var/lib/awx/projects/_15__gitlab_ansible_awx_devops/collections/requirements.yml'
Starting galaxy collection install process
Process install dependency map
Opened /home/runner/.ansible/galaxy_token
[WARNING]: Skipping Galaxy server https://galaxy.ansible.com/api/. Got an unexpected error when getting available versions of collection community.general: Unknown
error when attempting to call Galaxy at 'https://galaxy.ansible.com/api/v2/collections/community/general/': <urlopen error [Errno 101] Network is unreachable>
ERROR! Unknown error when attempting to call Galaxy at 'https://galaxy.ansible.com/api/v2/collections/community/general/': <urlopen error [Errno 101] Network is unreachable>
It alternates between getting the error with "Network is unreachable" and "Name or service not known". Local installation goes without any problem.
Thanks for any help
Info:
Ubuntu 20.04
ansible [core 2.12.5.post0]
AWX 21.1.0
k3s version v1.23.6+k3s1 (418c3fa8)

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

Error running playbook with encrypted group_vars

I am trying to run a playbook with a file that has been encrypted using ansible-vault. However, I get the following error:
ERROR! Syntax Error while loading YAML.
could not find expected ':'
The error appears to have been in 'inventories/group_vars/main_server': line 41, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
Content of vault file:
me#server:ansible$ cat inventories/group_vars/main_server
$ANSIBLE_VAULT;1.1;AES256
...
Command I'm running:
ansible-playbook -i inventories/group_vars/main_server --extra-vars "target=my_target" playbook.yml --ask-become-pass --ask-pass --vault-password-file vault_pwd.txt
vault_pwd.txt is populated with the required password. I have been encrypting/decrypting as I develop my playbook. For instance, the shell script with the encrypting command:
ansible-vault encrypt inventories/group_vars/main_server --vault-password-file vault_pwd.txt
Edit #1
Version of Ansible:
me#server:ansible$ ansible --version
ansible 2.7.5
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/me/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
The error you're seeing...
ERROR! Syntax Error while loading YAML.
could not find expected ':'
The error appears to have been in 'inventories/group_vars/main_server': line 41, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
...is a YAML syntax error. Ansible is probably complaining about an actual syntax error in the unencrypted content (and if you're lucky, the line referenced in the error message is in fact the one with the error).

Ansible: error when deploying playbooks in parallel

i am setting up a kubernetes cluster with ansible.
This is running fine.
Now i usually have 2 or 3 clusters i can test different things with.
Often it happens at some point in time that the cluster/server gots broken. If that happens, i usually recreate the servers and start the playbook again. because this takes some time, i want to be able to run 2 or more playbooks in parallel.
But every time i do this, i get the following error:
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: FileNotFoundError: [Errno 2] No such file or directory
I run my playbook like this:
"$ansible_playbook"
-i "${ANSIBLE_HOSTS}"
"${ANSIBLE_YML}"
--flush-cache
--user root
--become
--become-user root
--ask-sudo-pass
What could be the reason for the error?
I can imagine, that ansible creates some files in the background, used by the different playbooks. But which files could that be?
thx in advance!
Update more detailed error log (-vvv)
ansible-playbook 2.7.8
config file = /home/mod/cod/wo/thingylabs/kubernetes-provisioning/playbooks/test1/ansible.cfg
configured module search path = ['/home/mod/cod/wo/thingylabs/kubernetes-provisioning/vendors/kubespray/library']
ansible python module location = /usr/lib/python3.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.7.2 (default, Jan 10 2019, 23:51:51) [GCC 8.2.1 20181127]
Using /home/mod/cod/wo/thingylabs/kubernetes-provisioning/playbooks/test1/ansible.cfg as config file
SUDO password:
ERROR! Unexpected Exception, this is probably a bug: [Errno 2] No such file or directory
the full traceback was:
Traceback (most recent call last):
File "/usr/bin/ansible-playbook",
exit_code = cli.run()
File "/usr/lib/python3.7/site-packages/ansible/cli/playbook.py", line 104, in run
loader, inventory, variable_manager = self._play_prereqs(self.options)
File "/usr/lib/python3.7/site-packages/ansible/cli/__init__.py", line 786, in _play_prereqs
inventory = InventoryManager(loader=loader, sources=options.inventory)
File "/usr/lib/python3.7/site-packages/ansible/inventory/manager.py", line 148, in __init__
self.parse_sources(cache=True)
File "/usr/lib/python3.7/site-packages/ansible/inventory/manager.py", line 207, in parse_sources
source = unfrackpath(source, follow=False)
File "/usr/lib/python3.7/site-packages/ansible/utils/path.py", line 47, in unfrackpath
basedir = op.getcwd()
FileNotFoundError: [Errno 2] No such file or directory

Ansible command module can't execute arguments

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.

Resources