當前位置: 首頁>>代碼示例>>Python>>正文


Python Host.set_variable方法代碼示例

本文整理匯總了Python中ansible.inventory.host.Host.set_variable方法的典型用法代碼示例。如果您正苦於以下問題:Python Host.set_variable方法的具體用法?Python Host.set_variable怎麽用?Python Host.set_variable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ansible.inventory.host.Host的用法示例。


在下文中一共展示了Host.set_variable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: my_add_group

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def my_add_group(self, hosts, groupname, groupvars=None):
        my_group = Group(name=groupname)
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)

        for host in hosts:
            hostname = host.get('hostname')
            hostip = host.get('ip', hostname)
            hostport = host.get('port', 22)
            username = host.get('username', 'root')
            password = host.get('password')
            ssh_key = host.get("ssh_key")
            my_host = Host(name=hostname, port=hostport)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)

            for key, value in host.iteritems():
                if key not in ['hostname', 'port', 'username', 'password']:
                    my_host.set_variable(key, value)
            my_group.add_host(my_host)
        self.inventory.add_group(my_group)
開發者ID:hatmen,項目名稱:pystudy,代碼行數:27,代碼來源:ansible_api.py

示例2: _create_implicit_localhost

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def _create_implicit_localhost(self, pattern):

        if self.localhost:
            new_host = self.localhost
        else:
            new_host = Host(pattern)

            # use 'all' vars but not part of all group
            new_host.vars = self.groups['all'].get_vars()

            new_host.address = "127.0.0.1"
            new_host.implicit = True

            if "ansible_python_interpreter" not in new_host.vars:
                py_interp = sys.executable
                if not py_interp:
                    # sys.executable is not set in some cornercases.  #13585
                    py_interp = '/usr/bin/python'
                    display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. '
                                    'You can correct this by setting ansible_python_interpreter for localhost')
                new_host.set_variable("ansible_python_interpreter", py_interp)

            if "ansible_connection" not in new_host.vars:
                new_host.set_variable("ansible_connection", 'local')

            self.localhost = new_host

        return new_host
開發者ID:ernstp,項目名稱:ansible,代碼行數:30,代碼來源:data.py

示例3: __init__

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def __init__(self, play, inventory, variable_manager, loader):
        self._lookup = dict()
        self._loader = loader
        self._play = play
        self._variable_manager = variable_manager

        hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)

        # check to see if localhost is in the hosts list, as we
        # may have it referenced via hostvars but if created implicitly
        # it doesn't sow up in the hosts list
        has_localhost = False
        for host in hosts:
            if host.name in C.LOCALHOST:
                has_localhost = True
                break

        if not has_localhost:
            new_host =  Host(name='localhost')
            new_host.set_variable("ansible_python_interpreter", sys.executable)
            new_host.set_variable("ansible_connection", "local")
            new_host.address = '127.0.0.1'
            hosts.append(new_host)

        for host in hosts:
            self._lookup[host.name] = host
開發者ID:source-foundry,項目名稱:code-corpora,代碼行數:28,代碼來源:hostvars.py

示例4: __init__

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def __init__(self, vars_manager, play, inventory, loader):
        self._lookup = {}
        self._loader = loader

        # temporarily remove the inventory filter restriction
        # so we can compile the variables for all of the hosts
        # in inventory
        restriction = inventory._restriction
        inventory.remove_restriction()
        hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)
        inventory.restrict_to_hosts(restriction)

        # check to see if localhost is in the hosts list, as we
        # may have it referenced via hostvars but if created implicitly
        # it doesn't sow up in the hosts list
        has_localhost = False
        for host in hosts:
            if host.name in C.LOCALHOST:
                has_localhost = True
                break

        # we don't use the method in inventory to create the implicit host,
        # because it also adds it to the 'ungrouped' group, and we want to
        # avoid any side-effects
        if not has_localhost:
            new_host =  Host(name='localhost')
            new_host.set_variable("ansible_python_interpreter", sys.executable)
            new_host.set_variable("ansible_connection", "local")
            new_host.ipv4_address = '127.0.0.1'
            hosts.append(new_host)

        for host in hosts:
            self._lookup[host.name] = vars_manager.get_vars(loader=loader, play=play, host=host, include_hostvars=False)
