6

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 $ 

1 Answer 1

6

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.

1
  • Adding some context about why the test ansible localhost -m ping -u root worked ok: if you explicitly specify localhost 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.
    – hvindin
    Commented Sep 18, 2020 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.