简体   繁体   English

将列表作为库存传递给 ansible_runner python 模块

[英]Pass a list as inventory to ansible_runner python module

I want to use ansible_runner to do some parsing on hosts.我想使用 ansible_runner 对主机进行一些解析。 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.我有一个脚本,它从数据库中收集主机列表,然后我想将该列表传递给 ansible_runner python 模块,而不将“库存”写入磁盘。

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.我只想将列表中的元素用作要使用的主机,在这种情况下执行 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?我的问题是如何将库存变量传递给 ansible_runner python 模块是否是 json 文件、列表、字典,它在磁盘上的任何位置都不存在? and let ansible connect to those.并让 ansible 连接到那些。

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
                                           

The ansible_runner.run() accepts following values for inventory parameter. ansible_runner.run()接受以下inventory参数值。

  1. Path to the inventory file in the private_data_dir private_data_dir 中库存文件的路径
  2. Native python dict supporting the YAML/json inventory structure本机 python dict 支持 YAML/json 库存结构
  3. A text INI formatted string文本 INI 格式的字符串
  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.如果未传递,此参数的默认值为private_data_dir/inventory目录。 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.在问题中给出的代码示例中,主机列表作为inventory参数的值传递,并且根据设计,列表值被视为库存源文件列表。

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) .可以使用 python 构建包含所有必需详细信息的字典,并作为ansible_runner.run(inventory=my_inventory)传递。

web_server and backend_server will become host group name. web_serverbackend_server将成为主机组名称。


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.注意:这样做会将内容保存在private_data_dir/inventory目录下的hosts.json文件中。

  • Write inventory file:写入库存文件:

Other way is writing host details in YAML/json format into a file inside private_data_dir/inventory directory.另一种方法是将 YAML/json 格式的主机详细信息写入private_data_dir/inventory目录中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM