16

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!

TomTom
  • 318
  • 1
  • 3
  • 15
  • _I am doing an online course_. Is there a chapter called `inventory` in that course? – Konstantin Suvorov Jun 16 '17 at 14:50
  • Yes, there is, but in this very example I am running a one-off Ansible command for a specific host and as you can see I provide this host's IP address directly in the command. In this very case, according to my understanding, the inventory file is irrelevant. – TomTom Jun 16 '17 at 14:54
  • 1
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jun 17 '17 at 11:13
  • I think I am following the same tutorial, is it from Packt, and the instructor name is Dave? – grimmjow_sms Jul 25 '18 at 21:04
  • Yup, that was the one. – TomTom Jul 27 '18 at 12:57

1 Answers1

27

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.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • Thanks for your reply. It is a pity I can't paste in here a screenshot. I would like to show you, that the guy who runs that course (tutoriaLinux YT channel) does exactly the thing I was asking about. He enters this: ansible 10.0.3.248 -m ping -u ubuntu and gets the Ansible ping response. – TomTom Jun 16 '17 at 15:07
  • for this exact command to work, you should have inventory defined. – Konstantin Suvorov Jun 16 '17 at 15:17
  • Also, in the host pattern section of the Ansible documentation, it says: It is also possible to address a specific host or set of hosts by name: one.example.com one.example.com:two.example.com 192.0.2.50 192.0.2.* Which, with all due respect, seems to be in the contrary to what you wrote. – TomTom Jun 16 '17 at 15:18
  • If you don't believe me, you may want to believe Ansible: it gave you 4 warning messages about wrong inventory setup. It is possible to address a specific host **from inventory**, not arbitrary host by ip address. Please browse through official [docs](http://docs.ansible.com/ansible/intro_inventory.html). – Konstantin Suvorov Jun 16 '17 at 15:27
  • Thanks for your input. What I learned is that if you set the inventory value in the ansible.cfg file, then you do not need to use -i flag, as ansible looks by default to the inventory file specified in ansible.cfg. So I was able to eventually see what I want: `tomasz@tomasz-VirtualBox:~/ansible_code$ ansible 10.0.3.48 -m ping 10.0.3.48 | SUCCESS => { "changed": false, "failed": false, "ping": "pong" }` – TomTom Jun 19 '17 at 14:41
  • "omasz@tomasz-VirtualBox:~/ansible_code$ ansible 10.0.3.48 -m ping 10.0.3.48 | SUCCESS => { "changed": false, "failed": false, "ping": "pong" }" Hi Tomas, I already have my inventory declared in the ansible.cfg file, but each time I run the ping command, I get a failed message. How did you setup the ssh-rsa? as a normal user or sudo? – grimmjow_sms Jul 27 '18 at 17:21