Ansible Advanced Course

Install and Configure

Create simple shell scripts that run ad hoc Ansible commands

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 the /etc/hosts file on each node.

Tip

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:

export ANSIBLE_GATHERING=explicit
ansible -m ping all
ansible -a 'cat /etc/hosts' all
ansible-playbook playbook.yml

Running Your Shell Script

There are two convenient methods to execute your shell script:

MethodCommand Example
Using the sh commandsh script_name.sh
Making the script directly executablechmod 755 script_name.sh followed by ./script_name.sh

Choose the method that best suits your environment for running the script.

Security Warning

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.

Conclusion

Embedding environment variables within your shell scripts simplifies the execution of Ansible playbooks and ad-hoc commands, minimizing the risk of misconfiguration and saving time. For further study, consider revisiting related exercises on shell scripting and Ansible commands to reinforce your knowledge.

For additional resources, check out the Ansible Documentation to enhance your understanding of best practices and advanced usage tips.

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Validate a working configuration using ad hoc Ansible commands