access particular values gathered with stat

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

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

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?

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