ansible error: skipping: no hosts matched
8,901
The answer is in the warning you get:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
When you use hosts: all
in your playbook, localhost
is not matched.
If you want to run the playbook on localhost, you can do one of the following:
- Change your playbook to
hosts: localhost
. - Explicitly provide an inventory file that has
localhost
.
You can create an inventory file - inventory.yml
with the contents:
all:
hosts:
localhost
and then pass this inventory file to ansible explicitly like this:
$ ansible-playbook --connection=local -i inventory.yml playbook.yml
You can find more information about inventory in the Ansible documentation.
Author by
Nicholas Saunders
Updated on September 18, 2022Comments
-
Nicholas Saunders over 1 year
I've tried a few variations on running
ansible
locally as a test case:nicholas $ nicholas $ ls ansible.cfg ansible.cfg.orig first_playbook.yml inventory.txt playbook.yml nicholas $ nicholas $ cat ansible.cfg [defaults] transport = local nicholas $ nicholas $ cat playbook.yml --- - name: Network Getting Started First Playbook connection: network_cli gather_facts: false hosts: all tasks: - name: Get config for VyOS devices vyos_facts: gather_subset: all - name: Display the config debug: msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}" nicholas $ nicholas $ ansible --version ansible 2.9.6 config file = /home/nicholas/ansible/ansible.cfg configured module search path = ['/home/nicholas/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0] nicholas $ nicholas $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04 LTS Release: 20.04 Codename: focal nicholas $ nicholas $ ansible-playbook --connection=local playbook.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [Network Getting Started First Playbook] ************************************************************************************** skipping: no hosts matched PLAY RECAP ************************************************************************************************************************* nicholas $
perhaps I need an additional configuration for the localhost loopback?
Although I can ping localhost as:
nicholas $ nicholas $ ansible localhost -m ping -u root 127.0.0.1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } nicholas $
-
MrEvil over 3 yearsAdding some context about why the test
ansible localhost -m ping -u root
worked ok: if you explicitly specifylocalhost
then that will match the implicit localhost in the default hosts file. You also aren't using--connection=local
there. So this answer is the one you want, create a hosts file, it's almost always necessary.