開發者ID:thebeefcake,項目名稱:masterless,代碼行數:35,代碼來源:hostvars.py

示例5: my_add_group

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def my_add_group(self, hosts, groupname, groupvars=None):
        """
        add hosts to a group
        """
        my_group = Group(name=groupname)

        # if group variables exists, add them to group
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)

        # add hosts to group
        for host in hosts:
            # set connection variables
            hostname = host.get("hostname")
            hostip = host.get('ip', hostname)
            hostport = host.get("port")
            username = host.get("username")
            password = host.get("password")
            ssh_key = host.get("ssh_key")
            my_host = Host(name=hostname, port=hostport)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)

            # set other variables 
            for key, value in host.iteritems():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)
            # add to group
            my_group.add_host(my_host)

        self.inventory.add_group(my_group)
開發者ID:jinrenlab,項目名稱:MagicStack,代碼行數:37,代碼來源:ansible_api.py

示例6: _parse_base_groups

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def _parse_base_groups(self):
        # FIXME: refactor

        ungrouped = Group(name='ungrouped')
        all = Group(name='all')
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)
        active_group_name = 'ungrouped'

        for line in self.lines:
            if line.startswith("["):
                active_group_name = line.replace("[","").replace("]","").strip()
                if line.find(":vars") != -1 or line.find(":children") != -1:
                    active_group_name = None
                else:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    all.add_child_group(new_group)
            elif line.startswith("#") or line == '':
                pass
            elif active_group_name:
                tokens = shlex.split(line)
                if len(tokens) == 0:
                    continue
                hostname = tokens[0]
                port = C.DEFAULT_REMOTE_PORT
                # Two cases to check:
                # 0. A hostname that contains a range pesudo-code and a port
                # 1. A hostname that contains just a port
                if (hostname.find("[") != -1 and
                    hostname.find("]") != -1 and
                    hostname.find(":") != -1 and
                    (hostname.rindex("]") < hostname.rindex(":")) or
                    (hostname.find("]") == -1 and hostname.find(":") != -1)):
                        tokens2  = hostname.rsplit(":", 1)
                        hostname = tokens2[0]
                        port     = tokens2[1]

                host = None
                _all_hosts = []
                if hostname in self.hosts:
                    host = self.hosts[hostname]
                    _all_hosts.append(host)
                else:
                    if detect_range(hostname):
                        _hosts = expand_hostname_range(hostname)
                        for _ in _hosts:
                            host = Host(name=_, port=port)
                            self.hosts[_] = host
                            _all_hosts.append(host)
                    else:
                        host = Host(name=hostname, port=port)
                        self.hosts[hostname] = host
                        _all_hosts.append(host)
                if len(tokens) > 1:
                    for t in tokens[1:]:
                        (k,v) = t.split("=")
                        host.set_variable(k,v)
                for _ in _all_hosts:
                    self.groups[active_group_name].add_host(_)
開發者ID:broferek,項目名稱:ansible,代碼行數:62,代碼來源:ini.py

示例7: add_host

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def add_host(self, host_ip, add_to_root=True, group_list=[], var_list={}):
        """Add a host ip to the ansible host file
        This is a simple function to add hosts to
        add_to_root = Adds the host to the root group (unnamed)
        groups_list: List of groupnames where the host should appears.
        var_list: Variable list. see allowed_variables."""
        #root group in unnamed, but in the inventory object
        # is the 'ungrouped' group
        self.__dirty = True
        new_host = Host(host_ip)
        for key,value in var_list.iteritems():
            if self.is_allowed_variable(key):
                new_host.set_variable(key,value)
        if add_to_root:
            if 'ungrouped' not in group_list:
                group_list.append('ungrouped')

        #Check groups. The ansible inventory should contain each of the groups.
        for group in group_list:
            if not self.__inventory.get_group(group):
                new_group= Group(group)
                self.__inventory.add_group(new_group)
            grp = self.__inventory.get_group(group)
            host_names = [host.name for host in grp.get_hosts()]
            if new_host.name not in host_names:
                grp.add_host(new_host)
