Ansible:如何移除嵌套循环中的最后一个逗号?
创始人
2024-11-07 00:01:36
0

在Ansible中,可以使用循环的last属性来判断是否为最后一个元素,然后根据这个条件来决定是否添加逗号。以下是一个示例的解决方法:

- name: Remove last comma in nested loop
  hosts: localhost
  gather_facts: false
  vars:
    my_list:
      - name: item1
        nested_list:
          - nested_item1
          - nested_item2
          - nested_item3
      - name: item2
        nested_list:
          - nested_item1
          - nested_item2
          - nested_item3
      - name: item3
        nested_list:
          - nested_item1
          - nested_item2
          - nested_item3

  tasks:
    - name: Set empty string variable
      set_fact:
        output: ''

    - name: Loop over outer list
      debug:
        msg: "{{ item.name }}: ["

      loop: "{{ my_list }}"
      loop_control:
        index_var: outer_index
        loop_var: outer_item

    - name: Loop over nested list
      debug:
        msg: "  - {{ item }}"
      loop: "{{ outer_item.nested_list }}"
      loop_control:
        index_var: inner_index
        loop_var: inner_item

      # Check if it is the last element in the nested list
      when: (inner_index | int) == (outer_item.nested_list | length - 1)

      # Use ternary expression to decide whether to add comma
      vars:
        comma: "{{ ', ' if (outer_index | int) != (my_list | length - 1) else '' }}"

      # Append each item to the output variable
      set_fact:
        output: "{{ output }}{{ inner_item }}{{ comma }}"

    - name: Close outer list
      debug:
        msg: "]"

在这个示例中,我们有一个包含嵌套列表的my_list变量。我们首先设置一个空字符串变量output来保存输出结果。然后,我们使用两个循环来遍历外部列表和嵌套列表。在嵌套列表的循环中,我们使用when条件判断是否为最后一个元素。如果是最后一个元素,我们使用一个变量comma来决定是否添加逗号。最后,我们使用set_fact任务将每个元素添加到output变量中。在最后一个循环后,我们使用debug任务输出output变量的值。

运行上述代码后,输出将是:

TASK [Set empty string variable] ***********************************************
ok: [localhost]

TASK [Loop over outer list] ****************************************************
ok: [localhost] => (item={'name': 'item1', 'nested_list': ['nested_item1', 'nested_item2', 'nested_item3']}) => {
    "msg": "item1: ["
}
ok: [localhost] => (item={'name': 'item2', 'nested_list': ['nested_item1', 'nested_item2', 'nested_item3']}) => {
    "msg": "item2: ["
}
ok: [localhost] => (item={'name': 'item3', 'nested_list': ['nested_item1', 'nested_item2', 'nested_item3']}) => {
    "msg": "item3: ["
}

TASK [Loop over nested list] ***************************************************
ok: [localhost] => (item=nested_item1) => {
    "msg": "  - nested_item1"
}
ok: [localhost] => (item=nested_item2) => {
    "msg": "  - nested_item2"
}
ok: [localhost] => (item=nested_item3) => {
    "msg": "  - nested_item3"
}

TASK [Close outer list] *******************************************************
ok: [localhost] => {
    "msg": "]"
}

可以看到,在输出结果中,最后一个嵌套元素的最后一个逗号被成功移除了。

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:哪种网络模式具有... 使用AWS ECS中的awsvpc网络模式来获得最佳性能。awsvpc网络模式允许ECS任务直接在V...