# Ansible Builtin Modules not found using Python PlaybookExecutor

**URL:** <https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709>\
**Category:** Get Help\
**Tags:** rhel, python\
**Created:** [February 28, 2025, 2:54pm UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709 "2025-02-28T14:54:23Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [February 28, 2025, 2:54pm UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/1 "2025-02-28T14:54:23Z")

</div>

hi, folks

On a RHEL 9 system using Python 3.12, a private virtual environment and latest ansible-core (1.18)  
and the PlaybookExecutor API, I get errors if I call ansible.builtin modules full qualified in my playbook.

Error message is:  
`couldn't resolve module/action 'ansible.builtin.command'. This often indicates a misspelling, missing collection, or incorrect module path.`

the playbook is as follows:

```auto
- name: Test Playbook
  hosts: localhost
  tasks:
    - name: Echo a message
      ansible.builtin.command: echo "Hello from Ansible"

```

Python code is as follows:

```auto
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
from ansible.vars.manager import VariableManager
import sys
import ansible_runner
import os
from ansible.playbook.play import Play
from ansible import context
from ansible.module_utils.common.collections import ImmutableDict
from ansible.plugins.loader import module_loader
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase

mypath="~/automation/.venv/lib/python3.12/site-packages/ansible/modules/:."

def run_playbook(playbook_path):

    context.CLIARGS = ImmutableDict({
        'connection': 'local',
        'module_path': [mypath],
        'forks': 10,
        'become': None,
        'become_method': None,
        'become_user': None,
        'check': False,
        'diff': False,
        'syntax': None,
        'start_at_task': None
    })
    loader = DataLoader()
    os.environ['ANSIBLE_COLLECTIONS_PATHS'] = mypath
    os.environ['ANSIBLE_LIBRARY'] = mypath
    inventory = InventoryManager(loader=loader, sources=["localhost,"])
    variable_manager = VariableManager(loader=loader, inventory=inventory)
        
    executor = PlaybookExecutor(
        playbooks=[playbook_path],
        inventory=inventory,
        variable_manager=variable_manager,
        loader=loader,
        passwords={}
    )

    executor.run()

if __name__ == " __main__":
    run_playbook("ansible_test/test_playbook.yml")

```

when I run the playbook natively, its working:  
ansible-playbook test\_playbook.yml -i “localhost,”

the crazy thing: When I use non-qualified module, it also works from Python:

```auto
- name: Test Playbook
  hosts: localhost
  tasks:
    - name: Echo 
      command: echo "This works fine !!!!!"

```

this tells me that the builtin modules are installed correctly in my environment, but not accessible within Python runtime - although I set (and verified) the module path.

any help is apprechiated.

Kind regards  
Stephan

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [February 28, 2025, 7:24pm UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/2 "2025-02-28T19:24:43Z")

</div>

Hi  
What say `ansible --version` ?  
Probably your modules path isn’t properly configured.

Also your code seems to be based on venv, so provably your modules path isn’t updated into the venv.

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 1, 2025, 2:54pm UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/3 "2025-03-01T14:54:11Z")

</div>

Interfacing with ansible module directly can be quite flaky in the future as there are no promises regarding the api and how everything is put together.

