The long format listing provided by the ansible.builtin.ls
module offers a wealth of information about the files and directories in a directory. Let's dive deeper into understanding the different components of the long format listing.
When you use the long: yes
option in the ansible.builtin.ls
module, the output will include the following information for each file or directory:
Field |
Description |
mode |
The file permissions, represented as a 10-character string (e.g., -rw-r--r-- ). |
owner |
The owner of the file or directory. |
group |
The group that the file or directory belongs to. |
size |
The size of the file in bytes. |
modified |
The last modification time of the file or directory. |
name |
The name of the file or directory. |
Here's an example of the long format listing for the /etc
directory:
- hosts: all
tasks:
- name: List contents of /etc in long format
ansible.builtin.ls:
path: /etc
long: yes
register: etc_contents
- name: Print the contents of /etc in long format
debug:
var: etc_contents.files
The output of this playbook will look similar to the following:
"files": [
{
"mode": "-rw-r--r--",
"owner": "root",
"group": "root",
"size": 1024,
"modified": "2023-04-01 12:34:56",
"name": "file1.txt"
},
{
"mode": "drwxr-xr-x",
"owner": "root",
"group": "root",
"size": 4096,
"modified": "2023-04-02 09:15:22",
"name": "directory1"
}
]
This output provides a detailed view of the files and directories in the /etc
directory, including their permissions, ownership, size, and modification time.
You can also use the ansible.builtin.ls
module to filter the long format listing based on specific criteria, such as file type or size. For example, to list only the directories in the /etc
directory, you can use the following playbook:
- hosts: all
tasks:
- name: List directories in /etc in long format
ansible.builtin.ls:
path: /etc
long: yes
file_type: directory
register: etc_dirs
- name: Print the directories in /etc in long format
debug:
var: etc_dirs.files
This playbook will output only the directories in the /etc
directory, with their detailed information in the long format.