In this guide, you’ll learn how to create efficient shell scripts that execute ad-hoc Ansible commands. These scripts are especially useful for automating multiple tasks sequentially – for example, running a command to ping all nodes and then printing the contents of theDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
/etc/hosts file on each node.
Embedding Ansible configuration parameters as environment variables directly in your shell script ensures that your setup is consistently applied every time you run your commands.
Setting Up Your Environment
Previously, we discussed configuring Ansible parameters through environment variables. Instead of setting these variables manually each time, you can include them in your shell script. This guarantees that the required configurations are always in place before any command execution. Below is an example of a shell script that sets an environment variable and then runs several Ansible commands:Running Your Shell Script
There are two convenient methods to execute your shell script:| Method | Command Example |
|---|---|
| Using the sh command | sh script_name.sh |
| Making the script directly executable | chmod 755 script_name.sh followed by ./script_name.sh |
Before running any shell script, ensure you review its contents for security. Running untrusted scripts, especially with elevated privileges, may put your system at risk.