生成责任库存
ansinv的Python项目详细描述
一个简单的ansible库存生成器
=安装=
pip install ansinv
=使用清单主机=
使用可选的host variables:
创建主机host1 = ansinv.AnsibleHost("192.168.10.11", affinity=12, scan="no")
使用AnsibleHost.name属性获取主机的IP/名称:
print(host1.name)
使用AnsibleHost.hostvars属性读取/更新主机的主机变量:
print(host1.hostvars["scan"]) host1.hostvars["affinity"] = 5 host1.hostvars.update(x=100)
=与库存组一起工作=
创建具有可选group variables:
的组group1 = ansinv.AnsibleGroup("group1", ssh_port=8800)
使用AnsibleGroup.name属性获取组名:
print(group1.name)
使用AnsibleGroup.groupvars属性读取/更新组的组变量:
print(group1.groupvars["ssh_port"]) group1.groupvars["ssh_port"] = 22 group1.groupvars.update(x=100)
使用AnsibleGroup.add_hosts方法将主机添加到组:
group1.add_hosts(host1, host2, ...) # host1, host2, etc. must already exist group1.add_hosts(ansinv.AnsibleHost("192.168.12.12", hostvar1="value")) # creating and adding hosts at the same time
请注意:添加主机实际上会创建组下主机对象的副本。因此,要在添加宿主对象后对其进行修改,请使用AnsibleGroup.host方法,如下所述。
使用AnsibleGroup.host('hostname')方法访问成员主机:
group1.host("192.168.1.12").hostvars["hostvar1"] = "new value"
请注意:即使组中有多个同名主机,host()方法也将始终返回给定“主机名”的第一个匹配项。此行为假定即使允许您拥有多个同名主机,但实际上您永远不会需要这样的情况。
使用AnsibleGroup.hosts属性获取组中所有主机对象的列表:
print(group1.hosts[0].name)
使用AnsibleGroup.add_children方法在组之间建立父子关系:
child1 = AnsibleGroup("master") child2 = AnsibleGroup("worker") parent = AnsibleGroup("cluster") parent.add_children(child1, child2) parent.add_children(parent) # ValueError when trying to add itself as a child child1.add_children(parent) # ValueError when trying to add a parent group as a child
使用AnsibleGroup.is_parent_of方法检查组是否为给定组的父级:
group1.is_parent_of(group2) # Returns a bool value
使用AnsibleGroup.is_child_of方法检查组是否是给定组的子组:
group1.is_child_of(group2) # Returns a bool value
使用AnsibleGroup.children属性获取所有子对象的列表:
print(group1.children[0].name)
=使用库存本身=
创建库存:
inv = AnsibleInventory() # empty inventory inv = AnsibleInventory(AnsibleHost("h1"), AnsibleHost("h2")) # inventory initialized with two ungrouped hosts
使用AnsibleInventory.add_hosts方法将(未分组的)主机添加到资源清册:
h1 = AnsibleHost("h1") h2 = AnsibleHost("h2") inv.add_hosts(h1, h2)
请注意:直接添加到资源清册的主机是“未分组”主机,即它们不会出现在其他组下。
使用AnsibleInventory.add_groups方法将组添加到库存:
g1 = AnsibleGroup("g1") g2 = AnsibleGroup("g2") inv.add_groups(g1, g2)
请注意:添加主机/组实际上会在清单下创建主机/组对象的副本。因此,要在添加主机/组对象后对其进行修改,请使用AnsibleInventory.host(hostname)/AnsibleInventory.group(groupname)方法,如下所述。
使用AnsibleInventory.host方法从清单中获取一个未分组的主机对象:
print(inv.host("h1")) inv.host("h1").hostvars["somevar"] = 111 # modify an ungrouped host after it has been added to the inventory
使用AnsibleInventory.group('groupname')方法从清单中获取组对象:
inv.group("g1").groupvars["x"] = 1111 inv.group("g1").host("h1").hostvars["somevar"] = 333
请注意:即使清单中有多个同名组,group()方法始终返回给定“group name”的第一个匹配项。此行为假定即使允许您拥有多个同名组,但实际上您永远不会需要这样的情况。
使用AnsibleInventory.groups属性从清单中获取所有组对象的列表:
for grp in inv.groups: print(grp.name)
将整个库存作为字符串对象:
清单的字符串版本是ini格式,您可以简单地将其写入文件并传递给ansible。
inv = AnsibleInventory() ... # add some groups and hosts print(str(inv)) with open("inventory", "w") as f: f.write(str(inv))
更多说明解剖和完整的例子请访问wiki page。