I’d rather suggest to take a look at [Ansible runner](https://ansible.readthedocs.io/projects/runner/en/stable/) which provides stable interface to run ansible programmatically.

---

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [March 3, 2025, 6:57am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/4 "2025-03-03T06:57:10Z")

</div>

result is as follows:

ansible --version  
ansible [core 2.18.3]  
config file = /home/d7yc7x3@xxx/.ansible.cfg  
configured module search path = [‘/home/d7yc7x3@xxx/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]  
ansible python module location = /home/d7yc7x3@xxx/automation/.venv/lib64/python3.12/site-packages/ansible  
ansible collection location = /home/d7yc7x3@xxx/.ansible/collections:/usr/share/ansible/collections  
executable location = /home/d7yc7x3@xxx/automation/.venv/bin/ansible  
python version = 3.12.5 (main, Dec 3 2024, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/home/d7yc7x3@xxx/automation/.venv/bin/python)  
jinja version = 3.1.5  
libyaml = True

---

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [March 3, 2025, 7:21am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/5 "2025-03-03T07:21:18Z")

</div>

noticed, but ansible-runner is too slow for our usecase.

---

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [March 3, 2025, 9:47am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/6 "2025-03-03T09:47:34Z")

</div>

some more details:

```auto
[d7yc7x3@Kallie02i modules]$ ansible-config dump | grep COLLECTIONS_PATHS
COLLECTIONS_PATHS(/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/.ansible.cfg) = ['/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib64/python3.12/site-packages/ansible/modules']
[d7yc7x3@Kallie02i modules]$ ansible-test integration ping
FATAL: The current working directory must be within the source tree being tested.

Testing an Ansible collection: {...}/ansible_collections/{namespace}/{collection}/
Example #1: community.general -> ~/code/ansible_collections/community/general/
Example #2: ansible.util -> ~/.ansible/collections/ansible_collections/ansible/util/

Current working directory: /home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib/python3.12/site-packages/ansible/modules/
No "ansible_collections" parent directory was found.

```

in my modules dir, all moduls are present:

```auto
[d7yc7x3@Kallie02i modules]$ ls /home/d7yc7x3@xxx/automation/.venv/lib64/python3.12/site-packages/ansible/modules
add_host.py fail.py known_hosts.py setup.py
apt_key.py fetch.py lineinfile.py shell.py
apt.py file.py meta.py slurp.py
apt_repository.py find.py mount_facts.py stat.py
assemble.py gather_facts.py package_facts.py subversion.py
assert.py getent.py package.py systemd.py
async_status.py get_url.py pause.py systemd_service.py
async_wrapper.py git.py ping.py sysvinit.py
blockinfile.py group_by.py pip.py tempfile.py
command.py group.py __pycache__ template.py
copy.py hostname.py raw.py unarchive.py
cron.py import_playbook.py reboot.py uri.py
deb822_repository.py import_role.py replace.py user.py
debconf.py import_tasks.py rpm_key.py validate_argument_spec.py
debug.py include_role.py script.py wait_for_connection.py
dnf5.py include_tasks.py service_facts.py wait_for.py
dnf.py include_vars.py service.py yum_repository.py
dpkg_selections.py __init__.py set_fact.py
expect.py iptables.py set_stats.py

```

so, it’s pretty native what I am doing here ☹ 😰

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [March 3, 2025, 10:41am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/7 "2025-03-03T10:41:29Z")

</div>

It seems that ansibleconfigured module search path (seen in ansible --version) doesn’t include the path you’re using.

Ansible configured module search path is : `[‘/home/d7yc7x3@xxx/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]`

versus  
what you show/use is : `/home/d7yc7x3@xxx/automation/.venv/lib64/python3.12/site-packages/ansible/modules`

I’m using venv too, however my configuration for module search path is the same as yours in ansible --version output, and everything works well.

I think you got a misconfiguration with your venv.

---

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [March 3, 2025, 4:42pm UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/8 "2025-03-03T16:42:51Z")

</div>

> [@motorbass](#):
>
> .ansible/plugins/modules

thanks, Gael.

but that was obvious not the solution. I have change the module path in ansible.cfg but without result to my issue:

```auto
ansible --version
ansible [core 2.18.3]
  config file = /home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/.ansible.cfg
  configured module search path = ['/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib64/python3.12/site-packages/ansible/modules', '/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib/python3.12/site-packages/ansible/modules', '/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib/python3.12/site-packages/ansible/plugins', '/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/.local/lib/python3.12/site-packages/ansible/plugins', '/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib/python3.12/site-packages/ansible', '/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib/python3.12/site-packages']
  ansible python module location = /home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib64/python3.12/site-packages/ansible
  ansible collection location = /home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/lib64/python3.12/site-packages/ansible/modules
  executable location = /home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/bin/ansible
  python version = 3.12.5 (main, Dec 3 2024, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/home/d7yc7x3@shsr.ing.private.cloud.vwgroup.com/automation/.venv/bin/python)
  jinja version = 3.1.5
  libyaml = True

```

---

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [March 3, 2025, 5:09pm UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/9 "2025-03-03T17:09:32Z")

</div>

in addtion to my first mail: The error occurs on ansible-core (2.18). And it works with/until ansible-core 2.14.17.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 3, 2025, 6:00pm UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/10 "2025-03-03T18:00:32Z")

</div>

_ **cough** _init\_plugin\_loader_ **cough** _

Excuse me, might be catching a cold 🤒 😉

---

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [March 6, 2025, 8:50am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/11 "2025-03-06T08:50:36Z")

</div>

hello Sivel - you anwer was pretty unclear. My issue is still open and any help is appreciated.

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [March 6, 2025, 9:43am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/12 "2025-03-06T09:43:19Z")

</div>

@Stephan4711 i had a look on various issues regarding yours, however the official doc says [Python API isn’t for external use unfortunately.](https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html)

You probably better move to ansible-runner as @kristianheljas said.

By the way, why do you say it’s too slow for your usecase ? 🙂

---

<div class="post-metadata">

**Author:** ![Stephan4711](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Stephan4711](https://forum.ansible.com/u/Stephan4711)\
**Post date:** [March 6, 2025, 11:23am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/13 "2025-03-06T11:23:33Z")

</div>

Thanks, Gael. I have overseen this usage hint.

Runner is fine, it just takes some more seconds during initialzation , but that’s ok .

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 6, 2025, 11:39am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/14 "2025-03-06T11:39:36Z")

</div>

What sivel is truing to say thet you are missing `init_plugin_loader` function call.

```auto
from ansible.plugins.loader import init_plugin_loader
init_plugin_loader([])

```

> See [cli run method](https://github.com/ansible/ansible/blob/2c6b78f5166392491f43c07504029d02aecc3380/lib/ansible/cli/ __init__.py#L103)

And regarding ansible-runner:

Are you intefacing ansible-runner [using python](https://ansible.readthedocs.io/projects/runner/en/stable/python_interface/)?  
The example code below only costs few hundred milliseconds to start:

```auto
import ansible_runner

r = ansible_runner.run(
    project_dir='/my/project/dir',
    playbook='playbooks/site.yml'
)

print("{}: {}".format(r.status, r.rc))
# successful: 0
for each_host_event in r.events:
    print(each_host_event['event'])
print("Final status:")
print(r.stats)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 5, 2025, 11:40am UTC](https://forum.ansible.com/t/ansible-builtin-modules-not-found-using-python-playbookexecutor/40709/15 "2025-04-05T11:40:04Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