開發者ID:AntBean,項目名稱:alienvault-ossim,代碼行數:28,代碼來源:ansibleinventory.py

示例8: _create_implicit_localhost

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
 def _create_implicit_localhost(self, pattern):
     new_host = Host(pattern)
     new_host.address = "127.0.0.1"
     new_host.vars = self.get_host_vars(new_host)
     new_host.set_variable("ansible_connection", "local")
     if "ansible_python_interpreter" not in new_host.vars:
         new_host.set_variable("ansible_python_interpreter", sys.executable)
     self.get_group("ungrouped").add_host(new_host)
     return new_host
開發者ID:davismathew,項目名稱:stableansible,代碼行數:11,代碼來源:__init__.py

示例9: _create_implicit_localhost

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
 def _create_implicit_localhost(self, pattern):
     new_host = Host(pattern)
     new_host.set_variable("ansible_python_interpreter", sys.executable)
     new_host.set_variable("ansible_connection", "local")
     ungrouped = self.get_group("ungrouped")
     if ungrouped is None:
         self.add_group(Group('ungrouped'))
         ungrouped = self.get_group('ungrouped')
     ungrouped.add_host(new_host)
     return new_host
開發者ID:grlee,項目名稱:ansible,代碼行數:12,代碼來源:__init__.py

示例10: _create_implicit_localhost

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
 def _create_implicit_localhost(self, pattern):
     # 如果pattern不在all禮拜中,則創建新的local Host,並添加到ungrouped組(沒有則創建),然後返回Host對象
     new_host = Host(pattern)
     new_host.set_variable("ansible_python_interpreter", sys.executable)
     new_host.set_variable("ansible_connection", "local")
     ungrouped = self.get_group("ungrouped")
     if ungrouped is None:
         self.add_group(Group('ungrouped'))
         ungrouped = self.get_group('ungrouped')
         self.get_group('all').add_child_group(ungrouped)
     ungrouped.add_host(new_host)
     return new_host
開發者ID:rainslytherin,項目名稱:ansible,代碼行數:14,代碼來源:__init__.py

示例11: _create_implicit_localhost

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def _create_implicit_localhost(self, pattern):
        new_host = Host(pattern)
        new_host.set_variable("ansible_python_interpreter", sys.executable)
        new_host.set_variable("ansible_connection", "local")
        new_host.ipv4_address = "127.0.0.1"

        ungrouped = self.get_group("ungrouped")
        if ungrouped is None:
            self.add_group(Group("ungrouped"))
            ungrouped = self.get_group("ungrouped")
            self.get_group("all").add_child_group(ungrouped)
        ungrouped.add_host(new_host)
        return new_host
開發者ID:thebeefcake,項目名稱:masterless,代碼行數:15,代碼來源:__init__.py

示例12: _create_implicit_localhost

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
 def _create_implicit_localhost(self, pattern='localhost'):
     new_host = Host(pattern)
     new_host.address = "127.0.0.1"
     new_host.implicit = True
     new_host.vars = self.get_host_vars(new_host)
     new_host.set_variable("ansible_connection", "local")
     if "ansible_python_interpreter" not in new_host.vars:
         py_interp = sys.executable
         if not py_interp:
             # sys.executable is not set in some cornercases.  #13585
             display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. You can correct this by setting ansible_python_interpreter for localhost')
             py_interp = '/usr/bin/python'
         new_host.set_variable("ansible_python_interpreter", py_interp)
     self.get_group("ungrouped").add_host(new_host)
     return new_host
