Registering Script Output in Ansible
In Ansible, you can register the output of a script or command and use it in subsequent tasks. This is a powerful feature that allows you to capture and manipulate data from your playbooks, making your automation more dynamic and flexible.
Registering Output with the command
or shell
Modules
The most common way to register script output in Ansible is by using the command
or shell
modules. These modules allow you to execute a command or script on the remote host and capture the output.
Here's an example of how to register the output of a simple script:
- name: Run a script and register the output
command: /path/to/script.sh
register: script_output
In this example, the output of the /path/to/script.sh
script is captured and stored in the script_output
variable. You can then use this variable in subsequent tasks, for example, to print the output:
- name: Print the script output
debug:
var: script_output.stdout
The script_output
variable contains several attributes, such as stdout
, stderr
, rc
(return code), and changed
. You can access these attributes to handle the script's output and status.
Registering Output with the set_fact
Module
Another way to register script output is by using the set_fact
module. This module allows you to create or modify Ansible facts, which are variables that can be accessed throughout your playbook.
Here's an example of how to use set_fact
to register script output:
- name: Run a script and register the output
command: /path/to/script.sh
register: script_output
- name: Set a fact with the script output
set_fact:
script_result: "{{ script_output.stdout }}"
In this example, the script_output.stdout
value is stored in the script_result
fact, which can then be used in subsequent tasks.
Handling Script Errors
When registering script output, it's important to handle any errors that may occur. You can use the failed_when
or when
clauses to check the script's return code and take appropriate actions.
Here's an example of how to handle a script error:
- name: Run a script and register the output
command: /path/to/script.sh
register: script_output
failed_when: script_output.rc != 0
In this example, if the script's return code (script_output.rc
) is not 0 (indicating a successful execution), the task will be marked as failed, and you can handle the error accordingly.
Visualizing the Workflow with Mermaid
Here's a Mermaid diagram that illustrates the process of registering script output in Ansible:
This diagram shows the flow of registering script output, accessing the output, handling any errors, and then using the registered output in subsequent tasks.
Real-World Example: Checking Disk Usage
Let's consider a real-world example where you need to check the disk usage on a remote host and take action if the usage exceeds a certain threshold.
- name: Check disk usage
command: df -h
register: disk_usage
- name: Set a fact with the disk usage
set_fact:
disk_usage_percent: "{{ disk_usage.stdout_lines[1].split()[4][:-1] }}"
- name: Print the disk usage
debug:
msg: "Disk usage is {{ disk_usage_percent }}%"
- name: Warn if disk usage is high
warn:
msg: "Disk usage is high at {{ disk_usage_percent }}%"
when: disk_usage_percent | int > 80
In this example, we first run the df -h
command to get the disk usage information. We then use the set_fact
module to extract the percentage of disk usage from the command output and store it in the disk_usage_percent
variable.
Finally, we print the disk usage and use the warn
module to display a warning message if the disk usage exceeds 80%.
By registering the script output and using it in subsequent tasks, you can create more dynamic and intelligent automation workflows in Ansible.