用字符串替换 Ansible 列表的空索引

Replace Empty Indexes of Ansible List with String

我的 ansible 剧本中有一个名为 'inf_list_tmp' 的列表,它看起来像:

{
"changed": false,
"ansible_facts": {
    "inf_list_tmp": [
        [],
        [],
        [
            "[dataLoaderJobTaskExecutor-7] - 02/23/2021 07:23:30 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables"
        ],
        [],
        [
            "[dataLoaderJobTaskExecutor-5] - 02/23/2021 07:23:33 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables"
        ],
        [],
        [
            "[dataLoaderJobTaskExecutor-8] - 02/23/2021 07:40:49 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables"
        ],
        [
            "[dataLoaderJobTaskExecutor-1] - 02/23/2021 07:41:21 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables "
        ]
    ]
},
"_ansible_no_log": false

}

现在我想用字符串替换上面列表中的空索引 'STARTED JOB'

预期输出:

"inf_list_tmp": [
        [STARTED JOB],
        [STARTED JOB],
        [
            "[dataLoaderJobTaskExecutor-7] - 02/23/2021 07:23:30 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables"
        ],
        [STARTED JOB],
        [
            "[dataLoaderJobTaskExecutor-5] - 02/23/2021 07:23:33 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables"
        ],
        [STARTED JOB],
        [
            "[dataLoaderJobTaskExecutor-8] - 02/23/2021 07:40:49 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables"
        ],
        [
            "[dataLoaderJobTaskExecutor-1] - 02/23/2021 07:41:21 ES30CLIENT21  [EventHelperServiceImpl] INFO: Sending event notification [STARTED JOB] for tenant [ES30CLIENT21] with variables "
        ]
    ]

下面是我的代码,但它没有给我预期的输出:

- set_fact: my_list="{{ item.replace(item, 'STARTED JOB') if item|length == 0 else item }}"
  with_items: "{{ inf_list_tmp }}"

有没有办法在 Ansible 剧本中实现这一目标?提前致谢!

让我们简化数据,例如

inf_list_tmp: [[], [], [a], [], [b], [], [c], [d]]

问:"用字符串替换上面列表中的空索引'STARTED JOB'"

答:下面的任务

    - set_fact:
        inf_list: "{{ inf_list|default([]) + [_item] }}"
      loop: "{{ inf_list_tmp }}"
      vars:
        _item: "{{ (item|length > 0)|ternary(item,  'STARTED JOB') }}"

给予

  inf_list:
  - STARTED JOB
  - STARTED JOB
  - - a
  - STARTED JOB
  - - b
  - STARTED JOB
  - - c
  - - d