AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

ansible.builtin.command Module: Run Ad-Hoc Commands & Tasks (Guide) — Video Tutorial

How to run commands with Ansible command module (ansible.builtin.command). Execute ad-hoc tasks, manage remote systems, capture output with register.

Watch Video

Watch "ansible.builtin.command Module: Run Ad-Hoc Commands & Tasks (Guide)" on YouTube

What You'll Learn

Full Tutorial Content

What does the ansible command? I’m going to show you a live Playbook. 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 - [Introduction to ad hoc commands](https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html) Playbook 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: ```bash ansible -m ping host1.example.com ``` The output is like this: ```bash 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: ```bash ansible host1.example.com -a "/bin/echo hi" ``` The output is like this: ```bash 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: ```bash ansible -m setup host1.example.com ``` The output is something similar to this: ```bash 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

About This Tutorial

Read the full written article: ansible.builtin.command Module: Run Ad-Hoc Commands & Tasks (Guide)

Topics Covered

Related Video Tutorials