【问题标题】:Ansible - Repeat a list for N timesAnsible - 重复一个列表 N 次
【发布时间】:2022-08-02 19:23:25
【问题描述】:

我有一个主要的剧本,它使用include 在满足条件时调用其他剧本。这工作正常,但我需要将这些剧本执行n 次,其中n 是用户输入变量。因此,如果用户输入“5”,主 playbook 将调用 playbooks 5 次。

这是一个例子:

---
- name: main playbook
  hosts: localhost
  connection: local
  gather_facts: False
  var_files: weqwewq

  tasks:
    - include: 1.yml
      when: x == \"aaa\"
    - include: 2.yml
      when: x == \"bbb\"
    - include: 3.yml
      when: x == \"ccc\"
    - include: 4.yml
      when: x == \"ddd\"

我不需要的是这个:

      tasks:
        - include: 1.yml
          when: x == \"aaa\"
          with_sequence: count= \"{{ user_input }}\"
        - include: 2.yml
          when: x == \"aaa+bbb\"
          with_sequence: count= \"{{ user_input }}\"
        - include: 3.yml
          when: x == \"ccc\"
          with_sequence: count= \"{{ user_input }}\"
        - include: 4.yml
          when: x == \"ccc+ddd\"
          with_sequence: count= \"{{ user_input }}\"

而是像这样

    tasks:
      with_sequence: count= \"{{ user_input }}\"
        - include: 1.yml
          when: x == \"aaa\"
        - include: 2.yml
          when: x == \"aaa+bbb\"
        - include: 3.yml
          when: x == \"ccc\"
        - include: 4.yml
          when: x == \"ccc+ddd\"

但为此我得到一个错误:

\"with_sequence 不是播放的有效属性\"。

任何想法?

谢谢!

    标签: list loops ansible


    【解决方案1】:

    只需添加另一个include 级别,然后将您的序列循环添加到该任务。


    首先,注意include 模块has been deprecated。你应该使用include_tasks

    要使用单个循环结构循环您的任务,请从顶级剧本开始,如下所示:

    - hosts: localhost
      gather_facts: false
      tasks:
        - include_tasks: tasks.yaml
          with_sequence: count=5
    

    tasks.yaml 中,放置您的include_tasks 并附上条件:

    - name: show loop index
      debug:
        var: item
    
    - include_tasks: tasks_1.yaml
      when: x = 'aaa'
    - include_tasks: tasks_2.yaml
      when: x = 'bbb'
    - include_tasks: tasks_3.yaml
      when: x = 'ccc'
    

    然后根据需要创建单独的 task_N.yaml 文件

    【讨论】:

    • 谢谢!这是问题的一些扩展,也许你也可以帮助解决这个问题?我想在每个循环中更改一些变量,所以如果只有一次运行/循环,它将是“01”,如果是第 5 次运行,它将是“05”......我该怎么做?
    • 在循环内部,变量item 将包含当前循环索引。我已经更新了答案以显示这一点。
    【解决方案2】:

    欢迎来到 Stackoverflow。您可以使用Interactive Input 将 N 作为输入。此外,您可以考虑查看this 以获取有关 ansible 中变量使用的更多信息。

    此外,good practice 使用任务来组织您的代码。 检查以下代码:

    更新:我在with_sequence 中添加了stride 参数,因此现在可以将增量值设置为1(默认值)以外的值。

    您的主要剧本文件:

    ---
    - name: main playbook
      hosts: localhost
      connection: local
      gather_facts: False
      vars_prompt:
    
        - name: counter
          prompt: Enter loop counter
          private: no
    
    
      tasks:
        - include_tasks: tasks.yml
          with_sequence: start=1 end="{{ counter }}" stride="{{ 1 }}"
    

    额外的tasks.yml

    ---
    
    - include_tasks: 1.yml
      when: 
        - x is defined
        - x = "aaa"
    - include_tasks: 2.yml
      when:
        - x is defined 
        - x = "bbb"
    - include_tasks: 3.yml
      when:
        - x is defined
        - x = "ccc"
    - include_tasks: 4.yml
      when:
        - x is defined
        - x = "ddd"
    
    

    tasks.yml 中,我添加了条件来检查是否定义了 x。

    【讨论】:

    • 谢谢你。我使用的环境是 Ansible Tower,并且有用户调查,我从中获得了所需的周期数。但是我需要在每个循环中增加一些其他变量,所以它在第一个循环中从 1 开始,然后增加 1...对此有什么想法吗?
    • @LJS 检查我更新的代码。我在 with_sequence 中添加了额外的选项 'stride' 来控制增量值。但是,到目前为止,它是一个静态值。关于动态增加步幅,我不知道怎么做。幸运的是,社区中有更多的专家。肯定有人会帮助你的!
    • 我已经使它与其他提议的解决方案一起使用,但这也可以完成工作。我会记下它以备将来使用。谢谢。
    猜你喜欢
    • 2016-01-07
    • 2014-02-26
    • 2016-04-19
    • 2013-12-27
    • 1970-01-01
    • 2020-01-26
    • 2020-05-02
    • 2019-07-22
    • 2014-08-05
    相关资源
    最近更新 更多