【问题标题】:Ansible Variables only X times per hostAnsible 变量每台主机仅 X 次
【发布时间】:2016-08-09 11:19:40
【问题描述】:

我实际上是在编写一个 ansible playbook,用于在一台主机上自动安装具有多个相同实例的 Docker Container。

我有一个带有“客户”的变量文件,每个客户都有一个自己的 docker 容器,我的想法是:我有一堆主机,ansible 使用 5 个不同的容器为每个主机部署 5 个实例。当然,我可以用主机名定义一个新的 var,但它似乎更聪明一点,ansible 检查:“好的,已经有 5 个实例,下一个主机”。

有什么想法吗?

【问题讨论】:

    标签: docker ansible


    【解决方案1】:

    可能的解决方案:

    ---
    - hosts: all
      gather_facts: no
      vars:
        containers: ['cont1', 'cont2', 'cont3', 'cont4', 'cont5', 'cont6', 'cont7', 'cont8']
        per_host: 3
      tasks:
        - debug: msg='Deploy {{ item }}'
          with_items: '{{ containers[ play_hosts.index(inventory_hostname)*per_host : (play_hosts.index(inventory_hostname)+1)*per_host] }}'
    

    这将采用containers 列表的一部分,其中per_host 基于大小和主机索引作为偏移量。
    因此,在此示例中,您的清单中每个主机将收到不超过 3 个调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-22
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多