1

I want to use ansible_runner to do some parsing on hosts. I have a script which gathers a list of hosts from a database and then I want to pass that list to ansible_runner python module without writing the "inventory" to disk.

I tried to do like this based on what I could understand from the documentation:

>> import ansible_runner
>> hostlist = ['host1', 'host2']
>>> r = ansible_runner.run(private_data_dir='.',inventory=hostlist, playbook='check_ping.yml')

I appears that each element in the list that I pass is taken as if it was an inventory file located in the inventory directory. I just would like to use the elements of the list as hosts to be used and in this case do a ping.

my question is how to pass to the ansible_runner python module an inventory variable whether is a json file, list, dictionary that it does not exists anywhere on disk? and let ansible connect to those.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
danidar
  • 179
  • 2
  • 10

2 Answers2

2

Build a nested dictionary as shown. Give an Iterable hostsiwant

hosts = {r:None for r in hostsiwant}
inv = {'all': {'hosts': hosts}}
r = ansible_runner.run(inventory=inv, #remaining arguments as needed
                                           
gerardw
  • 5,822
  • 46
  • 39
0

The ansible_runner.run() accepts following values for inventory parameter.

  1. Path to the inventory file in the private_data_dir
  2. Native python dict supporting the YAML/json inventory structure
  3. A text INI formatted string
  4. A list of inventory sources, or an empty list to disable passing inventory

Default value, if not passed, for this parameter is private_data_dir/inventory directory. Passing this parameter overrides the inventory directory/file. Documentation here

In the code sample given in the question, a list of hosts is passed as a value for inventory param and as per design, the list values are considered as list of inventory source files.

Examples:

  • Passing inventory as dictionary:

A dictionary with all required details can be built using python and passed as ansible_runner.run(inventory=my_inventory).

web_server and backend_server will become host group name.


import ansible_runner

my_inventory = {
  "web_server": {
    "hosts": {
      "webserver_1.example.com": {
        "ansible_user": "test",
        "ansible_ssh_private_key_file": "test_user.pem",
        "ansible_host": "webserver_1.example.com"
      },
      "webserver_2.example.com": {
        "ansible_user": "test",
        "ansible_ssh_private_key_file": "test_user.pem",
        "ansible_host": "webserver_1.example.com"
      }
    }
  },
  "backend_server": {
    "hosts": {
      "backend_server_1.example.com": {
        "ansible_user": "test",
        "ansible_ssh_private_key_file": "test_user.pem",
        "ansible_host": "backend_server_1.example.com"
      }
    }
  }
}

runner_result = ansible_runner.run(private_data_dir='.', inventory=my_inventory, playbook='check_ping.yml')
print(runner_result.stats)

Note: Doing this will save the contents in hosts.json file in private_data_dir/inventory directory.

  • Write inventory file:

Other way is writing host details in YAML/json format into a file inside private_data_dir/inventory directory.

arunkvelu
  • 1,631
  • 10
  • 21