Capturing Command Output in Ansible
In Ansible, capturing the output of a command or task is a common requirement, as it allows you to store the result for further processing or use it in subsequent tasks. Ansible provides several ways to capture command output, and the choice depends on your specific use case.
Using the command
or shell
Modules
The most straightforward way to capture command output in Ansible is to use the command
or shell
modules. These modules allow you to execute a command on the remote host and store the output in a variable.
Here's an example using the command
module:
- name: Capture output of 'ls -l' command
command: ls -l
register: ls_output
- name: Print the captured output
debug:
var: ls_output.stdout
In this example, the command
module executes the ls -l
command on the remote host, and the output is stored in the ls_output
variable. The stdout
attribute of the ls_output
variable contains the standard output of the command.
Alternatively, you can use the shell
module, which allows you to execute more complex shell commands:
- name: Capture output of 'df -h' command
shell: df -h
register: df_output
- name: Print the captured output
debug:
var: df_output.stdout
The shell
module is more flexible than the command
module, as it allows you to use shell features like pipes, redirections, and environment variables.
Using the capture_output
Option
In Ansible 2.9 and later versions, you can use the capture_output
option to capture the output of a task. This option can be used with any module, not just the command
and shell
modules.
Here's an example:
- name: Capture output of 'uname -a' command
command: uname -a
capture_output: yes
register: uname_output
- name: Print the captured output
debug:
var: uname_output.stdout
In this example, the capture_output
option is set to yes
, which causes Ansible to capture the standard output of the uname -a
command and store it in the uname_output
variable.
Handling Error Codes
When capturing command output, you may also want to handle any error codes returned by the command. You can do this by checking the rc
(return code) attribute of the captured output:
- name: Capture output of 'cat /non-existent-file'
command: cat /non-existent-file
register: cat_output
ignore_errors: yes
- name: Print the captured output
debug:
var: cat_output.stdout
- name: Check the return code
debug:
msg: "Command failed with return code {{ cat_output.rc }}"
when: cat_output.rc != 0
In this example, the ignore_errors: yes
option allows the task to continue even if the cat /non-existent-file
command fails. The rc
attribute of the cat_output
variable contains the return code of the command, which can be checked in a subsequent task.
Visualizing the Workflow
Here's a Mermaid diagram that visualizes the workflow of capturing command output in Ansible:
This diagram shows the different ways to capture command output in Ansible, and how you can handle the return code of the executed command.
In conclusion, Ansible provides several ways to capture the output of commands, including using the command
or shell
modules, or the capture_output
option. Handling the return code of the executed command is also important, and can be done by checking the rc
attribute of the captured output. By understanding these techniques, you can effectively incorporate command output into your Ansible playbooks and automate your infrastructure management tasks.