PaddyInNZ
PaddyInNZ

Reputation: 13

Why is Ansible ignoring my inventory file?

I've just installed Ansible on Ubuntu 19.04. The environment variable, ANSIBLE_CONFIG is empty. I have a local ansible.cfg file pointing to a local inventory file (called hosts). The local inventory file (hosts) identifies two routers (R1 and R2). These devices, R1 and R2, are in my /etc/hosts file, and I can SSH to them. ansible --version shows my config file is /home/paddy/ansible.cfg. ansible --list-hosts all says there's no hosts file.

When I remove the local ansible.cfg file, and put the local inventory file (hosts) into /etc/ansible, and then run, I get:

$ansible --list-hosts all
    hosts (2):
      R1
      R2 

This is as expected. Unfortunately, when I use the local ansible.cfg file, it doesn't see the inventory file (/home/paddy/hosts)

  1. Environment Variable is not set:
$echo $ANSIBLE_CONFIG

    <nothing>
  1. Local ansible.cfg file (in /home/paddy) is as follows:
    [defaults]
    hostfile = /home/paddy/hosts
    host_key_checking = False
    timeout = 5
  1. Local inventory file (in /home/paddy called hosts):
    [routers]
    R1
    R2
  1. Files in /home/paddy:
$ls -l
    total 52
    -rw-r--r-- 1 paddy paddy   79 Aug 19 16:54 ansible.cfg
    -rw-r--r-- 1 paddy paddy   17 Aug 19 16:55 hosts
    <redacted directories>
  1. The output of ansible --version
$ansible --version
    ansible 2.8.4
      config file = /home/paddy/ansible.cfg
      configured module search path = [u'/home/paddy/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python2.7/dist-packages/ansible
      executable location = /usr/bin/ansible
      python version = 2.7.16 (default, Apr  6 2019, 01:42:57) [GCC 8.3.0]
  1. The output of ansible --list-hosts all.
$ansible --list-hosts all
    [WARNING]: provided hosts list is empty, only localhost is available. 
    Note that the implicit localhost does not match 'all'.

    hosts (0):

I expected to see:

$ansible --list-hosts all

    hosts (2):
       R1
       R2 

What I get is:

$ansible --list-hosts all

    [WARNING]: provided hosts list is empty, only localhost is available. 
    Note that the implicit localhost does not match 'all'.

    hosts (0):

Upvotes: 1

Views: 4572

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68294

The Ini Key is inventory

Wrong

[defaults]
hostfile = /home/paddy/hosts

Correct

[defaults]
inventory = /home/paddy/hosts

Note

It's possible to use environment variables in ansible.cfg. For example

[defaults]
inventory = $PWD/hosts
roles_path = $PWD/roles
library = $PWD/modules:/usr/share/ansible/plugins/modules
...

Upvotes: 3

Related Questions