Hello Ansible - Your First Command

Last Updated:

So, you have Ansible installed. Now what?

The simplest command for Ansible to process is a local command, which runs on the same machine as Ansible, typically your personal computer.

Your First Command

Here's the full command below, and it might look cumbersome at first.

Don't worry though, there are explanations of each piece and we'll simplify things as part of this tutorial series:

  
    ansible all --inventory "localhost," --module-name debug --args "msg='Hello Ansible'"
  

What this code does is connect to your computer (localhost) from your local computer, through Ansible and display some output.

Now, let's break it down...

The Executable Program (Binary)

  
    ansible
  

This part runs the “ansible” executable, one of the programs that comes in the full Ansible suite that you installed earlier

Which Machines?

  
    all
  

When we say “all” we mean “do this on all machines that are listed in the inventory file,” which is the next part of the command

List of Machines

  
    --inventory "localhost,"
  

The inventory in Ansible is where the nitty-gritty details of the machines are listed. Things like IP addresses, usernames, and much more. In this case, we only have one machine which is our local computer, where we're running Ansible from.

We're providing this information in-line because of how simple our “setup” is (one machine). As we grow, we'll move this information to an “inventory file” that we can give to Ansible instead of typing it all out on the command line.

Module to Use

  
    --module-name debug
  

Now that we have specified what machines we want to target, we need to tell Ansible what we want to do with those machines. In this case, We are using something called the “debug” module, which outputs information to our screen so we can get a better idea of what Ansible is doing.

Arguments (Parameters)

  
    --args "msg='Hello Ansible'"
  

We need to provide some information to the debug module so it can output to the screen, and we do this by providing arguments. The whole group of characters after “–args” has to be quoted due to the command line, so it interprets the entire thing as one and ignores the spaces. Here, we're passing an action of “msg” (short for “message”), an equal sign, and a bunch of characters we want to output in single quotes so they don't conflict with the double quotes we are already using.

The Result

After running the command:

  
    ansible all --inventory "localhost," --module-name debug --args "msg='Hello Ansible'"
  

We get the result:

  
    localhost | SUCCESS => {
        "msg": "Hello Ansible"
    }
  

Cleaning Up

That's a lot to type, for example “–inventory” and “–module-name”, in order to configure Ansible. The good news is we can shorten them considerably, and I only typed them all out so the meaning was more clear when introducing these concepts. Compare the command we ran to this shorter, identical command:

  
    ansible all -i "localhost," -m debug -a "msg='Hello Ansible'"
  

Much shorter, right? But also hard to know what “-m” and “-a” actually mean unless you understand the backstory.