当前位置: 首页 > 知识库问答 >
问题:

如何使用Ansible-Playbook--limit的IP地址,而不是主机名?

齐兴运
2023-03-14
foo-host ansible_host=1.2.3.4 some_var=bla
bar-host ansible_host=5.6.7.8 some_var=blup
$ ansible-playbook playbook.yml --limit foo-host
$ ansible-playbook playbook.yml --limit 1.2.3.4
ERROR! Specified hosts and/or --limit does not match any hosts

有没有办法让这起作用?修改IP地址(例如IP_1_2_3_4)是可以接受的。

我考虑过的事情:

>

  • 打开它,并根据IP地址标识所有主机:

    1.2.3.4 some_var=bla
    5.6.7.8 some_var=blup
    
  • 共有1个答案

    夏侯元忠
    2023-03-14

    更好的解决方案仍然受到欢迎,但目前我正在使用一个小的库存插件来实现这一点,该插件(与库存脚本相反)确实可以访问以前添加的库存:

    plugins/inventory/ip_based_groups.py

    import os.path
    import re
    
    from ansible.plugins.inventory import BaseInventoryPlugin
    from ansible.inventory.group import Group
    
    
    PATH_PLACEHOLDER = 'IP_BASED_GROUPS'
    IP_RE = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
    
    
    class InventoryModule(BaseInventoryPlugin):
        '''
        This inventory plugin does not create any hosts, but just adds groups based
        on IP addresses. For each host whose ansible_host looks like an IPv4
        address (e.g. 1.2.3.4), a corresponding group is created by prefixing the
        IP address with 'ip_' and replacing dots by underscores (e.g. ip_1_2_3_4).
    
        Use it by putting the literal string IP_BASED_GROUPS at the end of the list
        of inventory sources.
        '''
    
        NAME = 'ip_based_groups'
    
        def verify_file(self, path):
            return self._is_path_placeholder(path)
    
        def parse(self, inventory, loader, path, cache=True):
            if not self._is_path_placeholder(path):
                return
            for host_name, host in inventory.hosts.items():
                ansible_host = host.vars.get('ansible_host', '')
                if self._is_ip_address(ansible_host):
                    group = 'ip_' + ansible_host.replace('.', '_')
                    inventory.add_group(group)
                    inventory.add_host(host_name, group)
    
        def _is_path_placeholder(self, path):
            return os.path.basename(path) == PATH_PLACEHOLDER
    
        def _is_ip_address(self, s):
            return bool(IP_RE.match(s))
    

    ansible.cfg

    [defaults]
    
    # Load plugins from these directories.
    inventory_plugins = plugins/inventory
    
    # Directory that contains all inventory files, and placeholder to create
    # IP-based groups.
    inventory = inventory/,IP_BASED_GROUPS
    
    [inventory]
    
    # Enable our custom inventory plugin.
    enable_plugins = ip_based_groups, host_list, script, auto, yaml, ini, toml
    
     类似资料:
    • 但是通过使用,我无法从目录中挑选特定的主机。但是我可以使用一些语法,如,但是它查找DNS条目并相应地执行,但是不会检查目录文件。 另一种方法是为每个主机创建单独的目录文件,并由开发人员根据请求的主机调用它们,我不知道这是多少推荐的方法。 最后一个方法是AFAIK通过Python使用,在主机名中使用模式并调用Python程序。 除了这些,您还知道在执行剧本时从目录中选择特定主机/主机组的其他方法吗?

    • 如何使用库存文件中的主机名和IP地址的值? 例如,我在文件中只有一个主机,名称为 FQDN,但这是在 DNS 服务器上注册的。 我尝试了一些变量,但总是得到主机名。但是,需要他们两个:( My test.yml file: 输出是:

    • 我必须在ConsumerGroup中设置zookeeper IP地址,而不是Kafka-Host IP地址。因为我将复制因子设置为3,并且创建了3个代理。因此,如果一个主机出现故障,那么另一个主机可以接管。 当我尝试将zookeeper的IP地址而不是Kafka-Host的IP地址放在ConsumerGroup中时,它不会接收到任何来自生产者API的消息。 我希望这样,如果我在ConsumerGr

    • 问题内容: 我有DNS服务器IP地址和主机名。 使用Java,如何使用该IP地址和主机名找到该DNS服务器返回的主机名的IP地址? 问题答案: 看看和方法。

    • 问题内容: 我的主机文件(C:\ WINDOWS \ system32 \ drivers \ etc \ hosts)具有一堆IP地址到主机名的映射: 我正在尝试找到一种通过Java API以编程方式从IPAddress转换为HostName的方法。 伪代码: 问题答案: 我从这里尝试了代码,它可以工作。即:

    • 主机名到IP地址的解析接口 函数 struct hostent *  gethostbyname (const char *hostname)   获取主机地址   详细描述 主机名到IP地址的解析接口 函数说明 struct hostent* gethostbyname ( const char *  hostname ) 获取主机地址 参数 hostname 一个字符串指针,包含域名或主机名