AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Where Are Ansible Modules Stored? Location & Path Guide

By Luca Berton · Published 2024-01-01 · Category: installation

Find where Ansible modules are stored. Locate built-in modules, collection modules, custom modules, and configure module search paths with examples.

Ansible logs are crucial for debugging, auditing, and monitoring your automation workflows. By default, Ansible does not log to a file unless explicitly configured. This article explains where Ansible logs are stored, how to enable logging, and best practices for managing log files.

Where Are Ansible Logs Stored?

Default Behavior

By default, Ansible logs output to the terminal (stdout) during playbook execution. To capture logs in a file, you must configure the logging settings in the ansible.cfg file.

Enabling Logging in Ansible

To enable logging, follow these steps: Open or create the ansible.cfg file in your project directory or system-wide configuration path (e.g., /etc/ansible/ansible.cfg). Add or modify the following lines under the [defaults] section:

   [defaults]
   log_path = /var/log/ansible/ansible.log
   

This configuration stores logs in /var/log/ansible/ansible.log. Ensure the directory exists and has appropriate permissions:

   sudo mkdir -p /var/log/ansible
   sudo chmod -R 755 /var/log/ansible
   
Verify the configuration by running a playbook:
   ansible-playbook playbook.yml
   

Logs will be written to the specified file.

See also: Project Policy Validation with OPA and ansible-policy

Common Log File Locations

Project-Specific Logs: If you specify a relative path in ansible.cfg, logs will be stored in the project directory:
   ./ansible.log
   
System-Wide Logs: When using a global configuration file, logs are often stored under:
   /var/log/ansible/ansible.log
   
Custom Paths: You can define any valid file path for logs in the log_path setting.

Using Environment Variables for Logging

Set the log file path dynamically using the ANSIBLE_LOG_PATH environment variable:

export ANSIBLE_LOG_PATH=/custom/path/ansible.log

This overrides the log_path setting in the configuration file for the current session.

See also: What Are Ansible Playbooks? Definition, Structure, and Examples (2026 Guide)

Log Levels and Verbosity

To capture detailed information in logs, use the -v (verbose) option during playbook execution: • -v: Basic verbosity. • -vv: More detailed logs. • -vvv: Debug-level logs.

Example:

ansible-playbook playbook.yml -vvv

The increased verbosity helps in diagnosing complex issues.

Managing Log File Size

Large log files can become difficult to manage. Use log rotation to limit file size and maintain historical data: Install and configure logrotate:

   sudo apt install logrotate
   
Add a custom configuration for Ansible logs:
   /var/log/ansible/ansible.log {
       rotate 7
       daily
       compress
       missingok
       notifempty
   }
   

This configuration rotates the log file daily, keeps seven days of logs, and compresses older logs.

See also: Ansible Variables: Complete Guide to vars, facts & Precedence

Best Practices for Ansible Logging

Centralize Logs: Use centralized logging tools like Elasticsearch, Splunk, or Graylog for better visibility. Secure Logs: Restrict access to log files to protect sensitive information. Use Separate Logs for Projects: Store logs in project-specific directories for easier debugging and organization. Enable Debug Logging for Issues: Use higher verbosity levels only when troubleshooting to avoid excessive log size. Rotate Logs Regularly: Prevent disk space issues by enabling log rotation.

Conclusion

Ansible logs are not stored by default but can be enabled and customized through the ansible.cfg file or environment variables. By setting up logging properly and following best practices, you can efficiently manage and analyze logs to optimize your automation workflows.

Learn More About Ansible Logging

Find Module Locations

# Show configured module paths
ansible-config dump | grep DEFAULT_MODULE_PATH

# Find a specific module's file ansible-doc -t module ansible.builtin.copy --json | python3 -c "import sys,json; print(json.load(sys.stdin)['ansible.builtin.copy']['doc']['filename'])"

# Or simply python3 -c "import ansible.modules; print(ansible.modules.__path__)"

Module Storage Locations

| Type | Path | |------|------| | Built-in | /ansible/modules/ | | Collections (user) | ~/.ansible/collections/ansible_collections///plugins/modules/ | | Collections (system) | /usr/share/ansible/collections/ansible_collections/ | | Custom (project) | ./library/ (relative to playbook) | | Custom (global) | Path in ansible.cfg library setting |

Check Built-in Module Location

# Find ansible installation
python3 -c "import ansible; print(ansible.__file__)"
# Example: /usr/lib/python3/dist-packages/ansible/__init__.py

# Built-in modules are at: # /usr/lib/python3/dist-packages/ansible/modules/

Collection Module Locations

# List all collections and their paths
ansible-galaxy collection list

# Find specific collection ansible-galaxy collection list community.general # Path: /home/user/.ansible/collections/ansible_collections/community/general

