How to get host name in ansible?

How to get host name in ansible?

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.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Get hostname
      shell: echo $HOSTNAME
      register: result

   - name: Print the var
     debug:
       var: result

Output:

$ ansible-playbook get-host-name.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ************************************************************************************************************************************************************

TASK [Get hostname] *********************************************************************************************************************************************************
changed: [localhost]

TASK [Print the var] ********************************************************************************************************************************************************
ok: [localhost] => {
	"result": {
    	"changed": true,
    	"cmd": "echo $HOSTNAME",
    	"delta": "0:00:00.002578",
    	"end": "2020-06-10 15:53:45.307804",
    	"failed": false,
    	"rc": 0,
    	"start": "2020-06-10 15:53:45.305226",
    	"stderr": "",
    	"stderr_lines": [],
    	"stdout": "localhost.localdomain",
    	"stdout_lines": [
        	"localhost.localdomain"
    	]
	}
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=2	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0 

result is the output of Get hostnmae task and output is in the JSON format.

If ee need only output of only Get hostname task, we can use only result.stdout variable.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Get hostname
      shell: echo $HOSTNAME
      register: result

   - name: Print the var
     debug:
       var: result.stdout

Output:

$ ansible-playbook get-host-name.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ************************************************************************************************************************************************************

TASK [Get hostname] *********************************************************************************************************************************************************
changed: [localhost]

TASK [Print the var] ********************************************************************************************************************************************************
ok: [localhost] => {
	"result.stdout": "localhost.localdomain"
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=2	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0 




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^