How to effectively use the local_action module in Ansible?

AnsibleAnsibleBeginner
Practice Now

Introduction

Ansible, a widely-adopted IT automation tool, offers a versatile module called "local_action" that allows you to execute tasks on the control node rather than remote hosts. In this tutorial, we will explore how to effectively utilize the local_action module to enhance your Ansible-powered workflows.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ansible(("`Ansible`")) -.-> ansible/ModuleOperationsGroup(["`Module Operations`"]) ansible(("`Ansible`")) -.-> ansible/AnsibleSetupandConfigurationGroup(["`Ansible Setup and Configuration`"]) ansible(("`Ansible`")) -.-> ansible/PlaybookEssentialsGroup(["`Playbook Essentials`"]) ansible/ModuleOperationsGroup -.-> ansible/shell("`Execute Shell Commands`") ansible/AnsibleSetupandConfigurationGroup -.-> ansible/local_action("`Delegate Action Locally`") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("`Execute Playbook`") ansible/ModuleOperationsGroup -.-> ansible/command("`Execute Commands`") subgraph Lab Skills ansible/shell -.-> lab-414868{{"`How to effectively use the local_action module in Ansible?`"}} ansible/local_action -.-> lab-414868{{"`How to effectively use the local_action module in Ansible?`"}} ansible/playbook -.-> lab-414868{{"`How to effectively use the local_action module in Ansible?`"}} ansible/command -.-> lab-414868{{"`How to effectively use the local_action module in Ansible?`"}} end

Understanding the local_action Module

The local_action module in Ansible is a powerful tool that allows you to execute tasks on the control node (the machine running the Ansible playbook) instead of the remote hosts. This module is particularly useful when you need to perform actions that are specific to the control node, such as interacting with local files, services, or APIs.

What is the local_action Module?

The local_action module is a special type of Ansible module that runs the specified task on the control node, rather than on the remote hosts. This is different from the default behavior of Ansible, where tasks are executed on the remote hosts.

When to Use the local_action Module?

The local_action module is useful in the following scenarios:

  1. Local File Operations: When you need to interact with files or directories on the control node, such as creating, modifying, or deleting files.
  2. Local Service Management: When you need to manage services or daemons running on the control node, such as starting, stopping, or restarting a service.
  3. API Interactions: When you need to interact with APIs that are only accessible from the control node, such as querying a local database or calling a web service.
  4. Local Environment Checks: When you need to perform checks or gather information about the control node, such as checking available disk space or network connectivity.

How to Use the local_action Module?

To use the local_action module, you can include it in your Ansible playbook like any other module. Here's an example:

- name: Create a local file
  local_action:
    module: file
    path: /tmp/local_file.txt
    state: touch

In this example, the local_action module is used to create a file named local_file.txt in the /tmp directory on the control node.

You can also use the local_action module in combination with other modules to perform more complex tasks on the control node.

- name: Check available disk space on the control node
  local_action:
    module: command
    args:
      cmd: df -h
  register: disk_space
- debug:
    var: disk_space.stdout_lines

In this example, the local_action module is used to execute the df -h command on the control node, and the output is stored in the disk_space variable, which is then printed using the debug module.

By understanding the capabilities and use cases of the local_action module, you can effectively leverage it in your Ansible playbooks to perform tasks that are specific to the control node.

Leveraging local_action for Task Execution

The local_action module in Ansible provides a flexible way to execute tasks on the control node, allowing you to leverage its capabilities for a wide range of use cases. In this section, we'll explore how to effectively use the local_action module to execute tasks on the control node.

Executing Local Commands

One of the primary use cases for the local_action module is to execute commands on the control node. This can be particularly useful when you need to perform system-level tasks or interact with local resources that are not accessible from the remote hosts.

- name: Execute a local command
  local_action:
    module: command
    args:
      cmd: ls -l /tmp
  register: local_command_output
- debug:
    var: local_command_output.stdout_lines

In this example, the local_action module is used to execute the ls -l /tmp command on the control node, and the output is stored in the local_command_output variable, which is then printed using the debug module.

Interacting with Local Files and Directories

The local_action module can also be used to interact with files and directories on the control node. This can be useful for tasks such as creating, modifying, or deleting files, as well as managing directory structures.

- name: Create a local directory
  local_action:
    module: file
    path: /tmp/local_directory
    state: directory
- name: Create a local file
  local_action:
    module: file
    path: /tmp/local_directory/local_file.txt
    state: touch

In this example, the local_action module is used to create a directory named local_directory in the /tmp directory on the control node, and then create a file named local_file.txt within that directory.

Calling Local APIs and Services

The local_action module can also be used to interact with local APIs and services, such as querying a local database or calling a web service that is only accessible from the control node.

- name: Call a local API
  local_action:
    module: uri
    url: http://localhost:8080/api/data
  register: api_response
- debug:
    var: api_response.json

In this example, the local_action module is used to call a local API running on the control node, and the response is stored in the api_response variable, which is then printed using the debug module.

By understanding how to leverage the local_action module for task execution, you can enhance the flexibility and power of your Ansible playbooks, allowing you to perform a wide range of tasks on the control node.

Practical Examples of local_action Usage

In this section, we'll explore some practical examples of how to use the local_action module in your Ansible playbooks. These examples will cover a variety of use cases and demonstrate the versatility of this powerful module.

Backing Up Local Files

One common use case for the local_action module is to perform backups of files and directories on the control node. This can be particularly useful when you need to preserve local configurations or data before making changes.

- name: Backup local configuration files
  local_action:
    module: archive
    path:
      - /etc/nginx/conf.d
      - /etc/mysql/my.cnf
    dest: /tmp/local_backup.zip
    format: zip

In this example, the local_action module is used to create a ZIP archive of the /etc/nginx/conf.d and /etc/mysql/my.cnf directories on the control node, and the archive is saved to the /tmp/local_backup.zip file.

Generating SSL Certificates

Another practical use case for the local_action module is to generate SSL certificates on the control node. This can be useful when you need to create self-signed certificates for testing or development purposes.

- name: Generate a self-signed SSL certificate
  local_action:
    module: openssl_certificate
    path: /etc/ssl/certs/local_cert.pem
    privatekey_path: /etc/ssl/private/local_key.pem
    state: present
    organization_name: LabEx
    country_name: US
    email_address: admin@example.com

In this example, the local_action module is used to generate a self-signed SSL certificate and private key on the control node, and the certificate is stored in the /etc/ssl/certs/local_cert.pem file, while the private key is stored in the /etc/ssl/private/local_key.pem file.

Interacting with a Local Database

The local_action module can also be used to interact with a local database on the control node. This can be useful for tasks such as running SQL queries or performing database backups.

- name: Query a local MySQL database
  local_action:
    module: mysql_query
    login_user: root
    login_password: password
    query: SELECT * FROM users
  register: database_query
- debug:
    var: database_query.results

In this example, the local_action module is used to execute a SQL query against a local MySQL database on the control node, and the results are stored in the database_query variable, which is then printed using the debug module.

By exploring these practical examples, you can gain a better understanding of how to effectively use the local_action module in your Ansible playbooks to perform a wide range of tasks on the control node.

Summary

By the end of this guide, you will have a comprehensive understanding of the local_action module in Ansible, including its benefits, practical use cases, and best practices for seamless integration into your automation processes. Unlock the full potential of Ansible and streamline your infrastructure management with the insights provided in this tutorial.

Other Ansible Tutorials you may like