A module that allows you to run remote commands on your local machine and execute them with no SSH or VNC connection.
The “ansible shell command examples” is a module that allows users to run remote commands with the Ansible Shell. The module can be used in many different ways, and its usage is not limited to just one specific way.
Ansible is a well-known automation software that lets you manage thousands of nodes at once. With the Ansible shell module, you may execute ad-hoc commands on distant machines, which is one of the most valuable capabilities of Ansible.
The shell module lets you execute ad hoc commands or even tiny scripts as if you were sitting in front of each machine’s local terminal. If you need to perform a command on a managed node fast, this module comes in useful.
You’ll learn what the Ansible shell module is, how it works, and how to use it to execute commands on remote servers in this course.
This guide will walk you through the Ansible shell module step by step. If you want to join in, make sure you have the following items:
- A host for Ansible controllers – Ansible v2.9.18 will be used in this course on an Ubuntu 18.04.5 LTS computer. Here’s how to create an Ansible controller host.
How to Configure WinRM for Ansible over HTTPS
- An Ansible controller host user account that allows you to develop basic playbooks and execute them on controlled nodes.
- A distant computer on which to execute instructions – This tutorial will use the SRV1 node and the webserver.
Using the Ansible Shell Module to Execute Ad-Hoc Commands
The Ansible shell module may perform a single command on a remote computer in its most basic version. A single-line command on your Ansible controller will suffice. For example, let’s execute a simple command on a remote computer to report the value of the PATH environment variable.
Run the following command from your Ansible controller. This command connects to the webserver machine using the shell module (-m) and passes an argument (-a) that is the command to run. In this case, echo $PATH is being used to return the value of the PATH environment variable.
# webserver using ansible -m ansible.builtin.shell -a ‘echo $PATH’ is a syntax that may be used with any module.
webserver using ansible
Commands Executed Within a Playbook
When it comes to the Ansible shell module, appearances might be misleading. This module allows you to execute remote instructions in a variety of ways. But first, let’s start simply and learn how to execute a command.
Assume you need to create a text file at /opt/new file.txt on a Linux server on a managed node. You want to accomplish that via the Ansible shell module.
1. Log in to the Ansible controller host using SSH.
2. In your home directory, create an ansible shell module demo directory. The [playbook] you’ll need to execute the shell module will be in this directory.
/ansible shell module demo/mkdir cd /ansible shell module demo/ansible shell module demo
3. In the /ansible shell module demo directory, create a file named my playbook.yml and paste the contents of the following YAML playbook into it. This playbook just contains one job that utilizes the Ansible shell module to deliver text to the /opt/new file.txt text file (I’m creating a new file).
Ansible will need to execute with sudo permissions using the become attribute since the playbook will be generating a file in the /opt directory.
YAML is used to write Ansible playbooks. [Click here] to learn more about YAML.
tasks: – name: Create a text file in opt directory using /bin/sh shell ansible.builtin.shell: echo “I am creating a new file” > /opt/new_file.txt become: true # Ensure that the highest permissions are used on the remote host
When you provide shell as the module to utilize for a job, the Bash shell is used by default. You must use ansible.builtin.shell to call the Ansible shell module. If your remote machines are Windows-based, you must instead utilize the ansible. windows.win shell module.
4. Under /ansible shell module demo, create a subdirectory named SRV1. This folder represents the remote node on which Ansible will run the playbook.
Demo folder structure
5. Invoke the playbook to replicate the job to the remote computer and run it.
my playbook.yml ansible-playbook
Inventory is just a list of remote hosts organized by their hostnames or IP addresses. Click here to learn more about how to generate inventories.
The TASK has a changed status, indicating that the remote host was not in the right condition and was modified to perform the command.
TASK HAS TRANSFORMED
In the previous example, you ran the Ansible shell module to invoke a simple command. Sometimes, you need to customize that behavior Argumentation. Arguments allow you to pass configuration values to the shell module as it runs.
When you execute a shell command, for example, the command has a working directory or a directory that the command is aware of. You may need to execute a command from the /opt directory. You’d use the chdir parameter to adjust that behavior.
If you’re still in your Ansible controller host, do the following:
1. Open the playbook in your newly generated preferred text editor.
2. Replace the content in the playbook with the one below. The args property is used in this playbook to give the chdir argument to the job, which changes the working directory to /opt before running the command.
tasks: – name: Change the working directory to /opt before executing the command ansible.builtin.shell: ls -lh >> my_text_file.txt args: chdir: /opt # Changes to /opt directory
The output is the same as before, as you can see below.
Ansible shell module command output using the chdir option
3. Reopen the playbook and put the following YAML into it. The playbook that follows follows the same steps as the previous one. Instead, it uses the cmd parameter to provide the command to execute (ls -lh) on its own line, eliminating the requirement for the args attribute.
tasks: – name: Change to /opt ansible.builtin.shell as the working directory: cmd: ls -lh # chdir: to list files in the /opt directory # switches to the /opt directory
4. On remote Linux nodes, the shell module utilizes the sh shell by default. However, the executable argument may be used to modify this. The ls command will now utilize the Bash shell if the executable parameter is used as shown below.
tasks: – name: ls -lh args: executable: /bin/bash # Reads all log files in the /opt directory ansible.builtin.shell: ls -lh In the Bash shell, type ls.
Multiple Commands Execution
You’ve only ever ran one command at a time so far. Let’s make a difference. The Ansible shell module may also be used to perform many commands at once.
On the host of your Ansible controller:
1. In the /ansible shell module demo directory, open a text editor and create a new playbook named my playbook2.yml.
2. In the my playbook2.yml file, copy and paste the following playbook. By performing four independent commands in the same playbook, the shell module will not produce four separate files.
tasks: – name: Create multiple text file in tmp directory with shell module ansible.builtin.shell: | # Multiple commands in Ansible shell module echo “This will go in log file” > /tmp/log_file.txt echo “This will go in memory file”> /tmp/memory_file.txt echo “This will go in disk file” > /tmp/disk_file.txt echo “This will go in version file”> /tmp/version_file.txt become: true args: chdir: /var/log # Changing the directory
3. Run the playbook on the SRV1 node now.
my playbook.yml ansible-playbook –inventory SRV1
You should see that the TASK has been altered.
Playbook execution in the SRV1 node
4. Check that Ansible correctly generated the files on the remote host.
Numerous commands resulted in the creation of multiple files.
Producing Debug Output
When playbooks fail to perform as expected, it’s time to start troubleshooting. You’ll occasionally need more specific information about what’s going on within the playbook while troubleshooting. You’ll need to read the debug output in such instance.
1. Perhaps the prior playbook was creating issues, and you’d want to look into that. Use the debug argument in such instance, as demonstrated below.
When Ansible executes this playbook, it will provide JSON output that shows the command execution in detail.
tasks: – name: Create multiple text file in tmp directory with shell module shell: | # Multiple commands in Ansible shell module echo “This will go in log file” > /tmp/log_file.txt echo “This will go in memory file”> /tmp/memory_file.txt echo “This will go in disk file” > /tmp/disk_file.txt echo “This will go in version file”> /tmp/version_file.txt register: shell_output become: true args: chdir: /var/log # Changing the directory – debug: var=shell_output # This will provide the output
2. Run the playbook once again against the SRV1 node.
my playbook.yml ansible-playbook –inventory SRV1
If everything works well, you should get something like this.
output of shell module
3. Connect to the distant computer and check that the files were generated again.
Using the Ansible shell command, several files were successfully produced from numerous commands.
The Ansible shell module is an excellent tool for executing commands on remote systems. It offers a number of different functions as well as a convenient method to perform instructions remotely.
Ansible is a tool that can be used to manage and automate systems. The “ansible run shell command line” is a module which allows users to execute remote commands with the Ansible Shell.
- ansible shell module multiple commands
- ansible shell vs command
- ansible run command on remote host
- ansible execute command
- ansible run shell script with arguments