Reputation: 1
I am running below python script to run a playbook to ping a host machine. Getting below error
Traceback (most recent call last):
File "ansible.py", line 2, in <module>
from ansible.parsing.dataloader import DataLoader
File "/home/tcprod/schaitanya/python_ansible/ansible.py", line 2, in <module>
from ansible.parsing.dataloader import DataLoader
ImportError: No module named parsing.dataloader
Below is the entire script
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
#Initialize the objects that are needed for the play.
Options = namedtuple('Options',
['connection', 'module_path', 'forks', 'become',
'become_method', 'become_user', 'check']
)
#initialize needed objects
variable_manager = VariableManager()
loader = DataLoader()
options = Options(
connection='local', module_path='', forks=100, become=True,
become_method='sudo', become_user='root', check=False)
passwords = dict(vault_pass='secret')
#create inventory and pass to variable manager
inventory = Inventory(loader=loader, variable_manager=variable_manager,
host_list='localhost')
variable_manager.set_inventory(inventory)
#create play with tasks
play_src = dict(
name="ping localhost",
hosts="localhost",
gather_facts="no",
tasks=[
# installing dependencies
dict(name="ping local host",
action=dict(module="ping"))
])
play = Play().load(play_src, variable_manager=variable_manager, loader=loader)
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
stdout_callback="default",
)
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
How to resolve this dependency issue and make the python program work?
Upvotes: 0
Views: 1371
Reputation: 11
From what I saw in my environment I got this error when the script wasn't in my root directory.
[root@xxx API]# ./my_ansible.py
Traceback (most recent call last):
File "./my_ansible.py", line 5, in <module>
from ansible.parsing.dataloader import DataLoader
File "/home/xxx/projects/API/ansible.py", line 5, in <module>
ImportError: No module named parsing.dataloader
Once I moved it to /root - it ran fine:
[root@xxx ~]# ./my_ansible.py
[root@xxx ~]#
Upvotes: 1