Commonly Used Modules in Ansible

Commonly Used Modules in Ansible

Introduction

Ansible provides a vast array of modules that cover a wide range of system administration tasks. These modules enable users to automate various aspects of infrastructure management, including package management, file manipulation, service management, and more. In this article, we'll explore some commonly used modules in Ansible and their functionality.

1. apt Module:

  • Used for package management on Debian-based systems.

  • Allows installation, removal, and upgrade of packages.

  • Example: ansible all -m apt -a "name=nginx state=installed"

2. yum Module:

  • Similar to the apt module but used for package management on Red Hat-based systems.

  • Allows installation, removal, and upgrade of packages.

  • Example: ansible all -m yum -a "name=httpd state=present"

3. copy Module:

  • Used to copy files or directories to managed nodes.

  • Supports file permissions, ownership, and symbolic links.

  • Example: ansible all -m copy -a "src=file.txt dest=/tmp/file.txt"

4. template Module:

  • Used to deploy Jinja2 templates to managed nodes.

  • Allows for dynamic generation of configuration files based on variables.

  • Example: ansible all -m template -a "src=template.j2 dest=/etc/config.conf"

5. service Module:

  • Used to manage system services on managed nodes.

  • Allows starting, stopping, restarting, enabling, and disabling services.

  • Example: ansible all -m service -a "name=nginx state=started"

6. command Module:

  • Executes arbitrary commands on managed nodes.

  • Useful for one-off tasks or tasks not covered by specialized modules.

  • Example: ansible all -m command -a "echo 'Hello, World!' > /tmp/hello.txt"

Conclusion

These are just a few examples of commonly used modules in Ansible. With a rich ecosystem of modules available, Ansible users have the flexibility to automate a wide range of tasks, making infrastructure management more efficient and reliable.