Ansible – Command vs Shell Module

The Shell Module as well as command modules are used to execute Binary Commands.

The command module is the default module to execute the binary commands, if we do not specify the module name in our ad-hoc command, ansible will by default use the command module.

The command module will execute the commands without proceeding to through the shell.

The command module will not recognize certain variables like $HOME

Stream operators like <,>, &, and | will not work with command modules.

The command module is more secured, compared to the shell module

For example, both shell and command module will have the same outputs,

ansible localhost -m shell -a "uptime"
ansible localhost -m command -a "uptime"

The stream operator, when passed through the shell and command module, will show different results, as the command module doesn’t recognize the stream operators and will throw errors, whereas the same will work fine with the shell module.

Command Module, will throw an error as it will not recognize > stream operator

ansible localhost -m command -a "uptime > uptime.txt"

Shell Module

ansible localhost -m shell -a "uptime > uptime.txt"

Ansible – Default Command module

The Ansible command module is the default module that ansible. It is used to execute Binary Commands.

The command module will execute if we do not specific module name in Ansible Ad-Hoc commands.

ansible localhost -m command -a "uptime"

In the above example, we are passing the module as a command, if we skip the module name, ansible will still execute the command and will pick the command as the default module, the output will be the same for both ad-hoc commands.

ansible localhost -a "uptime"

Ansible – Install Packages

Ansible can be used to install application packages like Git, apache, Mysql, etc. These packages can be installed using Ansible Ad-Hoc commands or through ansible Playbooks.

Syntax – To install the git package on the Ubuntu system.

ansible localhost -m apt -a "name=git state=present" -b

  • localhost – inventory, the host to be targeted.
  • -m module name, here we are using apt for Ubuntu/Debian systems. For the Linux systems using yum.
  • -a arguments passed for installation of the package.
    • name – package name to be installed.
    • state – Ansible provides multiple state options
      • present/installed – to install package
      • latest – to update the existing package
      • absent/removed – to uninstall package
  • -b argument to change the owner, like to grant sudo privileges

Output –

Ansible – Modules

Ansible Modules are pre-defined, reusable scripts, Ansible ships the default modules with the install package, and also allows the creation of custom modules.

AnsibleModules can be used with Ansible Ad-hoc commands or with Ansible-playbook.

To list all modules available with your Ansible installation run the below command,

> ansible-doc -l

Ansible – Ad-hoc Command Structure

Ansible provides us the option to either run playbooks or ad-hoc commands on the local and remote nodes through ansible engines.

Let us look at an Ansible ad-hoc command example

> ansible localhost -m shell -a "uptime"

The above command can be divided into the following parts,

  • localhost – You can specify the target host/groups here where you want the command to be executed.
  • -m shell – You define the Ansible module, in our example shell.
  • -a “uptime” – Pass the command arguments in double quotes string.

To run the commands on custom inventory host files,

> ansible localhost -m shell -a "uptime" -i custom_inventory_file

On a similar line, we can create our own ad-hoc commands.

ansible.cfg – file priority

We can have the ansible.cfg file at various locations, the default location shipped with ansible installation is at,

/etc/ansible/ansible.cfg

The default ansible.cfg has the least priority when you have multiple ansible.cfg defined in your system/server.

The priority is set to ansible.cfg file is based on how it is defined or stored,

  • ANSIBLE_CONF – Environment variable, defined in your server/system.
  • ./ansible.cfg – Ansible config file defined in your current working directory.
  • ~/.ansible.cfg – Ansible config file defined in your home directory.
  • /etc/ansible/ansible.cfg – Ansible config file defined in default location.