access particular values gathered with stat

161 views
Skip to first unread message

dulh...@mailbox.org

unread,
Sep 6, 2023, 8:34:43 AM9/6/23
to ansible...@googlegroups.com
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?

Rowe, Walter P. (Fed)

unread,
Sep 6, 2023, 8:52:06 AM9/6/23
to ansible...@googlegroups.com
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

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1292803683.374335.1694003663225%40office.mailbox.org.

Rowe, Walter P. (Fed)

unread,
Sep 6, 2023, 8:53:05 AM9/6/23
to ansible...@googlegroups.com

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

Dick Visser

unread,
Sep 6, 2023, 12:22:12 PM9/6/23
to ansible...@googlegroups.com
On Wed, 6 Sept 2023 at 14:34, dulhaver via Ansible Project <ansible...@googlegroups.com> 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.

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?



dulh...@mailbox.org

unread,
Sep 7, 2023, 4:31:27 AM9/7/23
to ansible...@googlegroups.com
 
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
Reply all
Reply to author
Forward
0 new messages