linux - How to store command output into array in Ansible?

Linux - How to store command output into array in Ansible?

In Ansible, you can store the output of a command into an array (list) using the shell or command module and then process it further. The output from these modules is stored in a variable, which can be transformed into a list using Ansible's split filter.

Here's a step-by-step guide to store command output into an array in Ansible:

Example Playbook

  1. Run a Command and Capture the Output:

    Use the shell or command module to run a command and capture its output.

  2. Split the Output into a List:

    Use the split filter to transform the command output into a list.

Sample Playbook

--- - name: Store command output into an array hosts: localhost gather_facts: no tasks: - name: Run a command and capture its output shell: "echo -e 'line1\nline2\nline3'" register: command_output - name: Debug the raw output debug: var: command_output.stdout - name: Split the command output into a list set_fact: output_list: "{{ command_output.stdout.split('\n') }}" - name: Debug the output list debug: var: output_list - name: Iterate over the list debug: msg: "Item: {{ item }}" loop: "{{ output_list }}" 

Explanation

  • Run a command and capture its output:

    - name: Run a command and capture its output shell: "echo -e 'line1\nline2\nline3'" register: command_output 

    This task runs a shell command and captures its output in the command_output variable.

  • Debug the raw output:

    - name: Debug the raw output debug: var: command_output.stdout 

    This task prints the raw output of the command to verify its content.

  • Split the command output into a list:

    - name: Split the command output into a list set_fact: output_list: "{{ command_output.stdout.split('\n') }}" 

    This task uses the split filter to convert the command output (which is a single string) into a list of lines. The delimiter used for splitting is \n (newline character).

  • Debug the output list:

    - name: Debug the output list debug: var: output_list 

    This task prints the list to verify that the command output has been correctly split into individual elements.

  • Iterate over the list:

    - name: Iterate over the list debug: msg: "Item: {{ item }}" loop: "{{ output_list }}" 

    This task iterates over the list and prints each item.

Running the Playbook

Save the playbook to a file, e.g., store_output.yml, and run it using the ansible-playbook command:

ansible-playbook store_output.yml 

This playbook demonstrates how to capture the output of a command, split it into a list, and then process each item in the list in Ansible. Adjust the shell command and delimiter in the split filter as needed for your specific use case.

Examples

  1. Ansible store command output in array

    • Description: Demonstrates how to capture the output of a command into an array variable in Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Run command and store output in array shell: "ls /path/to/directory" register: command_output - set_fact: file_list: "{{ command_output.stdout_lines }}" - debug: var: file_list 
  2. Ansible capture command output to list

    • Description: Shows how to capture the output of a command into a list variable using Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Run command and capture output to list shell: "cat /path/to/file.txt | grep keyword" register: command_output - set_fact: results_list: "{{ command_output.stdout_lines }}" - debug: var: results_list 
  3. Ansible save command output to array

    • Description: Provides guidance on saving the output of a command into an array variable in Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Execute command and save output in array command: "echo 'item{{ item }}'" with_sequence: start=1 end=5 register: command_output - set_fact: items_array: "{{ command_output.results | map(attribute='stdout') | list }}" - debug: var: items_array 
  4. Ansible capture command output into array

    • Description: Demonstrates capturing the output of a command into an array using Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Execute command and capture output into array shell: "ls /path/to/directory" register: command_output - set_fact: file_list: "{{ command_output.stdout_lines }}" - debug: var: file_list 
  5. Ansible command output to array variable

    • Description: Shows how to assign the output of a command to an array variable in Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Run command and assign output to array variable command: "ls /path/to/directory" register: command_output - set_fact: directory_files: "{{ command_output.stdout_lines }}" - debug: var: directory_files 
  6. Ansible store command output in list

    • Description: Provides steps to store the output of a command in a list variable in Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Execute command and store output in list shell: "cat /path/to/file.txt | grep keyword" register: command_output - set_fact: results_list: "{{ command_output.stdout_lines }}" - debug: var: results_list 
  7. Ansible command output to array

    • Description: Demonstrates how to convert the output of a command into an array using Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Run command and convert output to array shell: "ls /path/to/directory" register: command_output - set_fact: file_list: "{{ command_output.stdout_lines }}" - debug: var: file_list 
  8. Ansible assign command output to array

    • Description: Shows how to assign the output of a command to an array variable in Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Execute command and assign output to array variable shell: "ls /path/to/directory" register: command_output - set_fact: directory_files: "{{ command_output.stdout_lines }}" - debug: var: directory_files 
  9. Ansible save command output in array

    • Description: Provides instructions on saving the output of a command into an array in Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Run command and save output in array shell: "cat /path/to/file.txt | grep keyword" register: command_output - set_fact: results_array: "{{ command_output.stdout_lines }}" - debug: var: results_array 
  10. Ansible capture command output to array variable

    • Description: Demonstrates capturing the output of a command into an array variable using Ansible.
    • Code:
      # Example Ansible Playbook - hosts: localhost tasks: - name: Execute command and capture output to array variable shell: "ls /path/to/directory" register: command_output - set_fact: file_list: "{{ command_output.stdout_lines }}" - debug: var: file_list 

More Tags

flowlayoutpanel pushviewcontroller hystrix numeric-input default-value snowflake-cloud-data-platform powermockito spark-structured-streaming unused-variables restart

More Programming Questions

More Organic chemistry Calculators

More Electrochemistry Calculators

More Investment Calculators

More Cat Calculators