Multiline shell script in Ansible

In Ansible, a multiline shell script refers to a script that spans multiple lines and contains multiple commands or instructions. Writing multiline shell scripts in Ansible allows you to perform complex tasks on the managed hosts, automate tasks that involve multiple commands, and maintain better code readability.

Using the script module

To execute a multiline shell script in Ansible using the script module, you can use the YAML pipe symbol (|) to create a multiline string and pass it as the script content. This allows you to write the shell script in a readable and structured way.

The script module will run the shell script on the target hosts and execute each command sequentially. You can include variables, conditional statements, loops, and any other standard shell script constructs as needed. Here's how you can do it:

---
- name: Execute Multiline Shell Script using script module
  hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: Create a temporary shell script file
      copy:
        content: |
          #!/bin/bash
          echo "Hello, World!"
          echo "This is a multiline shell script using script module."
          echo "You can write more commands here."
        dest: /tmp/multiline_script.sh
        mode: '0755'

    - name: Execute the script using script module
      script: /tmp/multiline_script.sh
      register: script_output

    - name: Display script output using script module
      debug:
        var: script_output.stdout_lines

    - name: Remove the temporary script file
      file:
        path: /tmp/multiline_script.sh
        state: absent

Code explanation

  • Line 1: The three dashes (---) indicate the beginning of a YAML file. This Ansible playbook is starting, and it's written in YAML format.

  • Line 2: The name field specifies a descriptive name for the playbook. In this case, the playbook is named Execute Multiline Shell Script using script module.

  • Line 3: The hosts section specifies the target hosts for the playbook. In this case, it targets the localhost, which means the playbook will run on the local host.

  • Line 4: The connection option is set to local, which tells Ansible to execute tasks locally on the control node (the machine where Ansible is running) instead of using SSH to connect to remote hosts.

  • Line 5: The gather_facts option is set to false. This means Ansible will not gather facts (system information) about the target host before running the tasks.

  • Line 7: The tasks section starts here, where you define a list of tasks to be executed.

  • Line 8–16: The first task is named Create a temporary shell script file. It uses the copy module to create a file at the destination /tmp/multiline_script.sh. The content field specifies the script content, which is a multiline shell script. The script file will have the permissions set to 0755, making it executable.

    • Note that here we use | to use multiline commands.

  • Line 18–20: The second task is named Execute the script using script module. It uses the script module to execute the script located at /tmp/multiline_script.sh. The output of the script execution is registered in the variable script_output.

  • Line 22–24: The third task is named Display script output using script module. It uses the debug module to display the output of the previously executed script. It uses the var field to specify the variable script_output.stdout_lines, which contains the lines of output from the script.

  • Line 26–29: The fourth task is named Remove the temporary script file. It uses the file module to remove the temporary script file located at /tmp/multiline_script.sh.

Conclusion

The provided Ansible playbook demonstrates the execution of a multiline shell script using the script module. The playbook creates a temporary shell script with multiple commands, executes it on the localhost, displays the output, and finally removes the temporary script file. It showcases how Ansible can handle complex tasks using the script module to run custom scripts efficiently.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved