# access particular values gathered with stat

**URL:** <https://forum.ansible.com/t/access-particular-values-gathered-with-stat/38409>\
**Category:** Ansible Project\
**Created:** [September 6, 2023, 12:34pm UTC](https://forum.ansible.com/t/access-particular-values-gathered-with-stat/38409 "2023-09-06T12:34:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 6, 2023, 12:34pm UTC](https://forum.ansible.com/t/access-particular-values-gathered-with-stat/38409/1 "2023-09-06T12:34:23Z")

</div>

I want to check for existence of a folder and also the right owner, group and mode.  
When any of the conditions (exists, owner=postgres, group=postgres, mode=0755) does not meet requirements I want to stop and be notified of the reason.

I think I need stat for that (exists, isdir, gr\_name, pw\_name, mode)

As a first step I try to get the value of pg\_name with debug, but can not figure out how to adress that value

- name: Get stats of a file  
ansible.builtin.stat:  
path: “{{ item }}”  
register: postgres  
loop:

- /opt/db/data

- name: return ownership is right  
ansible.builtin.debug:  
msg: “owned by postgres”  
when: postgres.stat.pw\_name == ‘postgres’

output

TASK [show content of ‘postgres’] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [[dvzsn-rd5400.ref.eakte.rz-dvz.cn-mv.de](http://dvzsn-rd5400.ref.eakte.rz-dvz.cn-mv.de)]: FAILED! =\> {“msg”: “The conditional check ‘postgres.stat.pw\_name == ‘postgres’’ failed. The error was: error while evaluating conditional (postgres.stat.pw\_name == ‘postgres’): ‘dict object’ has no attribute ‘stat’. ‘dict object’ has no attribute ‘stat’\n\nThe error appears to be in ‘/home/gwagner/repos/automation\_postgres/playbooks/check\_postgres\_dir.yml’: line 16, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: show content of ‘postgres’\n ^ here\n”}

what I am doing wrong?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 6, 2023, 12:51pm UTC](https://forum.ansible.com/t/access-particular-values-gathered-with-stat/38409/2 "2023-09-06T12:51:40Z")

</div>

You are using “loop”. This places the results in a list called “results” (ie postgres.results). Use the literal path in stat and you will get the reference you seek in the debug task.

> - name: Get stats of a file  
> ansible.builtin.stat:  
> path: /opt/db/data

> register: postgres
> 
> - name: return ownership is right  
> ansible.builtin.debug:  
> msg: “owned by postgres”  
> when: postgres.stat.pw\_name == ‘postgres’

OR

- name: Get stats of a file  
ansible.builtin.stat:  
path: “{{ item }}”  
register: postgres  
loop:

- /opt/db/data

- name: return ownership is right  
ansible.builtin.debug:  
msg: “{{ item.item }} owned by postgres”  
loop: “{{ postgres.results }}”  
when:

- item.stat.exists == true

- item.stat.pw\_name is defined

- item.stat.pw\_name == ‘postgres’

- name: return ownership is not right  
ansible.builtin.debug:  
msg: “{{ item.item }} owned not by postgres”  
loop: “{{ postgres.results }}”  
when:

- item.stat.exists == true

- item.stat.pw\_name is defined

- item.stat.pw\_name != ‘postgres’

- name: return does not exist  
ansible.builtin.debug:  
msg: “{{ item.item }} does not exist”  
loop: “{{ postgres.results }}”  
when:

- item.stat.exists == false

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 6, 2023, 12:52pm UTC](https://forum.ansible.com/t/access-particular-values-gathered-with-stat/38409/3 "2023-09-06T12:52:46Z")

</div>

TASK [what is postgres] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [localhost] =\> {

“postgres”: {

“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”: “/opt/db/data”

}

},

“item”: “/opt/db/data”,

“stat”: {

“exists”: false

}

}

],

“skipped”: false

}

}

Walter

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 6, 2023, 4:21pm UTC](https://forum.ansible.com/t/access-particular-values-gathered-with-stat/38409/4 "2023-09-06T16:21:36Z")

</div>

What are you trying to do at a high level?  
While you can use ansible as a glorified shell wrapper or reporting tool, but its intended purpose is configuration management.  
So in this case, what will you do if the condition DO meet the requirements?

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [September 7, 2023, 8:31am UTC](https://forum.ansible.com/t/access-particular-values-gathered-with-stat/38409/5 "2023-09-07T08:31:06Z")

</div>

once the conditions are met I want to proceed importing/including a role that installs postgres under that specified location.  
This role expects the conditions to be met in order to run successfully
