Creating a Reusable Role for File Listing
In this step, we'll learn how to create an Ansible role for file listing. Roles are a way to organize playbooks and make them more reusable. This is especially useful when you need to perform the same task across multiple playbooks or projects.
Understanding Ansible Roles
An Ansible role is a set of tasks, variables, files, templates, and other resources that are grouped together in a standard directory structure. Roles make it easier to reuse code and share it with others.
The standard directory structure for a role looks like this:
roles/
rolename/
tasks/ ## Main tasks for the role
handlers/ ## Handlers triggered by tasks
defaults/ ## Default variables
vars/ ## Role variables
files/ ## Static files
templates/ ## Templates
meta/ ## Metadata for the role
Creating a File Listing Role
Let's create a role for listing files in long format:
- First, create the directory structure for our role:
cd ~/project/ansible-lab
mkdir -p roles/file_lister/tasks
- Create the main task file for our role:
cd ~/project/ansible-lab
- In the Explorer panel, navigate to
~/project/ansible-lab/roles/file_lister/tasks
- Right-click and select "New File"
- Name the file
main.yml
- Add the following content:
---
## Tasks for file_lister role
- name: Find files in the specified directory
ansible.builtin.find:
paths: "{{ path | default('/etc') }}"
patterns: "{{ patterns | default('*') }}"
file_type: "{{ file_type | default('any') }}"
recurse: "{{ recurse | default(false) }}"
register: found_files
- name: Display files in long format
debug:
msg: "{{ item.mode }} {{ item.uid }} {{ item.gid }} {{ item.size }} {{ item.mtime }} {{ item.path }}"
loop: "{{ found_files.files | sort(attribute='path') }}"
loop_control:
label: "{{ item.path }}"
when: show_details | default(true)
- name: Display only file paths
debug:
msg: "{{ item.path }}"
loop: "{{ found_files.files | sort(attribute='path') }}"
loop_control:
label: "{{ item.path }}"
when: not (show_details | default(true))
This role:
- Finds files in a specified directory based on parameters
- Displays the files in either long format or just the paths
- Uses default values for parameters if they're not specified
Using Our Role in a Playbook
Now, let's create a playbook that uses our new role:
- In the Explorer panel, navigate to the
~/project/ansible-lab
directory
- Right-click and select "New File"
- Name the file
use_role.yml
- Add the following content:
---
- name: Use file listing role
hosts: local
roles:
- role: file_lister
vars:
path: "/etc"
patterns: "*.conf"
file_type: "file"
show_details: true
This playbook:
- Runs on the local host
- Uses our
file_lister
role
- Sets variables for the role to customize its behavior
Let's run the playbook:
cd ~/project/ansible-lab
ansible-playbook use_role.yml
You should see output showing the details of all .conf
files in the /etc
directory.
Customizing Role Variables
One of the advantages of roles is that we can easily customize their behavior by changing the variables. Let's create another playbook that uses our role with different parameters:
- In the Explorer panel, right-click and select "New File"
- Name the file
custom_listing.yml
- Add the following content:
---
- name: Custom file listing
hosts: local
roles:
- role: file_lister
vars:
path: "/home/labex"
patterns: "*.yml"
file_type: "file"
show_details: false
This playbook:
- Uses our
file_lister
role
- Sets it to look in the
/home/labex
directory
- Filters for YAML files (*.yml)
- Shows only file paths (not full details)
Let's run the playbook:
ansible-playbook custom_listing.yml
You should see a list of all YAML files in the /home/labex
directory, without the detailed information.
Advanced Usage: Creating a Report
Finally, let's create a more advanced playbook that uses our role to generate a report of files:
- In the Explorer panel, right-click and select "New File"
- Name the file
generate_report.yml
- Add the following content:
---
- name: Generate file listing report
hosts: local
vars:
report_path: "~/project/ansible-lab/file_report.txt"
tasks:
- name: Find files in the specified directory
ansible.builtin.find:
paths: "/etc"
patterns: "*.conf"
file_type: "file"
register: found_files
- name: Create report header
copy:
dest: "{{ report_path }}"
content: |
File Listing Report
Generated on: {{ ansible_date_time.date }} at {{ ansible_date_time.time }}
-----------------------------------------------------------
Mode Owner Group Size Modified Path
-----------------------------------------------------------
force: yes
- name: Append file information to report
lineinfile:
path: "{{ report_path }}"
line: "{{ item.mode }} {{ item.uid }} {{ item.gid }} {{ item.size | string | ljust(10) }} {{ item.mtime }} {{ item.path }}"
loop: "{{ found_files.files | sort(attribute='path') }}"
loop_control:
label: "{{ item.path }}"
- name: Display report location
debug:
msg: "Report generated at {{ report_path }}"
This playbook:
- Finds
.conf
files in the /etc
directory
- Creates a text file with a header
- Appends information about each file to the report
- Displays the location of the report
Let's run the playbook:
ansible-playbook generate_report.yml
After running the playbook, you can view the report:
cat ~/project/ansible-lab/file_report.txt
You should see a formatted report listing all the .conf
files in the /etc
directory.