pangpang
pangpang

Reputation: 8821

Ansible specifying inventory file doesn't work

I am trying to specify inventory file in Ansible.

The help command output:

  -i INVENTORY, --inventory-file=INVENTORY
                        specify inventory host file
                        (default=/usr/local/etc/ansible/hosts)

I tried to do like this:

 ansible -i /Users/liu/personal/test_ansible/hosts

but it doesn't work and instead it outputs the help content once again:

➜  test_ansible  ansible -i /Users/liu/personal/test_ansible/hosts
Usage: ansible <host-pattern> [options]

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
  --ask-become-pass     ask for privilege escalation password
  -k, --ask-pass        ask for SSH password
  --ask-su-pass         ask for su password (deprecated, use become)
  -K, --ask-sudo-pass   ask for sudo password (deprecated, use become)
  --ask-vault-pass      ask for vault password
  -B SECONDS, --background=SECONDS
                        run asynchronously, failing after X seconds
                        (default=N/A)
  .......

What am I missing here?

Upvotes: 2

Views: 10059

Answers (2)

ydaetskcoR
ydaetskcoR

Reputation: 56877

When you use the ansible command it will run ad-hoc Ansible modules rather than the more typical Ansible playbooks (which is ran by the ansible-playbook executable instead).

The ansible executable has a requirement of a "host pattern" which will match a group of remote nodes defined in the inventory.

So if we supplied an inventory file (named inventory.ini for this example) that looked like this:

[web]
web-1.example.org
web-2.example.org

[app]
app-1.example.org
app-2.example.org
app-3.example.org

[database:children]
database-master
database-slave

[database-master]
database-master.example.org

[database-child]
database-slave1.example.org
database-slave2.example.org

We could target just the web nodes by using ansible web -i /path/to/inventory.ini -m ping to get Ansible to use the ping module against the web-1.example.org and web-2.example.org.

Alternatively we could target all of the database nodes including the master and the 2 slaves by using ansible database -i /path/to/inventory.ini -m ping.

And finally, we can also target all of the servers in the inventory by using the "magic" all group that covers all of the groups in the inventory file by using ansible all -i /path/to/inventory.ini -m ping.

Upvotes: 3

pangpang
pangpang

Reputation: 8821

I found the solution:

export ANSIBLE_INVENTORY=/Users/liu/personal/test_ansible/hosts

then will be ok!

Upvotes: 1

Related Questions