Failed to send commands to switch via ansible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 11:35 AM
Hi I am trying to send command show run for switch via ansible, but I got an error message as below. The commands I use is also listed as below. Not sure what is wrong with it. Thank you
admin1@Lenovo10:~$ ansible lab-hosts -m raw -a "show run" -u admin -k | grep 'hostname\| system mtu'
Traceback (most recent call last):
File "/usr/bin/ansible", line 34, in <module>
from ansible import context
File "/usr/local/lib/python3.8/dist-packages/ansible/context.py", line 18, in <module>
from ansible.module_utils.common._collections_compat import Mapping, Set
ModuleNotFoundError: No module named 'ansible.module_utils'
sudo nano hosts
[lab-hosts]
192.168.1.[11:15]
cat hosts
sudo nano ansible.cfg
[defaults]
inventory = ./hosts
host_key checking = false
timeout = 5
cat ansible.cfg
mkdir group_vars
cd /group_vars
sudo nano devices.yml
ansible_network_os: ios
- Labels:
-
EEM Scripting
-
Network Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 11:39 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2022 01:24 PM
Hello,
are the files in /usr/local/lib/python3.8/dist-packages/ansible/ readable by anybody else than the root ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2022 08:05 PM
Thank you Georg for your reply. You mentioned the "file" on your post. which file do you mean?
Please see the below and see if the file is in the folder.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 06:49 AM
as the errors self descriptive, i will look the line numbers mentioned see if that fix any issue
raceback (most recent call last):
File "/usr/bin/ansible", line 34, in <module>
from ansible import context
File "/usr/local/lib/python3.8/dist-packages/ansible/context.py", line 18, in <module>
from ansible.module_utils.common._collections_compat import Mapping, Set
ModuleNotFoundError: No module named 'ansible.module_utils'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 11:03 AM
Hello,
what read/write rights do the files in /usr/local/lib/python3.8/dist-packages/ansible/ have ? I am a bit rusty on that subject, but I think you can see which users have which rights with the command:
ls -lart /usr/lib/python2.7/site-packages/ansible/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 08:02 AM
Thanks balaji for your suggestions which is correct direction for resolving the issue. I am new in this field. Can you explain it a little more?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 08:15 AM
Can you attach the file to look context.py or other python files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 08:33 AM
Please see below:
admin1@Lenovo10:/usr/local/lib/python3.8/dist-packages/ansible$ cat context.py
# Copyright: (c) 2018, Toshio Kuratomi <tkuratomi@ansible.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
"""
Context of the running Ansible.
In the future we *may* create Context objects to allow running multiple Ansible plays in parallel
with different contexts but that is currently out of scope as the Ansible library is just for
running the ansible command line tools.
These APIs are still in flux so do not use them unless you are willing to update them with every Ansible release
"""
from ansible.module_utils.common._collections_compat import Mapping, Set
from ansible.module_utils.common.collections import is_sequence
from ansible.utils.context_objects import CLIArgs, GlobalCLIArgs
__all__ = ('CLIARGS',)
# Note: this is not the singleton version. The Singleton is only created once the program has
# actually parsed the args
CLIARGS = CLIArgs({})
# This should be called immediately after cli_args are processed (parsed, validated, and any
# normalization performed on them). No other code should call it
def _init_global_context(cli_args):
"""Initialize the global context objects"""
global CLIARGS
CLIARGS = GlobalCLIArgs.from_options(cli_args)
def cliargs_deferred_get(key, default=None, shallowcopy=False):
"""Closure over getting a key from CLIARGS with shallow copy functionality
Primarily used in ``FieldAttribute`` where we need to defer setting the default
until after the CLI arguments have been parsed
This function is not directly bound to ``CliArgs`` so that it works with
``CLIARGS`` being replaced
"""
def inner():
value = CLIARGS.get(key, default=default)
if not shallowcopy:
return value
elif is_sequence(value):
return value[:]
elif isinstance(value, (Mapping, Set)):
return value.copy()
return value
return inner
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 09:03 AM
from ansible.module_utils.common._collections_compat import Mapping, Set
this what Line 18 was the issue, i will revisit the environment setup ansible and check all ok and installed.
i will review whole code and let you know if i find any solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2022 01:23 PM
Hi, Client Ubunto can access the router via ssh. When the command "show ip route" is sent to router via ansible, it get the below errro message.. Anyone can help to share some suggestions? Thank you
admin1@Lenovo10:~$ ansible lab-hosts -m raw -a "show ip route" -u admin -k
Traceback (most recent call last):
File "/usr/bin/ansible", line 34, in <module>
from ansible import context
File "/usr/local/lib/python3.8/dist-packages/ansible/context.py", line 18, in <module>
from ansible.module_utils.common._collections_compat import Mapping, Set
ModuleNotFoundError: No module named 'ansible.module_utils'
