Ansible Inventory Variables – ansible_shell_type with Examples
Ansible, an open-source automation tool, allows system administrators to automate tasks such as configuration management, application deployment, and many more. This tool has a multitude of configuration options that dictate the way Ansible interacts with the managed hosts. In this blog post, we will discuss the
ansible_shell_type inventory variable, explaining its purpose and how to use it.
ansible_shell_type variable is used to specify the type of shell that Ansible should use on the remote host. By default, Ansible uses the ‘/bin/sh’ shell for executing commands. However, you can change this to a different shell (such as ‘/bin/bash’, ‘/bin/csh’, ‘/bin/ksh’, etc.) if the commands you’re running require features not available in ‘/bin/sh’.
Defining ansible_shell_type in the Inventory File
ansible_shell_type, you need to define it in your inventory file. Here is the format:
alias ansible_host=your_actual_host ansible_user=your_user ansible_shell_type=your_shell_type.
Example with ansible_shell_type
Let’s say you have a server with the IP address
192.168.1.100, and you want Ansible to connect as the user
deploy and use the ‘/bin/bash’ shell for command execution. Here’s how you could define this in your inventory file:
my_server ansible_host=192.168.1.100 ansible_user=deploy ansible_shell_type=/bin/bash
In this scenario, Ansible connects to
my_server and uses ‘/bin/bash’ shell for command execution.
Using ansible_shell_type in a Playbook
ansible_shell_type is defined in your inventory file, Ansible will use the specified shell when it connects to the host. Here’s an example playbook:
- hosts: my_server
- name: Create a directory
command: mkdir /opt/my_directory
When you run this playbook, Ansible connects to
192.168.1.100) as the
deploy user, and uses ‘/bin/bash’ shell (because
ansible_shell_type=/bin/bash) for running the ‘mkdir’ command.
ansible_shell_type inventory variable provides you with the flexibility to choose the shell that Ansible should use on the remote host. This can be very useful in cases where certain commands require a specific shell for execution.