Search code examples
ansibleyaml

Ansible issuing warning about localhost


I am running the following ansible playbook

- hosts: localhost
  connection: local
  vars_files:
    - vars/config_values.yaml

  gather_facts: no

  tasks:
    - name: Set correct project in gcloud config
      shell: "gcloud config set project {{ google_project_name }}"

Which yields the following warning:

[WARNING]: No inventory was parsed, only implicit localhost is available

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

Given that I am explicitly stating that it will be run against host: localhost, why is it complaining about no inventory being parsed and that the "provided host list is empty"?

How to remove these warnings? (without just suppressing them if possible)


Solution

  • These are just warnings telling you that:

    1. you did not provide any inventory file either with the -i option, in your ansible.cfg or by default in /etc/ansible/hosts (and hence none was parsed and it is empty)
    2. your inventory is empty and hence only the implicit localhost is available (reminding you that it does not match the all group)

    additional notes

    hosts: localhost in your above playbook example is the target for your play. It could be an other host or a group (or a more complicated pattern). The targeted hosts must exist in the inventory to be managed. localhost always exists at least as the implicit local machine.

    You can read Ansible introduction to inventories for more information


    Although those two warning seem a bit redundant, they give different information (i.e. "no inventory at all" vs "inventory is simply empty"). As such they are governed by different config options


    For the records, the first feature as been added to Ansible by a pull request proposed by @larsk, a stackoverflow user


    So if you intend to run playbooks primarily targeted to localhost without giving any inventory to parse, you can silence both warnings as in the following one-liner (see links above for all options to set those vars)

    ANSIBLE_LOCALHOST_WARNING=False \
    ANSIBLE_INVENTORY_UNPARSED_WARNING=False \
    ansible-playbook your_localhost_playbook.yml
    

    If you want to make this permanent, you can add the following two lines to your .bashrc (or equivalent file for the shell you use):

    # Silence absent and/or empty Ansible inventory warnings
    export ANSIBLE_LOCALHOST_WARNING=False
    export ANSIBLE_INVENTORY_UNPARSED_WARNING=False
    

    Note that, as explained in the above links, these features can be turned off for individual projects using an ansible.cfg file at project root:

    [defaults]
    localhost_warning=False
    
    [inventory]
    inventory_unparsed_warning=False