6

I'm following an Ansible tutorial I got from Packt, I reached this part where I've created 3 Ubuntu containers (lxc) and got them up and running. I'm also able to login to each of them.

I've downloaded Ansible by doing: git clone ansible-git-url and then sourced it.

My working setup is as follows: /home/myuser/code in here I have 2 folders: ansible (the whole git repo) and ansible_course where I have 2 files: ansible.cfg and inventory.

inventory contains the following:

[allservers]
192.168.122.117 
192.168.122.146
192.168.122.14

[web]
192.168.122.146
192.168.122.14

[database]
192.168.122.117

And ansible.cfg contains:

[root@localhost ansible_course]# cat ansible.cfg
[defaults]
host_key_checking = False

Then from this path: /home/myuser/code/ansible_course I try to execute the following:

$ ansible 192.168.122.117 -m ping -u root

The guy from the tutorial does exactly like this, and he gets success response from the ping, but I get the following error messages:

[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: 192.168.122.117

In the tutorial, he never says that I need to do something special in order to give an inventory source, he just says that we need to create an inventory file with the IP addresses of the Linux containers that we have.

I mean, he doesn't say that we need to execute a command to set this up.

2
  • Are those HTML <br> tags in your ansible.cfg?
    – Jeff Schaller
    Jul 25, 2018 at 23:30
  • @JeffSchaller yeah, I think I erased them, sometimes i forgot that need to edit some text as code. Jul 26, 2018 at 4:28

2 Answers 2

11

You'll probably want to tell ansible where the hosts file is in ansible.cfg, e.g.

[defaults]
inventory=inventory

assuming inventory is actually your inventory file.

2
  • 1
    this was really usefull, as I said, the guy from the vids never mentioned that. No I was able to pass throght those 4 error messages, but now I am getting the following: '[root@localhost ansible_course]# ansible 192.168.122.117 -m ping -u root 192.168.122.117 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n", "unreachable": true }' Jul 26, 2018 at 4:37
  • you'll either need to setup public key authentication (a different question) or use -k so that Ansible prompts for a password to use for the SSH connection
    – thrig
    Jul 26, 2018 at 13:50
5

Background

With ansible you can either depend on the ansible.cfg file to designate the name of the inventory file or you can specify it manually like so:

explicitly told
$ ansible -i inventory -m ping -u root 192.168.122.117
implicitly told through ansible.cfg
$ ansible -m ping -u root 192.168.122.117

Explicit

For the method where you explicitly tell ansible which inventory file to use the usage shows this description for it:

From the ansible usage:

 -i INVENTORY, --inventory=INVENTORY
          specify inventory host path or comma separated host list.

Implicit

For the implicit method, you have to be a little bit more versed in Ansible to realize that it works like this. You can use ansible's verbose mode to see more of what it's doing by default:

$ ansible -vvv -m ping -u root box-101
...
...
config file = /Users/user1/somedir/ansible.cfg
...
...
Using /Users/user1/somedir/ansible.cfg as config file
Parsed /Users/user1/somedir/inventory inventory source with ini plugin
META: ran handlers
Using module file /Users/user1/projects/git_repos/ansible/lib/ansible/modules/system/ping.py
...
...
box-101 | SUCCESS => {
    "changed": false,
    "invocation": {
        "module_args": {
            "data": "pong"
        }
    },
    "ping": "pong"
}
...
...

In the above I'm pinging box-101. You can see these lines show which ansible.cfg file is getting used:

config file = /Users/user1/somedir/ansible.cfg
Using /Users/user1/ansible.cfg as config file

And through this ansible.cfg file ultimately which inventory:

Parsed /Users/user1/somedir/inventory inventory source with ini plugin

And it's these options that are directing ansible to the inventory file:

$ cat ansible.cfg
...
[defaults]
inventory      = inventory
...
1
  • thanks for all of this information, I will save it in my notes. I used the implicity way, I think it is easier. Jul 26, 2018 at 4:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .