Ansible:如何移除嵌套循环中的最后一个逗号?
创始人
2024-11-07 00:00:35
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": "]"
}

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

相关内容

热门资讯

七分钟辅助!丽水茶苑苹果手机辅... 七分钟辅助!丽水茶苑苹果手机辅助,本来是真的有辅助教程(有挂方式)1、实时丽水茶苑苹果手机辅助透视辅...
第一分钟辅助!闲来辅助神器下载... 第一分钟辅助!闲来辅助神器下载2022,好像真的有辅助方法(有挂教程)1、不需要AI权限,帮助你快速...
九分钟辅助!丽水都莱辅助工具试... 九分钟辅助!丽水都莱辅助工具试用,确实存在有辅助神器(有挂方法)九分钟辅助!丽水都莱辅助工具试用,确...
第一分钟辅助!蛮王辅助器,好像... 第一分钟辅助!蛮王辅助器,好像是有辅助方法(有挂教学)1、首先打开蛮王辅助器辅助器下载最新版本,在蛮...
第六分钟辅助!潮汕汇挂,一贯真... 第六分钟辅助!潮汕汇挂,一贯真的是有辅助插件(有挂辅助)1、这是跨平台的潮汕汇挂轻量版有透视,在线的...
六分钟辅助!微信开心泉州辅助器... 六分钟辅助!微信开心泉州辅助器,一直有辅助器(有挂教学)1、下载好微信开心泉州辅助器透视辅助下载之后...
第3分钟辅助!佛手十三道破解版... 第3分钟辅助!佛手十三道破解版安卓,竟然真的有辅助攻略(有挂存在)1、让任何用户在无需佛手十三道破解...
2分钟辅助!sohoo竞技联盟... 2分钟辅助!sohoo竞技联盟辅助,切实真的有辅助脚本(有挂技术)1.sohoo竞技联盟辅助 选牌创...
第8分钟辅助!心悦手游辅助器,... 第8分钟辅助!心悦手游辅助器,原来真的是有辅助技巧(确实有挂);1、每一步都需要思考,不同水平的挑战...
第十分钟辅助!广东雀神祈福真的... 第十分钟辅助!广东雀神祈福真的有用吗,都是是有辅助技巧(有挂方略)1、下载好广东雀神祈福真的有用吗透...