生成责任库存

ansinv的Python项目详细描述


pic1pic2pic3pic4

一个简单的ansible库存生成器

=概述=

这个简单的库使得在一键式部署的基础设施bringup/orchestration和软件配置阶段之间编写glue代码变得更加容易。

转到wiki page获取有关此项目的更多说明。

=安装=

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java计数字符串中的符号数   如何将(Android)应用程序上下文传递给Java类?   arraylist中的java查找索引始终位于1   java如何在Grpc中通过MethodDescriptor的InputStream调用服务器?   java Netbeans、Glassfish部署问题   java在安卓上拖放一个按钮。。帮助   java Hibernate不从映射表填充数据   数组用户定义的文件读取函数导致NullPointerException Java   java组织。冬眠ErrorClassException:对象[id=null]不属于指定的子类   java中迭代破坏二叉树的算法   openssl如何通过java确保使用哪个私钥创建服务器证书   伏地魔项目的java对象映射   java这是JSlider中的一个bug吗?   java Spring究竟如何使用JDK代理?   java跨JVM分发订阅者   java Mockito void方法如何检查私有变量?   java如何在安卓 Studio中启用httpclient?   java软件设计原则:将您的收藏与处理对象分开?   java Exe包装的JavaFX应用程序(使用Launch4J)不会打开其他阶段   读取Java中可能被其他进程修改的文件