Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the host name of the current machine as defined in the Ansible hosts file?

Tags:

ansible

People also ask

How do I find my Ansible hostname?

Simple ansible playbook which reads the host name in the local machine and prints the result. echo $HOSTNAME shell command returns the host nname of the current machine. result is the output of Get hostnmae task and output is in the JSON format.

Which Ansible module is used to fetch the hostname of a device?

hostname module – Manage hostname. This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name hostname even without specifying the collections: keyword.


The necessary variable is inventory_hostname.

- name: Install this only for local dev machine
  pip: name=pyramid
  when: inventory_hostname == "local"

It is somewhat hidden in the documentation at the bottom of this section.


You can limit the scope of a playbook by changing the hosts header in its plays without relying on your special host label ‘local’ in your inventory. Localhost does not need a special line in inventories.

- name: run on all except local
  hosts: all:!local