2

Here is my code: I am not able to run a ansible module using python. How to pass a inventory file for which this command is running. I am not able to run it for my inventory. Do I need to do something else ? Here is my ansible command:

ansible all -i /home/ubuntu/extra -m 'debug' -a 'var=hostvars' 

Here is my code:

import json
import ansible.runner
import ansible.playbook
import ansible.inventory

hosts = ["10.12.11.101"]
example_inventory = ansible.inventory.Inventory(hosts)
pm = ansible.runner.Runner( module_name = 'debug', module_args = 'vars=hostvars', timeout = 5, inventory = example_inventory, subset = 'all')
out = pm.run()
print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))
kohi
  • 936
  • 2
  • 12
  • 31
  • what do you mean by you are not able to run it for your inventory ? what was the error occured ? have you tried passing your inventory file to`ansible.inventory.Inventory` ? – frank Nov 05 '15 at 07:36
  • its a python import error, how did you installed ansible ? – frank Nov 05 '15 at 08:53
  • And , post the error occured along with your question, it is the way questions are asked in SO. – frank Nov 05 '15 at 08:59
  • How to pass inventory file? global name 'inventory' is not defined – kohi Nov 05 '15 at 09:14
  • you can pass the inventory file just like you did with your `hosts` list. just pass the path to your inventory file. make sure you have installed ansible correctly. i suggest use `pip`. – frank Nov 05 '15 at 09:17
  • I installed it correctly. But no idea how to pass a -i file on which I run this ansible – kohi Nov 05 '15 at 09:19
  • have you tried passing your inventory file to `ansible.inventory.Inventory`? – frank Nov 05 '15 at 09:20
  • When I give a path as inventory="/home/ubuntu/common_shared" it gives me error – kohi Nov 05 '15 at 09:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94299/discussion-between-frank-and-kohi). – frank Nov 05 '15 at 09:22
  • Does this answer your question? [Running ansible-playbook using Python API](https://stackoverflow.com/questions/27590039/running-ansible-playbook-using-python-api) – user7610 Oct 21 '22 at 08:46

2 Answers2

4

You can pass the inventory file path to ansible.runner.Runner()

And for getting group-names and host-names , you should pass var=hostvars, not vars=hostvars

Your code would look like this,

import json
import ansible.runner
import ansible.playbook
import ansible.inventory

example_inventory = ansible.inventory.Inventory('path/to/your/inventory')
pm = ansible.runner.Runner( module_name = 'debug', module_args = 'var=hostvars', timeout = 5, inventory = example_inventory, subset = 'all')
out = pm.run()
print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))

and your output

{'contacted': {'ip-address': {'invocation': {'module_args': u'var=hostvars',
    'module_complex_args': {},
    'module_name': 'debug'},
   'var': {u'hostvars': {'group_names': ['group1', 'group2', 'group3'],
     'groups': {'group1': ['ip-address'],
      'all': ['ip-address'],
      'group2': ['ip-address'],
      'group3': ['ip-address'],
      'ungrouped': []},
     'inventory_hostname': 'ip/hostname',
     'inventory_hostname_short': 'hostname-short'}},
   'verbose_always': True}},
 'dark': {}}
frank
  • 656
  • 3
  • 12
0

Nowadays, it is best to go through the ansible_runner package, (https://github.com/ansible/ansible-runner), instead of importing ansible directly. See my answer to Running ansible-playbook using Python API for a complete example.

user7610
  • 25,267
  • 15
  • 124
  • 150