Executing Shell Commands Using the local_action Module in Ansible
The local_action module in Ansible allows you to execute shell commands on the control node (the machine running the Ansible playbook) instead of the remote hosts. This can be useful when you need to perform tasks that are specific to the control node, such as interacting with local files or services.
Here's how you can use the local_action module to execute shell commands:
Syntax
The basic syntax for using the local_action module is as follows:
- name: Execute a shell command on the control node
local_action:
module: shell
command: <your_shell_command>
register: command_output
In this example, the module parameter specifies that we want to use the shell module to execute a shell command. The command parameter is where you'll put the actual shell command you want to run.
The register keyword is used to capture the output of the command, which can be accessed later in the playbook.
Example Playbook
Let's look at an example playbook that demonstrates the use of the local_action module:
- hosts: localhost
connection: local
tasks:
- name: Get the current working directory
local_action:
module: shell
command: pwd
register: current_dir
- name: Print the current working directory
debug:
msg: "The current working directory is: {{ current_dir.stdout }}"
In this example, the playbook runs on the localhost (the control node) using the local connection type. The first task uses the local_action module to execute the pwd command, which prints the current working directory. The output of the command is stored in the current_dir variable using the register keyword.
The second task then uses the debug module to print the value of the current_dir.stdout variable, which contains the output of the pwd command.
Mermaid Diagram
Here's a Mermaid diagram that illustrates the flow of execution when using the local_action module:
graph TD
A[Ansible Playbook] --> B[local_action Module]
B --> C[Shell Command Execution]
C --> D[Output Capture]
D --> E[Variable Assignment]
E --> F[Further Processing]
The diagram shows that the local_action module is used within the Ansible playbook to execute a shell command on the control node. The output of the command is then captured and assigned to a variable, which can be used for further processing in the playbook.
Real-Life Example
Imagine you're a DevOps engineer responsible for managing a web application deployed on multiple servers. You need to occasionally generate a report that summarizes the disk usage on each server. Using the local_action module, you can create an Ansible playbook that collects this information and generates the report on your local machine.
Here's an example playbook:
- hosts: webservers
tasks:
- name: Get disk usage on remote hosts
command: df -h
register: disk_usage
- name: Generate disk usage report
local_action:
module: template
src: disk_report.j2
dest: /path/to/disk_report.txt
delegate_to: localhost
In this playbook, the first task uses the command module to run the df -h command on the remote hosts (the webservers group) and stores the output in the disk_usage variable.
The second task uses the local_action module to generate a disk usage report on the control node. It uses the template module to render a Jinja2 template (disk_report.j2) and saves the output to a file (/path/to/disk_report.txt). The delegate_to: localhost parameter ensures that this task runs on the control node, not the remote hosts.
By using the local_action module, you can perform tasks that are specific to the control node, such as generating reports, interacting with local services, or executing commands that require access to the control node's resources.
