The output of a shell command in an Ansible playbook can be accessed using the register keyword to store the command's result in a variable. You can then reference this variable to access the output. Here's how it works:
Execute the Shell Command: Use the
shellmodule to run the command and register the output.Access the Output: The output can be accessed through the registered variable, specifically using
stdoutfor standard output orstdout_linesfor the output as a list of lines.
Example
- name: Execute a Shell Command
hosts: localhost
gather_facts: false
tasks:
- name: Run a command
shell: ls -l
register: command_output
- name: Display command output
debug:
var: command_output.stdout_lines
In this example, command_output.stdout_lines contains the output of the ls -l command as a list of lines.
