# Problem with stat module and multiple files in the register

**URL:** <https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862>\
**Category:** Get Help\
**Created:** [July 13, 2025, 6:15pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862 "2025-07-13T18:15:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RRohi](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@RRohi](https://forum.ansible.com/u/RRohi)\
**Post date:** [July 13, 2025, 6:15pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862/1 "2025-07-13T18:15:27Z")

</div>

Hello

I’m struggling with file check when there’s more than one file in the `stat` module.

For example, this works:

```yml
- name: Check if conf file exist.
  ansible.builtin.stat:
    path: /etc/file.conf
  register: confcheck

- name: Create the file.conf file.
  ansible.builtin.lineinfile:
    create: true
    group: root
    line: root
    mode: '0600'
    owner: root
    path: /etc/file.conf
    state: present
  when: not confcheck.stat.exists

```

When I would add a list of files, I’m no longer able to use `when:` because I don’t understand how it works.  
Using `debug` to display the register result, I see the structure, but whatever I put in the `when:` block, I get `this item is not in the list` error every time. Ansible docs didn’t help.

What I’m trying to do here is to copy the file to `/etc/app.d/` folder if it exists in `/etc`, if it doesn’t exist in `/etc/` and `/etc/app.d/`, create the file in `/etc/app.d/` and I should be able to determine the `exists` value for each file from ther registered variable.

```yml
- name: Check if conf files exist.
  ansible.builtin.stat:
    path: "{{ item }}"
  loop:
    - /etc/app1.conf
    - /etc/app.d/app1.conf
    - /etc/app2.conf
    - /etc/app.d/app2.conf
  register: confcheck

- name: Copy app1.conf to app.d directory.
  ansible.builtin.copy:
    dest: /etc/app.d/app1.conf
    group: root
    mode: '0600'
    owner: root
    remote_src: true
    src: "/etc/app1.conf"
  loop:
    - "{{ confcheck }}"
  when: item.stat.exists # This should be: exists /etc/app1.conf and not exists /etc/app.d/app1.conf. This would fail as there is no stat item, although there is when I check with the debug module.

- name: Create the file.conf file.
  ansible.builtin.lineinfile:
    create: true
    group: root
    line: root
    mode: '0600'
    owner: root
    path: /etc/file.conf
    state: present
  loop:
    - "{{ confcheck }}"
  when: not items.stat.exists # This should be: not exists /etc/app1.conf and not exists /etc/app.d/app1.conf.

```

I need someone to tell me how to iterate registered list vars.  
Right now, I have separate registers per file, and I don’t like it.

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [July 13, 2025, 10:11pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862/2 "2025-07-13T22:11:49Z")

</div>

This is nicely explained in the documentation:

> **[Loops — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#registering-variables-with-a-loop)**

Update: sorry I didn’t read you question carefully. You already mentioned docs were of no help.

I think you are missing only one thing. You should loop over `confcheck['results']` like this:

```auto
- name: Copy app1.conf to app.d directory.
  ansible.builtin.copy:
    dest: /etc/app.d/app1.conf
    group: root
    mode: '0600'
    owner: root
    remote_src: true
    src: "/etc/app1.conf"
  loop:
    - "{{ confcheck['results'] }}"
  when: item.stat.exists

```

It’s a little bit hard to understand what are you trying to accomplish, but in the previous code either `dest` or `src` or both should also be variables dependent on `{{ item['item'] }}`. Possibly something like this:

```auto
- name: Copy app1.conf to app.d directory.
  ansible.builtin.copy:
    dest: "/etc/app.d/{{ item['item'] }}"
    group: root
    mode: '0600'
    owner: root
    remote_src: true
    src: "/etc/{{ item['item'] }}"
  loop:
    - "{{ confcheck['results'] }}"
  when: item.stat.exists

```

And another note, `{{ item['item'] }}` is the notation I preferably used but `{{ item.item }}` can also be used and is the equivalent.

---

<div class="post-metadata">

**Author:** ![RRohi](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@RRohi](https://forum.ansible.com/u/RRohi)\
**Post date:** [July 16, 2025, 7:04pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862/3 "2025-07-16T19:04:17Z")

</div>

Doesn’t work.  
Complains about the list object not having an attribute ‘stat’ in when.  
Tried with a single condition, but the error is the same.

```plaintext
The error was: error while evaluating conditional (item.stat.exists and item.item is not match(\"app.d\")): 'list object' has no attribute 'stat'.

```

```yml
- name: Check for conffiles
  ansible.builtin.stat:
    path: "{{ item }}"
  loop:
    - /etc/app1.conf
    - /etc/app.d/app1.conf
    - /etc/app2.conf
    - /etc/app.d/app1.conf
  register: confcheck
  tags: filecheck

- name: Copy app.conf files to app.d directory.
  ansible.builtin.copy:
    dest: "/etc/app.d/"
    group: root
    mode: '0600'
    owner: root
    remote_src: true
    src: "{{ item['item'] }}"
  loop:
    - "{{ confcheck['results'] }}"
  tags: filecheck
  when: item.stat.exists and item.item is not match("app.d")

```

Here’s the JSON of the stat output from the debug module:

```json
{
    "confcheck": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": false,
                        "get_mime": true,
                        "path": "/etc/app1.conf"
                    }
                },
                "item": "/etc/app1.conf",
                "stat": {
                    "exists": false
                }
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": false,
                        "get_mime": true,
                        "path": "/etc/app.d/app1.conf"
                    }
                },
                "item": "/etc/app.d/app1.conf",
                "stat": {
                    "exists": false
                }
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": false,
                        "get_mime": true,
                        "path": "/etc/app2.conf"
                    }
                },
                "item": "/etc/app2.conf",
                "stat": {
                    "exists": false
                }
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": false,
                        "get_mime": true,
                        "path": "/etc/app.d/app2.conf"
                    }
                },
                "item": "/etc/app.d/app2.conf",
                "stat": {
                    "exists": false
                }
            }
        ],
        "skipped": false
    }
}

```

There are clearly `stat` nodes in the result array/list items.

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [July 16, 2025, 9:58pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862/4 "2025-07-16T21:58:08Z")

</div>

Aaaah, sorry. I did no spot this initially.

The loops are wrong:

```auto
loop:
    - "{{ confcheck['results'] }}"

```

this should be:

```auto
loop: "{{ confcheck['results'] }}"

```

The first (wrong) case makes a nested list with a single element. The second one makes the proper list with one element per file.

---

<div class="post-metadata">

**Author:** ![RRohi](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@RRohi](https://forum.ansible.com/u/RRohi)\
**Post date:** [July 17, 2025, 4:57pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862/5 "2025-07-17T16:57:22Z")

</div>

Thanks! Finally.

All these hours wasted because of a dash 😔.

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [July 17, 2025, 4:58pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862/6 "2025-07-17T16:58:27Z")

</div>

Welcome to the club 😄

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 16, 2025, 4:59pm UTC](https://forum.ansible.com/t/problem-with-stat-module-and-multiple-files-in-the-register/43862/7 "2025-08-16T16:59:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
