Ansible Ad-Hoc command - ansible command
How to use the ping module, execute a command and retrieve the Ansible Facts from a target node via the ansible command line.


What does the ansible command?
I’m going to show you a live demo. I’m Luca Berton, and welcome to today’s episode of Ansible Pilot.
ansible command
- Included in Ansible installation
- command line
- Ansible ad-hoc
The ansible
command is probably the first helpful command to learn when you start your journey with Ansible.
It is included in every Ansible installation for the most modern operating system.
It relies on Python language and some libraries such as Jinja2, YAML, WinRM, etc.
It is a command line tool so interact with that using your terminal.
Using the ansible
command, you could perform some operation to your target node(s), for example, executing single modules or retrieving system information (AKA Ansible Facts).
Each command in the Ansible jargon is called a module. Each module has its own parameter for the execution that you could read in the documentation.
It is useful when you would like to execute only one module (AKA task) against a limited amount of host(s).
The next step in your automation journey will be to use the ansible-playbook
command with an Ansible Playbook that enables you to execute more tasks against more hosts.
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
Let me show you how to execute some Ansible ad-hoc commands via ansible command.
I will show you how to use the ping module, run a command and retrieve the Ansible Facts from a target node via the ansible command line.
ping module
You can execute any Ansible module, for example ping , using the following Ansible ad-hoc command:
ansible -m ping host1.example.com
The output is like this:
host1.example.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
As you can see, the connection was successful, green color text, and a “ping: pong” was exchanged between the controller and the target Ansible nodes.
run a command
You can execute any Linux commands ( behind the scene, the ansible.builtin.command
module) using the following Ansible ad-hoc command:
ansible host1.example.com -a "/bin/echo hi"
The output is like this:
host1.example.com | CHANGED | rc=0 >>
hi
As you can see, the connection was successful, with amber color text and a CHANGED status, and a “hi” message is printed onscreen.
Ansible facts
You can list all the Ansible facts for one host using the following Ansible ad-hoc command:
ansible -m setup host1.example.com
The output is something similar to this:
ansible -m setup host1.example.com
"ansible_all_ipv4_addresses": [
"REDACTED"
],
"ansible_all_ipv6_addresses": [
"REDACTED"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "11/28/2013",
"ansible_bios_version": "4.1.5",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-3.10.0-862.14.4.el7.x86_64",
"console": "ttyS0,115200",
"no_timer_check": true,
"nofb": true,
"nomodeset": true,
"ro": true,
"root": "LABEL=cloudimg-rootfs",
"vga": "normal"
"ansible_date_time": {
"date": "2018-10-25",
"day": "25",
"epoch": "1540469324",
"hour": "12",
"iso8601": "2018-10-25T12:08:44Z",
"iso8601_basic": "20181025T120844109754",
"iso8601_basic_short": "20181025T120844",
"iso8601_micro": "2018-10-25T12:08:44.109968Z",
"minute": "08",
"month": "10",
"second": "44",
"time": "12:08:44",
"tz": "UTC",
"tz_offset": "+0000",
"weekday": "Thursday",
"weekday_number": "4",
"”weeknumber": "43",
"year": "2018"
},
"ansible_default_ipv4": {
"address": "REDACTED",
"alias": "eth0",
[...]
Recap
Now you know what an Ansible Playbook is and how to use it. You know how to use it based on your use case.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate