The idea is to add an item into the array if the item does not exist. The select attribute function returns an empty list even though there are items. Therefore the length of an empty is always 0, which in turn will create a new list every time in my case. So the union function in add a new comment section is returning a list with the only new comment erasing all the old ones.

I understand that the issue is with `t_array_exists["ansible_facts"]|selectattr("common_motd_qsc_comments_array", "defined")|list|length == 0` conditional statement but I am not sure what I am doing wrong. I have tried many variations of this command. Few of them are commented. Any advice/suggestion is appreciated :)

main.yml:

```yaml
- name: Get comment array from facts
  set_fact:
    common_motd_qsc_comments_array: "{{ ansible_local['snps']['motd']['comment_array'] }}"
  register: t_array_exists
  when:
    - ansible_local['snps']['motd'] is defined
    - ansible_local['snps']['motd']['comment_array'] is defined

- debug:
    var: t_array_exists

- debug:
    var: t_array_exists["ansible_facts"]|selectattr("common_motd_qsc_comments_array", "defined")|list
    # var: ansible_facts|selectattr("common_motd_qsc_comments_array", "defined")|list
    # var: t_array_exists|selectattr("common_motd_qsc_comments_array", "defined")|list

- name: Create an empty array if there is no array
  set_fact:
    common_motd_qsc_comments_array: []
  when:
    - t_array_exists["ansible_facts"]|selectattr("common_motd_qsc_comments_array", "defined")|list|length == 0

- debug:
    var: t_array_exists["ansible_facts"]|selectattr("common_motd_qsc_comments_array", "defined")|list|length

- name: Deleting a comment if it exists
  set_fact:
    common_motd_qsc_comments_array: "{{ common_motd_qsc_comments_array | difference([t_new_entry]) }}"
  loop: "{{ common_motd_qsc_delete_comment }}"
  when: t_new_entry in common_motd_qsc_comments_array
  vars:
    t_new_entry: "{{ item | trim }}"

- name: Add a new comment if it doesn't exist
  set_fact:
    common_motd_qsc_comments_array: "{{ common_motd_qsc_comments_array | union([t_new_entry]) }}"
  loop: "{{ common_motd_qsc_add_comment }}"
  when: t_new_entry not in common_motd_qsc_comments_array
  vars:
    t_new_entry: "{{ item | trim }}"

- name: Saving comments to snps.fact file
  ini_file:
    dest: "/etc/ansible/facts.d/snps.fact"
    section: 'motd'  # header
    option: 'comment_array'  # key
    value: "{{ common_motd_qsc_comments_array }}"  # value

```

Ansible output:

```
TASK [common/motd_scratch/v1 : Get comment array from facts] ***************************************************************************************
ok: [ansible-poc-cos6]

TASK [common/motd_scratch/v1 : debug] **************************************************************************************************************
ok: [ansible-poc-cos6] => {
    "t_array_exists": {
        "ansible_facts": {
            "common_motd_qsc_comments_array": [
                "new comment 1"
            ]
        },
        "changed": false,
        "failed": false
    }
}

TASK [common/motd_scratch/v1 : debug] **************************************************************************************************************
ok: [ansible-poc-cos6] => {
    "ansible_facts|selectattr(\"common_motd_qsc_comments_array\", \"defined\")|list": []
}

TASK [common/motd_scratch/v1 : Create an empty array if there is no array] *************************************************************************
ok: [ansible-poc-cos6]

TASK [common/motd_scratch/v1 : debug] **************************************************************************************************************
ok: [ansible-poc-cos6] => {
    "t_array_exists[\"ansible_facts\"]|selectattr(\"common_motd_qsc_comments_array\", \"defined\")|list|length": "0"
}

TASK [common/motd_scratch/v1 : Add a new comment if it doesn't exist] ******************************************************************************
ok: [ansible-poc-cos6] => (item=new comment 22)

TASK [common/motd_scratch/v1 : Saving comments to snps.fact file] **********************************************************************************
changed: [ansible-poc-cos6]

TASK [common/motd_scratch/v1 : debug] **************************************************************************************************************
ok: [ansible-poc-cos6] => {
    "common_motd_qsc_add_comment": [
        "new comment 22"
    ]
}

```

snps.fact output:

Before:

```
[motd]
comment_array = ['new comment 1']

```

After passing new\_comment as "new comment 2":

```
[motd]
comment_array = ['new comment 2']

```

Expected:

```
[motd]
comment_array = ['new comment 1', 'new comment 2']

```