本文整理汇总了Python中ansible.inventory.group.Group.vars方法的典型用法代码示例。如果您正苦于以下问题:Python Group.vars方法的具体用法?Python Group.vars怎么用?Python Group.vars使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ansible.inventory.group.Group
的用法示例。
在下文中一共展示了Group.vars方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _add_group
# 需要导入模块: from ansible.inventory.group import Group [as 别名]
# 或者: from ansible.inventory.group.Group import vars [as 别名]
def _add_group(self, host, result_item):
'''
Helper function to add a group (if it does not exist), and to assign the
specified host to that group.
'''
changed = False
# the host here is from the executor side, which means it was a
# serialized/cloned copy and we'll need to look up the proper
# host object from the master inventory
real_host = self._inventory.get_host(host.name)
group_name = result_item.get('add_group')
new_group = self._inventory.get_group(group_name)
if not new_group:
# create the new group and add it to inventory
new_group = Group(name=group_name)
self._inventory.add_group(new_group)
new_group.vars = self._inventory.get_group_vars(new_group)
# and add the group to the proper hierarchy
allgroup = self._inventory.get_group('all')
allgroup.add_child_group(new_group)
changed = True
if group_name not in host.get_groups():
new_group.add_host(real_host)
changed = True
return changed
示例2: _add_host
# 需要导入模块: from ansible.inventory.group import Group [as 别名]
# 或者: from ansible.inventory.group.Group import vars [as 别名]
def _add_host(self, host_info):
'''
Helper function to add a new host to inventory based on a task result.
'''
host_name = host_info.get('host_name')
# Check if host in cache, add if not
if host_name in self._inventory._hosts_cache:
new_host = self._inventory._hosts_cache[host_name]
else:
new_host = Host(name=host_name)
self._inventory._hosts_cache[host_name] = new_host
allgroup = self._inventory.get_group('all')
allgroup.add_host(new_host)
# Set/update the vars for this host
# FIXME: probably should have a set vars method for the host?
new_vars = host_info.get('host_vars', dict())
new_host.vars = self._inventory.get_host_vars(new_host)
new_host.vars.update(new_vars)
new_groups = host_info.get('groups', [])
for group_name in new_groups:
if not self._inventory.get_group(group_name):
new_group = Group(group_name)
self._inventory.add_group(new_group)
new_group.vars = self._inventory.get_group_variables(group_name)
else:
new_group = self._inventory.get_group(group_name)
new_group.add_host(new_host)
# add this host to the group cache
if self._inventory.groups is not None:
if group_name in self._inventory.groups:
if new_host not in self._inventory.get_group(group_name).hosts:
self._inventory.get_group(group_name).hosts.append(new_host.name)
# clear pattern caching completely since it's unpredictable what
# patterns may have referenced the group
# FIXME: is this still required?
self._inventory.clear_pattern_cache()
示例3: _add_group
# 需要导入模块: from ansible.inventory.group import Group [as 别名]
# 或者: from ansible.inventory.group.Group import vars [as 别名]
def _add_group(self, task, iterator):
'''
Helper function to add a group (if it does not exist), and to assign the
specified host to that group.
'''
# the host here is from the executor side, which means it was a
# serialized/cloned copy and we'll need to look up the proper
# host object from the master inventory
groups = {}
changed = False
for host in self._inventory.get_hosts():
original_task = iterator.get_original_task(host, task)
all_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=original_task)
templar = Templar(loader=self._loader, variables=all_vars)
group_name = templar.template(original_task.args.get('key'))
if task.evaluate_conditional(templar=templar, all_vars=all_vars):
if group_name not in groups:
groups[group_name] = []
groups[group_name].append(host)
for group_name, hosts in iteritems(groups):
new_group = self._inventory.get_group(group_name)
if not new_group:
# create the new group and add it to inventory
new_group = Group(name=group_name)
self._inventory.add_group(new_group)
new_group.vars = self._inventory.get_group_vars(new_group)
# and add the group to the proper hierarchy
allgroup = self._inventory.get_group('all')
allgroup.add_child_group(new_group)
changed = True
for host in hosts:
if group_name not in host.get_groups():
new_group.add_host(host)
changed = True
return changed
示例4: run
# 需要导入模块: from ansible.inventory.group import Group [as 别名]
# 或者: from ansible.inventory.group.Group import vars [as 别名]
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
if self.runner.noop_on_check(inject):
return ReturnData(
conn=conn, comm_ok=True, result=dict(skipped=True, msg="check mode not supported for this module")
)
args = {}
if complex_args:
args.update(complex_args)
args.update(parse_kv(module_args))
if not "hostname" in args and not "name" in args:
raise ae("'name' is a required argument.")
result = {}
# Parse out any hostname:port patterns
new_name = args.get("name", args.get("hostname", None))
vv("creating host via 'add_host': hostname=%s" % new_name)
if ":" in new_name:
new_name, new_port = new_name.split(":")
args["ansible_ssh_port"] = new_port
# redefine inventory and get group "all"
inventory = self.runner.inventory
allgroup = inventory.get_group("all")
# check if host in cache, add if not
if new_name in inventory._hosts_cache:
new_host = inventory._hosts_cache[new_name]
else:
new_host = Host(new_name)
# only groups can be added directly to inventory
inventory._hosts_cache[new_name] = new_host
allgroup.add_host(new_host)
groupnames = args.get("groupname", args.get("groups", args.get("group", "")))
# add it to the group if that was specified
if groupnames:
for group_name in groupnames.split(","):
group_name = group_name.strip()
if not inventory.get_group(group_name):
new_group = Group(group_name)
inventory.add_group(new_group)
new_group.vars = inventory.get_group_variables(group_name, vault_password=inventory._vault_password)
grp = inventory.get_group(group_name)
grp.add_host(new_host)
# add this host to the group cache
if inventory._groups_list is not None:
if group_name in inventory._groups_list:
if new_host.name not in inventory._groups_list[group_name]:
inventory._groups_list[group_name].append(new_host.name)
vv("added host to group via add_host module: %s" % group_name)
result["new_groups"] = groupnames.split(",")
# actually load host vars
new_host.vars = combine_vars(
new_host.vars,
inventory.get_host_variables(new_name, update_cached=True, vault_password=inventory._vault_password),
)
# Add any passed variables to the new_host
for k in args.keys():
if not k in ["name", "hostname", "groupname", "groups"]:
new_host.set_variable(k, args[k])
result["new_host"] = new_name
# clear pattern caching completely since it's unpredictable what
# patterns may have referenced the group
inventory.clear_pattern_cache()
return ReturnData(conn=conn, comm_ok=True, result=result)