DebugFactor.com | Forum

How to find the FQDN, hostname, shortname and IP of linux and windows server using Ansible

Is there a way to find the hostname of the remote server using ansible.
Also, please let me know how to set the hostname to another name or change the hostname of the remote server using Ansible.

Please advice.

You can use the below ones to get these details. Keep in mind that gather_facts: true should be set.

ansible_fqdn
ansible_hostname
ansible_default_ipv4.address

If gather_facts: false then you can use as below. These values are taken from the inventory, unlike the above ones where Ansible finds the values.

inventory_hostname
inventory_hostname_short

To change the hostname of a server , you can use as below.

  - name: Set hostname
    hostname:
     name: "newhostname"

or
  - name: Set hostname
    hostname:
     name: {{ inventory_hostname }}