# Modules inside collection: ls ~/.ansible/collections/ansible_collections/community/general/plugins/modules/

Add Custom Module Path

Project-level (./library/)

my-project/
  library/
    my_custom_module.py    # Auto-discovered
  playbooks/
    site.yml

ansible.cfg

[defaults]
library = ./library:/opt/ansible/modules

Environment variable

export ANSIBLE_LIBRARY=./library:/opt/ansible/modules

List Available Modules

# All modules
ansible-doc -l

# Filter by keyword ansible-doc -l | grep docker

# Module documentation ansible-doc ansible.builtin.copy

# Module parameters (short form) ansible-doc -s ansible.builtin.copy

Built-in vs Collection Modules

| Category | Location | |----------|----------| | ansible.builtin. | Bundled with Ansible core | | community.general. | Installed via ansible-galaxy | | amazon.aws.* | Installed via ansible-galaxy | | Custom | ./library/ or configured path |

FAQ

Can I edit built-in modules?

Technically yes, but it's overwritten on upgrade. Create a custom module in ./library/ instead — it takes precedence over built-in modules with the same name.

How do I check which module version I have?

ansible-doc ansible.builtin.copy | head -5
ansible-galaxy collection list community.general

Where are Windows modules?

In the ansible.windows collection:

ls ~/.ansible/collections/ansible_collections/ansible/windows/plugins/modules/

How do I write a custom module?

Create a Python file in ./library/:

#!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule

def main(): module = AnsibleModule(argument_spec=dict(name=dict(required=True))) module.exit_json(changed=False, msg="Hello " + module.params['name'])

if __name__ == '__main__': main()

Find Module Locations

# Show all module paths
ansible-config dump | grep MODULE

# Show specific module location ansible-doc -t module ansible.builtin.copy --json | python3 -c "import sys,json; print(json.load(sys.stdin)['ansible.builtin.copy']['doc']['filename'])"

# Quick way python3 -c "import ansible; print(ansible.__file__)"

Default Locations

Built-in Modules (ansible.builtin)

# Inside the ansible-core Python package
<python-site-packages>/ansible/modules/

# Common paths: /usr/lib/python3/dist-packages/ansible/modules/ /usr/local/lib/python3.11/dist-packages/ansible/modules/ ~/.local/lib/python3.11/site-packages/ansible/modules/

# In virtual environments: /path/to/venv/lib/python3.11/site-packages/ansible/modules/

Collection Modules

# Default collection path
~/.ansible/collections/ansible_collections/<namespace>/<collection>/plugins/modules/

# Example: ~/.ansible/collections/ansible_collections/community/general/plugins/modules/ ~/.ansible/collections/ansible_collections/amazon/aws/plugins/modules/

System-Wide Collections

/usr/share/ansible/collections/ansible_collections/

Custom Module Paths

Project-Level

project/
├── library/          ← Ansible looks here automatically
│   └── my_module.py
├── ansible.cfg
└── playbook.yml

In ansible.cfg

[defaults]
library = ./library:/opt/ansible/modules

Environment Variable

export ANSIBLE_LIBRARY=/opt/custom-modules:$ANSIBLE_LIBRARY

In Roles

roles/webserver/
└── library/          ← Available when role is used
    └── my_module.py

List All Available Modules

# All modules
ansible-doc -l

# Modules in a collection ansible-doc -l -t module community.general

# Search for modules ansible-doc -l | grep docker

# Module documentation ansible-doc ansible.builtin.copy

Collection Paths

# Show collection search paths
ansible-galaxy collection list

# Default paths (in order): # 1. ./collections/ # 2. ~/.ansible/collections/ # 3. /usr/share/ansible/collections/

# ansible.cfg
[defaults]
collections_path = ./collections:~/.ansible/collections

Module Resolution Order

library/ in playbook directory library/ in role being executed Paths from ANSIBLE_LIBRARY or ansible.cfg library= Collection modules (by FQCN) Built-in ansible.builtin modules

Create a Custom Module

# library/my_module.py
from ansible.module_utils.basic import AnsibleModule

def main(): module = AnsibleModule( argument_spec=dict( name=dict(type='str', required=True), state=dict(type='str', default='present', choices=['present', 'absent']), ) ) name = module.params['name'] module.exit_json(changed=True, msg=f"Processed {name}")

if __name__ == '__main__': main()

# Use it
- my_module:
    name: test
    state: present

FAQ

How do I find which collection provides a module?

ansible-doc <module-name>
# Shows the collection and file path at the top

Can I override a built-in module?

Yes — place a module with the same name in library/. Local modules take precedence over built-in ones.

Modules not found after pip install?

Ensure you're using the same Python/venv. Check: which ansible and ansible --version to verify paths.

Related Articles

Ansible become guideAnsible env var patterns

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home