Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Using Modules | Managing Systems with Ansible
Introduction to Ansible

bookUsing Modules

What Are Ansible Modules?

Ansible modules are small, reusable scripts that perform specific tasks on your managed systems. Think of a module as a tool in a toolbox—each one designed for a particular purpose, such as installing software, copying files, or managing users.

Why Modules Matter in Ansible Automation

Modules are the core of Ansible automation. Every time you run an Ansible playbook or use an ad-hoc command, you are telling Ansible which modules to use and what tasks to perform. Modules make your automation powerful and flexible because:

  • Each module is focused on a single, well-defined task;
  • You can combine multiple modules in a playbook to automate complex workflows;
  • Modules are idempotent, meaning they only make changes if something needs to be updated;
  • Ansible provides hundreds of modules out of the box, covering a wide range of system administration tasks.

By understanding and using modules, you unlock the full potential of Ansible to manage your infrastructure efficiently and reliably.

Common Tasks Performed by Modules

  • Manage files: create, delete, copy, or modify files and directories on your systems;
  • Install packages: add or remove software packages using your system's package manager (such as apt for Ubuntu or yum for CentOS);
  • Configure services: start, stop, enable, or restart services like nginx, httpd, or sshd so they run as needed.

When you write a playbook, you specify which module to use and what parameters to give it. Ansible then runs the module on your target machines and reports the result. This approach ensures your systems always end up in the state you describe, no matter what their starting point was.

Example:

  • To make sure a file named hello.txt exists in /tmp, you use the file module with the correct path and state.
  • To install the nginx web server, you use the apt module (on Ubuntu) or yum module (on CentOS) with the package name.
  • To ensure the nginx service is running and enabled at boot, you use the service module with the service name and desired state.

By combining modules in a playbook, you automate complex tasks in a clear, repeatable way.

Examples: Common Ansible Modules

Explore these practical examples to see how to use some of the most common Ansible modules for managing systems.

Using the copy Module

Copy a local file to a target server:

- name: Copy a configuration file to the server
  ansible.builtin.copy:
    src: /home/ansible/config.txt
    dest: /etc/myapp/config.txt

Using the file Module

Set permissions and ownership on a directory:

- name: Ensure log directory exists with correct permissions
  ansible.builtin.file:
    path: /var/log/myapp
    state: directory
    owner: root
    group: root
    mode: '0755'

Using the apt Module

Install a package on a Debian-based system:

- name: Install nginx web server
  ansible.builtin.apt:
    name: nginx
    state: present
    update_cache: yes

Using the service Module

Start and enable a service:

- name: Ensure nginx is running and enabled
  ansible.builtin.service:
    name: nginx
    state: started
    enabled: yes

These examples show how you can automate routine tasks using Ansible modules. Adjust file paths, user names, and package names as needed for your environment.

How to Customize Modules with Parameters

You can customize each module by specifying parameters in your playbook or ad-hoc command. Parameters are provided as key-value pairs under the module name. For example, to create a new user named deploy and add it to the sudo group:

- name: Create a deploy user
  user:
    name: deploy
    state: present
    groups: sudo

To copy a configuration file with specific permissions:

- name: Copy nginx configuration
  copy:
    src: ./nginx.conf
    dest: /etc/nginx/nginx.conf
    owner: root
    group: root
    mode: '0644'

Always check the official Ansible documentation for a full list of parameters available for each module. Customizing modules with the right parameters ensures your automation is precise and effective.

question mark

Which Ansible module should you use to copy a file from your control node to a managed host

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

What are some other commonly used Ansible modules?

Can you explain how idempotency works in Ansible modules?

How do I find the right module for a specific task?

Awesome!

Completion rate improved to 8.33

bookUsing Modules

Свайпніть щоб показати меню

What Are Ansible Modules?

Ansible modules are small, reusable scripts that perform specific tasks on your managed systems. Think of a module as a tool in a toolbox—each one designed for a particular purpose, such as installing software, copying files, or managing users.

Why Modules Matter in Ansible Automation

Modules are the core of Ansible automation. Every time you run an Ansible playbook or use an ad-hoc command, you are telling Ansible which modules to use and what tasks to perform. Modules make your automation powerful and flexible because:

  • Each module is focused on a single, well-defined task;
  • You can combine multiple modules in a playbook to automate complex workflows;
  • Modules are idempotent, meaning they only make changes if something needs to be updated;
  • Ansible provides hundreds of modules out of the box, covering a wide range of system administration tasks.

By understanding and using modules, you unlock the full potential of Ansible to manage your infrastructure efficiently and reliably.

Common Tasks Performed by Modules

  • Manage files: create, delete, copy, or modify files and directories on your systems;
  • Install packages: add or remove software packages using your system's package manager (such as apt for Ubuntu or yum for CentOS);
  • Configure services: start, stop, enable, or restart services like nginx, httpd, or sshd so they run as needed.

When you write a playbook, you specify which module to use and what parameters to give it. Ansible then runs the module on your target machines and reports the result. This approach ensures your systems always end up in the state you describe, no matter what their starting point was.

Example:

  • To make sure a file named hello.txt exists in /tmp, you use the file module with the correct path and state.
  • To install the nginx web server, you use the apt module (on Ubuntu) or yum module (on CentOS) with the package name.
  • To ensure the nginx service is running and enabled at boot, you use the service module with the service name and desired state.

By combining modules in a playbook, you automate complex tasks in a clear, repeatable way.

Examples: Common Ansible Modules

Explore these practical examples to see how to use some of the most common Ansible modules for managing systems.

Using the copy Module

Copy a local file to a target server:

- name: Copy a configuration file to the server
  ansible.builtin.copy:
    src: /home/ansible/config.txt
    dest: /etc/myapp/config.txt

Using the file Module

Set permissions and ownership on a directory:

- name: Ensure log directory exists with correct permissions
  ansible.builtin.file:
    path: /var/log/myapp
    state: directory
    owner: root
    group: root
    mode: '0755'

Using the apt Module

Install a package on a Debian-based system:

- name: Install nginx web server
  ansible.builtin.apt:
    name: nginx
    state: present
    update_cache: yes

Using the service Module

Start and enable a service:

- name: Ensure nginx is running and enabled
  ansible.builtin.service:
    name: nginx
    state: started
    enabled: yes

These examples show how you can automate routine tasks using Ansible modules. Adjust file paths, user names, and package names as needed for your environment.

How to Customize Modules with Parameters

You can customize each module by specifying parameters in your playbook or ad-hoc command. Parameters are provided as key-value pairs under the module name. For example, to create a new user named deploy and add it to the sudo group:

- name: Create a deploy user
  user:
    name: deploy
    state: present
    groups: sudo

To copy a configuration file with specific permissions:

- name: Copy nginx configuration
  copy:
    src: ./nginx.conf
    dest: /etc/nginx/nginx.conf
    owner: root
    group: root
    mode: '0644'

Always check the official Ansible documentation for a full list of parameters available for each module. Customizing modules with the right parameters ensures your automation is precise and effective.

question mark

Which Ansible module should you use to copy a file from your control node to a managed host

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2
some-alt