0

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?

2
  • Don't overlap names in Python. Try to rename your script from ansible.py to my_ansible.py. Commented Nov 19, 2016 at 8:38
  • Thanks I has tried the same. But error persist Commented Nov 22, 2016 at 14:48

1 Answer 1

1

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 ~]#
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.