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:
- name: Test Playbook
hosts: localhost
tasks:
- name: Echo a message
ansible.builtin.command: echo "Hello from Ansible"
Python code is as follows:
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:
- 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