如何从清单python代码中请求特定主机和组
如何调用该清单文件的特定主机和组 示例:如何从清单python代码中请求特定主机和组,python,ansible,ansible-2.x,ansible-inventory,Python,Ansible,Ansible 2.x,Ansible Inventory,如何调用该清单文件的特定主机和组 示例:main.yml hosts: group roles: - java_installation 我的用法: ansible-playbook inventory main.yml 我不想将主机添加到main.yml 主机和组应在运行时决定。 如何将输入传递到此示例\u inventory()?当我运行时是否可以传递库存 ansible playbook inventory main.yml? 例如:在数据库中,组有5个服务器。但我想从组中选择单个
main.yml
hosts: group
roles:
- java_installation
我的用法:
ansible-playbook inventory main.yml
我不想将主机添加到main.yml
主机和组应在运行时决定。如何将输入传递到此
示例\u inventory()
?当我运行时是否可以传递库存
ansible playbook inventory main.yml
?例如:在数据库中,组有5个服务器。但我想从组中选择单个服务器并运行角色
[group]
10.2.3.4.
10.2.3.5
我想在10.2.3.5
你能帮我一下吗。对不起,如果我的通讯有问题
示例代码:
#!/usr/bin/env python
'''
Example custom dynamic inventory
'''
import os
import sys
import argparse
try:
import json
except ImportError:
import simplejson as json
class ExampleInventory(object):
def __init__(self):
self.inventory = {}
self.read_cli_args()
# Called with `--list`.
if self.args.list:
self.inventory = self.example_inventory()
# Called with `--host [hostname]`.
elif self.args.host:
# Not implemented, since we return _meta info `--list`.
self.inventory = self.empty_inventory()
# If no groups or vars are present, return an empty inventory.
else:
self.inventory = self.empty_inventory()
print json.dumps(self.inventory);
# Example inventory for testing.
def example_inventory(self):
my_array = ['xx.xx.xxx', 'xx.xx.xxx']
return {
'group': {
'hosts': my_array,
'vars': {
'ansible_ssh_user': 'username'
}
},
'_meta': {
'hostvars': {
'xx.xx.xxx': {
'ansible_ssh_pass':'password'
},
'xx.xxx.xx': {
'ansible_ssh_pass':'password'
}
}
}
}
# Empty inventory for testing.
def empty_inventory(self):
return {'_meta': {'hostvars': {}}}
# Read the command line args passed to the script.
def read_cli_args(self):
parser = argparse.ArgumentParser()
parser.add_argument('--list', action = 'store_true')
parser.add_argument('--host', action = 'store')
self.args = parser.parse_args()
# Get the inventory.
ExampleInventory()
可能重复我正在寻找通过编码。我需要在运行时在清单python代码中添加主机条目。这是我在描述中添加的。可能与我正在查看的代码重复。我需要在运行时在清单python代码中添加主机条目。这是我在描述中添加的。