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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Sep 6, 2023, at 8:34 AM, dulhaver via Ansible Project 
<[email protected]> wrote:

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]: 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?

--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1292803683.374335.1694003663225%40office.mailbox.org<https://groups.google.com/d/msgid/ansible-project/1292803683.374335.1694003663225%40office.mailbox.org?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/69BC3525-B55F-4FE1-9FBF-7D48166BFC0A%40nist.gov.

Reply via email to