【问题标题】:How can I get the sequence number of host entry defined in inventory file (Ansible)?如何获取清单文件(Ansible)中定义的主机条目的序列号?
【发布时间】:2018-04-13 16:26:24
【问题描述】:

我有一个清单文件,其中两个主机定义如下

[testservers]
xx.xx.xx.106 ansible_ssh_user=johndoe
xx.xx.xx.138 ansible_ssh_user=johndoe
xx.xx.xx.141 ansible_ssh_user=johndoe

我想在任务中使用已定义主机的序列号。 就像xx.xx.xx.106 我应该得到序列1,对于xx.xx.xx.141 我应该得到序列值3,因为它是第三个条目。

如何在不定义其他变量的情况下获取清单文件中定义的主机条目的序列号?

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:

    您可以使用标准 Python 的 index 方法。

    用下面的变量替换字符串:

    debug:
      msg: "{{ groups['testservers'].index('xx.xx.xx.141') + 1 }}"
    

    +1 因为您明确要求从 1 到 3 的序列,但 index0 开头)

    【讨论】:

      【解决方案2】:

      这是一个使用 @techraf's answer 的 yaml 库存示例:

          fpm:
            vars:
              Offset: 31
            hosts:
              fpm[01:10]-dc1:
                  HostUnit: "{{groups['fpm'].index(inventory_hostname) + Offset}}"
                  targethost: "prox0{{HostUnit|int % 2 + 1}}-dc1"
                  ansible_ssh_host: "10.123.12.{{HostUnit}}"
      

      【讨论】:

        猜你喜欢
        • 2018-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-28
        • 2014-02-16
        • 2020-02-25
        • 1970-01-01
        相关资源
        最近更新 更多