【问题标题】:Ansible with items in rangeAnsible 范围内的项目
【发布时间】:2018-02-06 22:35:38
【问题描述】:

我想用 ansible 实现这样的目标

- debug:
    msg: "{{ item }}"
  with_items:
    - "0"
    - "1"

但是要从 range(2) 生成,而不是对迭代进行硬编码。你会怎么做呢?

【问题讨论】:

    标签: loops ansible


    【解决方案1】:
    - debug:
        var: item
      with_sequence: 0-1
    

      with_sequence: start=0 end=1
    

      with_sequence: start=0 count=2
    

    注意序列是字符串值,而不是整数(你可以使用item|int进行转换)

    参考:Looping over Integer Sequences

    【讨论】:

    • 太棒了,我在自欺欺人,而且有一个专门的循环命令。
    【解决方案2】:

    因为with_sequencelooprange 函数替换,你也可以像这个例子一样使用带范围函数的循环:

    - hosts: localhost
      tasks:
        - name: loop with range functions
          ansible.builtin.debug:
            msg: "{{ 'number: %s' | format(item) }}"
          loop: "{{ range(0, 2, 1)|list }}"
    

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      相关资源
      最近更新 更多