gekmihesg/ansible-openwrt

monkeypatch.py generates infinite recrusion

greg-hellings opened this issue · 2 comments

Currently when running code based on this role, I get the following error (Ansible 2.8.{3,4,5}):

Command:

ansible-playbook -i hosts ~/path/to/openwrt.yml -vvv

Relevant output:

The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 111, in run
    item_results = self._run_loop(items)
  File "/usr/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 371, in _run_loop
    res = self._execute(variables=task_vars)
  File "/usr/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 664, in _execute
    result = self._handler.run(task_vars=variables)
  File "/usr/lib/python3.7/site-packages/ansible/plugins/action/normal.py", line 46, in run
    result = merge_hash(result, self._execute_module(task_vars=task_vars, wrap_async=wrap_async))
  File "/usr/lib/python3.7/site-packages/ansible/plugins/action/__init__.py", line 809, in _execute_module
    (module_style, shebang, module_data, module_path) = self._configure_module(module_name=module_name, module_args=module_args, task_vars=task_vars)
  File "/home/ghelling/config/mkgreg/roles/openwrt_wifi/filter_plugins/monkeypatch.py", line 28, in _configure_module
    self.__configure_module(module_name, module_args, task_vars)
  File "/home/ghelling/config/mkgreg/roles/openwrt_switch/filter_plugins/monkeypatch.py", line 28, in _configure_module
    self.__configure_module(module_name, module_args, task_vars)
  File "/home/ghelling/config/mkgreg/roles/openwrt_switch/filter_plugins/monkeypatch.py", line 28, in _configure_module
    self.__configure_module(module_name, module_args, task_vars)
  File "/home/ghelling/config/mkgreg/roles/openwrt_switch/filter_plugins/monkeypatch.py", line 28, in _configure_module
    self.__configure_module(module_name, module_args, task_vars)
  [Previous line repeated 968 more times]
  File "/home/ghelling/config/mkgreg/roles/openwrt_switch/filter_plugins/monkeypatch.py", line 22, in _configure_module
    openwrt_module = self._shared_loader_obj.module_loader.find_plugin('openwrt_' + module_name, '.sh')
  File "/usr/lib/python3.7/site-packages/ansible/plugins/loader.py", line 502, in find_plugin
    plugin = self._find_plugin(name, mod_type=mod_type, ignore_deprecated=ignore_deprecated, check_aliases=check_aliases, collection_list=collection_list)
  File "/usr/lib/python3.7/site-packages/ansible/plugins/loader.py", line 418, in _find_plugin
    return self._find_plugin_legacy(name, ignore_deprecated, check_aliases, suffix)
  File "/usr/lib/python3.7/site-packages/ansible/plugins/loader.py", line 440, in _find_plugin_legacy
    for path in (p for p in self._get_paths() if p not in self._searched_paths and os.path.isdir(p)):
  File "/usr/lib/python3.7/site-packages/ansible/plugins/loader.py", line 440, in <genexpr>
    for path in (p for p in self._get_paths() if p not in self._searched_paths and os.path.isdir(p)):
  File "/usr/lib64/python3.7/genericpath.py", line 42, in isdir
    st = os.stat(s)
RecursionError: maximum recursion depth exceeded while calling a Python object

fatal: [genesis.greg.thehellings.com]: FAILED! => 
  msg: Unexpected failure during module execution.
  stdout: ''

Should note: this occurs if the monkeypatch.py is somehow invoked multiple times. Specifically, I have forked your role to create 3 or 4 specific roles for my use case. When I add multiple of them to the playbook, the infinite recursion occurs.

This is not how this role should be used. Your folder structure should look something like this:

...
├── roles
│   ├── gekmihesg.openwrt
│   │   └── ...
│   ├── network
│   │   ├── meta
│   │   │   └── main.yml
│   │   └── tasks
│   │       └── main.yml
│   └── wifi
│       ├── meta
│       │   └── main.yml
│       └── tasks
│           └── main.yml
└── site.yml

Where roles/*/meta/main.yml contains:

---
dependencies:
- gekmihesg.openwrt

And site.yml something like:

---
- hosts: all
  roles:
    - network
    - wifi

This way, the openwrt role gets executed once before the network role, loading all necessary patches once. The other roles can just be written like any ordinary Ansible role, the OpenWRT specific overrides will automatically be used for all hosts that are member of a group named openwrt.