開發者ID:LukeInkster,項目名稱:PythonCorpus,代碼行數:17,代碼來源:__init__.py

示例13: _parse_base_groups

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def _parse_base_groups(self):

        ungrouped = Group(name='ungrouped')
        all = Group(name='all')
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)
        active_group_name = 'ungrouped'

        for line in self.lines:
            if line.startswith("["):
                active_group_name = line.replace("[","").replace("]","").strip()
                if line.find(":vars") != -1 or line.find(":children") != -1:
                    active_group_name = None
                else:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    all.add_child_group(new_group)
            elif line.startswith("#") or line == '':
                pass
            elif active_group_name:
                tokens = line.split()
                if len(tokens) == 0:
                    continue
                hostname = tokens[0]
                port = C.DEFAULT_REMOTE_PORT
                if hostname.find(":") != -1:
                   tokens2  = hostname.split(":")
                   hostname = tokens2[0]
                   port     = tokens2[1]
                host = None
                if hostname in self.hosts:
                    host = self.hosts[hostname]
                else:
                    host = Host(name=hostname, port=port)
                    self.hosts[hostname] = host
                if len(tokens) > 1:
                   for t in tokens[1:]:
                      (k,v) = t.split("=")
                      host.set_variable(k,v)
                self.groups[active_group_name].add_host(host)
開發者ID:Kafkamorph,項目名稱:n-repo,代碼行數:42,代碼來源:ini.py

示例14: run_module

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
def run_module(hosts, module_name, args, **complex_args):
    import ansible.inventory
    pattern = "all"
    inventory = ansible.inventory.Inventory(',')
    
    all_group = inventory.get_group("all")
    from ansible.inventory.host import Host
    for h in hosts:
        host = Host(h["host"], h.get("port"))
        for key, value in h["settings"].iteritems():
            host.set_variable(key, value)
    
        all_group.add_host(host)
    
    # в конструкторе кэш посчитался заранее
    inventory.clear_pattern_cache()
    
    # сам Ansible использует отдельный Runner для каждого действия в playbook,
    # так что вот готов отдельный кирпичик для системы развертывания
    import ansible.runner 
    runner = ansible.runner.Runner(
        module_name=module_name,
        module_args=args,
        complex_args=complex_args,
        pattern=pattern,
        inventory=inventory,
        
    )
    results = runner.run()
    
    # :COPY_N_PASTE_REALIZATION: bin/ansible
    if results['dark']:
        assert False, "Couldn't connect to %s" % results['dark']
    total = 0
    for result in results['contacted'].values():
        if 'failed' in result or result.get('rc', 0) != 0:
            assert False, result
        total += 1
    assert total == len(hosts), "The host was not connected to"
開發者ID:muravjov,項目名稱:disvolvu,代碼行數:41,代碼來源:setup_container.py

示例15: _create_implicit_localhost

# 需要導入模塊: from ansible.inventory.host import Host [as 別名]
# 或者: from ansible.inventory.host.Host import set_variable [as 別名]
    def _create_implicit_localhost(self, pattern):

        if self.localhost:
            new_host = self.localhost
        else:
            new_host = Host(pattern)

            new_host.address = "127.0.0.1"
            new_host.implicit = True

            # set localhost defaults
            py_interp = sys.executable
            if not py_interp:
                # sys.executable is not set in some cornercases. see issue #13585
                py_interp = '/usr/bin/python'
                display.warning('Unable to determine python interpreter from sys.executable. Using /usr/bin/python default. '
                                'You can correct this by setting ansible_python_interpreter for localhost')
            new_host.set_variable("ansible_python_interpreter", py_interp)
            new_host.set_variable("ansible_connection", 'local')

            self.localhost = new_host

        return new_host
開發者ID:awiddersheim,項目名稱:ansible,代碼行數:25,代碼來源:data.py


注:本文中的ansible.inventory.host.Host.set_variable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。