Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible ad-hoc command with direct host specified - no hosts matched

I am running a 16.04 Ubuntu desktop machine using VirtualBox. This VM has Ansible 2.4.0 installed. I am trying to run an ad-hoc ansible command just to prove it works (I am doing an online course). To simulate a small server farm, I use lxc (linux containters) and have three of them running:

root@tomasz-VirtualBox:/home/tomasz/ansible# lxc-ls --fancy
NAME STATE   AUTOSTART GROUPS IPV4       IPV6 
db1  RUNNING 0         -      10.0.3.248 -    
web1 RUNNING 0         -      10.0.3.110 -    
web2 RUNNING 0         -      10.0.3.226 -

I can SSH to any of these servers, however when I try to run a one-off ansible command, for example:

root@tomasz-VirtualBox:/home/tomasz/ansible# ansible 10.0.3.248 -m ping -u ubuntu

I get the following errors, that no inventory has been matched:

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available

 [WARNING]: Could not match supplied host pattern, ignoring: 10.0.3.248

 [WARNING]: No hosts matched, nothing to do

I am puzzled, to be honest, and as an Ansible novice, I have no idea how to move this forward. Seems such a simple issue, have not come across any similar thing here on stackoverflow. Many thanks for any hints!

like image 968
TomTom Avatar asked Jun 16 '17 14:06

TomTom


People also ask

How do I run an ad hoc command in Ansible?

Automation with Ansible Playbooks Ad hoc commands are commands which can be run individually to perform quick functions. These commands need not be performed later. For example, you have to reboot all your company servers. For this, you will run the Adhoc commands from '/usr/bin/ansible'.

Which command is used to add a user to a host using an Ansible ad hoc command?

Execute ansible ad hoc commands as different user If you have a requirement to connect to the managed node and execute command as a different user then we must use -u or --user along with ansible command.

How Ansible playbook is different from ad hoc commands?

An ansible ad-hoc command is a one-line command that lets you perform basic tasks efficiently without writing playbooks. An Ansible ad-hoc command uses ansible command-line tool to automate a single task on one or more managed nodes. Ad-hoc commands are quick and easy, but they are not reusable.

How do you pass inventory to Ansible adhoc command?

If you're using the custom inventory file, add the '-i' option followed the inventory file name. You will get the same result. Now if you want to run against single host on the inventory configuration, you can use the name of the host such as below. And the ad-hoc command will run on the 'provision' server only.


1 Answers

I provide this host's IP address directly in the command. In this very case, according to my understanding, the inventory file is irrelevant.

Wrong. You specify host pattern, which should match hosts in your inventory. Inventory is a must for Ansible.

There's an option to specify "inline" inventory. For your case:

ansible all -i '10.0.3.248,' -m ping -u ubuntu

in this example: host pattern is all, inventory is a list of a single host 10.0.3.248.
Note comma at the end – it is important, this way Ansible understand that it is inline inventory, and not path to file.

like image 156
Konstantin Suvorov Avatar answered Sep 28 '22 07:09

Konstantin Suvorov