How is the output of a shell command accessed in an Ansible playbook?

QuestionsQuestions4 SkillsProAnsible Shell ModuleAug, 22 2025
0374

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:

  1. Execute the Shell Command: Use the shell module to run the command and register the output.

  2. Access the Output: The output can be accessed through the registered variable, specifically using stdout for standard output or stdout_lines for 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.

0 Comments

no data
Be the first to share your comment!