Search code examples
linuxshellunixansible

Retrieving user and group name of a file and storing it in different variables in Linux using Ansible


How to retrieve the user and group name associated with a file in Linux using Ansible, i repeat not id's but names. Or if there is any shell command that i can use in Ansible. I checked even in the stat module documentation but couldn't find anything related to the names, only numeric id retrieval part was present.I need the string name of user and group to use it afterwards.


Solution

  • I checked even in the stat module documentation but couldn't find anything related to the names

    From the stat module documentation:

    gr_name - Group name of owner

    pw_name - User name of owner

    - name: Get stats of a file
      ansible.builtin.stat:
        path: /etc/foo.conf
      register: st
    
    - name: Show the username
      ansible.builtin.debug:
        var: st.stat.pw_name
    
    - name: Show the group name
      ansible.builtin.debug:
        var: st.stat.gr_name