Hi @KodeKloud team

I have one query regarding ansible-playbook command, I kept my inventory file and YAML playbook in same directory , when i run

ansible-playbook playbook.yml --> I am getting message “No matching hosts” .
But when i explicitly mention a dynamic inventory command like

ansible-playbook -i inventory playbook.yml --> then it successfully executes the desired action, now my query is :
what is the difference between >> ansible-playbook playbook.yml & >> ansible-playbook -i inventory playbook.yml while i have my inventory file and playbook.yml file under same directory ?

Hi, Yaml file is set of instruction to run where as inventory file is list of host where you wanna run those instruction. hope it help you.

All the best

1 Like

As a best practice, Add the hosts in a separate inventory file.

1 Like

@Aashiqahamed, When you run ansible-playbook Ansible looks in several places to find the inventory. It looks in these places, in this order, until it finds an inventory reference:

  1. Reference in the CLI command:

    $ansible-playbook -i [inventory_filename] playbook.yml

  2. Reference in the environment variable:

    export ANSIBLE_INVENTORY=/home/myuser/ansible/my_inventory.ini

  3. Reference in ansible.cfg in the current working directory (where the playbook is executed):

    $ grep '$inventory ' ${PWD}/ansible.cfg
    inventory = /etc/ansible/inventory/my_inventory.txt
  4. Reference in .ansible.cfg (note the leading .) in the current user’s home directory:

    $ grep '$inventory ' ~/.ansible.cfg
    inventory = /home/myuser/ansible/
  5. Reference in ansible.cfg in /etc/ansible/ansible.cfg:

    $ grep '$inventory ' /etc/ansible/ansible.cfg
    inventory = /etc/ansible/inventory/inv*.txt
  6. Inventory actually defined in the default inventory file: /etc/ansible/hosts.

Notes and References:

  1. Any of these references can be to a directory or can use wildcards to collect multiple inventory files in the same directory.
  2. INI-style inventory files do not need file extensions.
  3. Inventory can be formatted in INI, YAML, JSON, and more (depending on plugins).
  4. The ansible-inventory command is really handy to get an idea of what Ansible thinks is the inventory. You can also use this command to print out the inventory in different formats.
  7. Inventory can get really complex, but understanding the these basics is important.
1 Like

Hi @tabernarious Thank you for this info