Ansible Basics 03: Running Ad-Hoc Commands

Ansible consists of several key components that collectively build a robust automation and configuration management. Ad-hoc commands are the easiest and fastest way to run some commands remotely or on a host. You have to understand the basic terminology to do this, so here is a quick start, and later we are going to go into the details in the upcoming articles.

The inventory is a crucial part, serving as a list of managed hosts, allowing Ansible to target specific systems.

Modules represent discrete units of programming work and encapsulate specific functionalities, such as managing packages or configuring services. These modules are grouped in collections, and they are the building blocks for Ansible tasks and playbooks, providing a wide array of actions that can be executed on target hosts.

Ad-hoc commands, on the other hand, are concise, one-off commands executed directly from the command line without the need for a playbook. They enable quick and direct interactions with managed systems, making them handy for troubleshooting or performing simple tasks.

Together, these components enable users to orchestrate complex automation tasks efficiently and maintain consistent configurations across their infrastructure using Ansible.

The other elements of the Ansible ecosystem:

Control node

A system on which Ansible is installed.

Inventory

An organized list of managed nodes.

Managed node

A remote or local host that Ansible controls.

Inventory quick guide

An Ansible inventory is a structured list of hosts or nodes that Ansible can manage, providing a foundation for orchestrating automation tasks across a network. We will go into the details later, for now let’s create an ini file and put a managed machine’s IP in it.

# inventory.ini:
192.168.1.91

Running the first command

Issue the following command in the ansible project directory and take a look at its output:

ansible -i inventory.ini -m ping 192.168.1.91 --ask-pass

After providing the login password for the remote machine you should see something like this:

192.168.1.91 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

If you see an error message about the missing ssphass, then on the Ansible master machine install the package.

sudo apt install sshpass
OR
sudo dnf install sshpass

The successful output means that the remote server is reachable by our ansible master node, the ansible user can authenticate to it using the SSH.

  • The ansible command line interface is used to issue ad-hoc commands.
  • The -i option followed by our inventory file will load that specific file.
  • The -m option loads a module to run on the remote or local host from the inventory. This case it is the ping module.
  • The last argument (192.168.1.91) is the remote host from the list of the hosts from the inventory. This case our inventory file contains only one host.

You can use the same modules in ad-hoc commands and playbooks.

Let’s run a simple shell command!

ansible -i inventory.ini -m command -a "whoami" 192.168.1.52
192.168.1.52 | CHANGED | rc=0 >>
tmolnar
  • This case the -m is followed by the command module. In its most simple form you can give it only one argument with the -a option, a command to run.
  • The example above ran the whoami command on the host from the inventory.

Now you are ready to take a look at the other elements of the Ansible environment.

If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES

Now we have an IRC channel as well: irc.libera.chat / #tomsitcafe

One thought on “Ansible Basics 03: Running Ad-Hoc Commands

Leave a comment