Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Commonly Used Ansible Modules

Introduction

Ansible is an open-source automation tool that simplifies IT tasks such as configuration management, application deployment, and task automation. Modules are the units of work in Ansible, which can perform specific tasks in playbooks. This tutorial covers some of the most commonly used Ansible modules with examples and detailed explanations.

1. The ping Module

The ping module is used to check the availability of your hosts. It sends a ping to each host and returns the result.

- name: Ping all hosts
  hosts: all
  tasks:
    - name: Ping the hosts
      ping:

To run this playbook, use:

ansible-playbook ping_hosts.yml
PLAY [Ping all hosts] ***************************************************

TASK [Gathering Facts] *************************************************
ok: [host1]
ok: [host2]

TASK [Ping the hosts] **************************************************
ok: [host1]
ok: [host2]

PLAY RECAP *************************************************************
host1 : ok=2    changed=0    unreachable=0    failed=0   
host2 : ok=2    changed=0    unreachable=0    failed=0

2. The file Module

The file module is used to manage files and directories on remote hosts. You can create, delete, and modify files and directories using this module.

- name: Ensure a directory exists
  hosts: all
  tasks:
    - name: Create a directory
      file:
        path: /path/to/directory
        state: directory
        mode: '0755'

To run this playbook, use:

ansible-playbook create_directory.yml

3. The copy Module

The copy module is used to copy files from the control machine to remote hosts. It ensures that the file on the remote host matches the source file on the control machine.

- name: Copy a file to the remote host
  hosts: all
  tasks:
    - name: Copy the file
      copy:
        src: /path/to/source/file
        dest: /path/to/destination/file

To run this playbook, use:

ansible-playbook copy_file.yml

4. The yum Module

The yum module is used for managing packages on Red Hat-based systems such as CentOS and Fedora. You can install, update, and remove packages using this module.

- name: Install a package using yum
  hosts: all
  tasks:
    - name: Install httpd
      yum:
        name: httpd
        state: present

To run this playbook, use:

ansible-playbook install_package.yml

5. The service Module

The service module is used to manage services on remote hosts. You can start, stop, restart, and reload services using this module.

- name: Manage services
  hosts: all
  tasks:
    - name: Start the httpd service
      service:
        name: httpd
        state: started

To run this playbook, use:

ansible-playbook manage_services.yml

6. The user Module

The user module is used to manage user accounts on remote hosts. You can create, modify, and delete user accounts using this module.

- name: Manage user accounts
  hosts: all
  tasks:
    - name: Create a new user
      user:
        name: newuser
        state: present
        groups: "wheel"

To run this playbook, use:

ansible-playbook manage_users.yml

Conclusion

This tutorial has covered some of the most commonly used Ansible modules, providing examples and explanations for each. These modules are essential for automating tasks and managing configurations efficiently. By understanding and using these modules, you can streamline your IT operations and improve productivity.