Replace Empty Indexes of Ansible List with String

I have a list called 'inf_list_tmp' in my ansible playbook and it looks like:

{
"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

}

Now I want to replace empty indexes in above list with a string 'STARTED JOB'

Expected Output:

"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 "
        ]
    ]

Below is my code, but it's not giving me expected output:

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

Is there a way to achieve this within the Ansible playbook? Thanks in advance!



Read more here: https://stackoverflow.com/questions/66335569/replace-empty-indexes-of-ansible-list-with-string

Content Attribution

This content was originally published by Anushah Khawaja at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: