Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible issuing warning about localhost

Tags:

yaml

ansible

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)

like image 672
pkaramol Avatar asked Jan 27 '20 20:01

pkaramol


3 Answers

This is just a warning telling you that you did not provide any inventory file (with -i), that the default one (usually /etc/ansible/hosts) could not be found and therefore your inventory is empty, only containing the implicit localhost.

hosts: localhost is the target for your play. It can be a host or a group (or a more complicated pattern). The targeted hosts must exist in the inventory to be managed. localhost always exists as the implicit local machine.

See https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

If your default inventory will stay empty and you basically only run playbooks targeted to localhost, you can suppress this warning by setting a config option in your environment or in ansible cfg (Thanks @larsk for the pull request adding this feature).

like image 149
Zeitounator Avatar answered Sep 17 '22 07:09

Zeitounator


As a non-expert. in Ansible, I suppressed these messages by adding localhost on the /etc/ansible/hosts. Then set hosts: localhost on the playbook file. You should be good and won't be seeing those warning messages.

Hosts file

[ec2-user@ip-192-168-1-38 ~]$ sudo head -20 /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses

#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.
localhost
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]

Playbook file

[ec2-user@ip-192-168-1-38  ~]$ cat msteams.yml
  - name: MSTeams    
    hosts: localhost
    connection: local
    gather_facts: false
    tasks:
    - name: MSTeams Sending
      uri:
        url: https://outlook.office.com/webhook/asdasda-asdasd-40sdfd-btryrty2-posadjfm,sdnfoersmdnrfoasf,mvmnmtrny40956839523842/IncomingWebhook/102938091283kdjoasdyo214/vnmlasdhfaosldnaehywe
        method: POST
        body: "{\"text\": \"**Header:** {{Header}}\n\n **Next Line Message1:** {{MESSAGE1}}\n\n **Next Line Message2:** '{{ MESSAGE2 }}'\", \"themeColor\": \"{{COLOR}}\"}"
        body_format: json
        headers:
          Content-Type: "application/json"

Command Result

[ec2-user@ip-192-168-1-38 ~]$ ansible-playbook msteams.yml -e "Header=TEST" -e "MESSAGE1=Message_without_space" -e ' MESSAGE2="Message with space" ' -e "COLOR=##FF0000"

PLAY [MSTeams] ***********************************************************************************************************************************************************************************************

TASK [MSTeams Sending] ************************************************************************************************************************************************************************************
ok: [localhost]


PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost                 : ok=1    changed=0    unreachable=0    failed=0
like image 22
Lagot Avatar answered Sep 20 '22 07:09

Lagot


As of ansible 2.6 you can also run ANSIBLE_LOCALHOST_WARNING=false ansible-playbook /path/to/your/playbook.yaml or set "localhost_warning=false" in your ansible.cfg file

https://docs.ansible.com/ansible/latest/reference_appendices/config.html#localhost-warning

like image 28
Bruc3 Avatar answered Sep 18 '22 07:09

Bruc3