<p>It is assumed that you have a proper and valid inventory defined. Then, according <a href="https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables" rel="nofollow noreferrer">Information about Ansible: magic variables</a></p>
<blockquote>
<p><em>You can use the magic variable <code>inventory_hostname</code>, the name of the host as configured in your inventory, as an alternative to <code>ansible_hostname</code> when fact-gathering is disabled. If you have a long FQDN, you can use <code>inventory_hostname_short</code>, which contains the part up to the first period, without the rest of the domain.</em></p>
</blockquote>
<p>If <a href="https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html" rel="nofollow noreferrer">facts are gathered</a> you can use additionally <code>ansible_hostname</code> or <code>ansible_facts['nodename']</code>.</p>
<p>A minimal example playbook</p>
<pre class="lang-yaml prettyprint-override"><code>---
- hosts: localhost
  become: false
  gather_facts: true

  tasks:

  - name: Show hostname
    debug:
      msg: &quot;Host: {{ ansible_hostname }} FQDN: {{ ansible_nodename }}&quot;
</code></pre>
<p>will result into an output of</p>
<pre class="lang-yaml prettyprint-override"><code>TASK [Show hostname] *********************
ok: [localhost] =&gt;
  msg: 'Host: test FQDN: test.example.com'
</code></pre>
<hr />
<p>You may then have a look into the following examples</p>
<pre class="lang-yaml prettyprint-override"><code>  - name: Split nodename
    debug:
      msg: &quot;{{ ansible_nodename.split('.')[0] }}&quot;

  - name: Split nodename
    debug:
      msg: &quot;{{ ansible_nodename | split('.') | first }}&quot;
</code></pre>
<p>resulting both into an output of</p>
<pre class="lang-yaml prettyprint-override"><code>TASK [Split nodename] ******
ok: [localhost] =&gt;
  msg: test
</code></pre>
<p><strong>Further Documentation</strong></p>
<ul>
<li><a href="https://docs.ansible.com/ansible/devel/collections/ansible/builtin/split_filter.html" rel="nofollow noreferrer"><code>split</code> filter – split a string into a list</a></li>
</ul>
<p><strong>Similar Q&amp;A</strong></p>
<ul>
<li><a href="https://stackoverflow.com/a/69433646/6771046">Ansible string split</a></li>
<li><a href="https://stackoverflow.com/a/55194553/6771046">How to split value in Ansible with delimiter?</a></li>
<li><a href="https://stackoverflow.com/questions/45908067/">What's the difference between <code>inventory_hostname</code> and <code>ansible_hostname</code>?</a></li>
</ul>

CC BY-SA 4.0
2022-12-28T07:56:08.310Z
2023-07-28T06:46:05.467Z
2023-07-28T06:46:05.467Z
U880D
<p>How to get value from <code>/etc/hostname</code> ignoring domain name. For example, <code>/etc/hostname</code> says <code>&quot;client.test.dom&quot;</code> and I need to get <code>&quot;client&quot;</code>.</p>
<p>I have tried <code>&quot;{{ ansible_nodename | replace('.rail.dom','') }}&quot;</code> but I get syntax problem and can't figure out how to solve it.</p>

CC BY-SA 4.0
2022-12-28T08:01:43.420Z
Regarding "_I get syntax problem and can't figure out how to solve it._" can you describe that in more detail and provide more information?
U880D
<ansible>
Ansible: How to get hostname without domain name?
Виктор Гринько
# Ansible: How to get hostname without domain name?

(Asked by: username_0 on 2022-12-26)

How to get value from `/etc/hostname` ignoring domain name. For example, `/etc/hostname` says `"client.test.dom"` and I need to get `"client"`.

I have tried `"{{ ansible_nodename | replace('.rail.dom','') }}"` but I get syntax problem and can't figure out how to solve it.

**Tags:** ansible

> **Comments:**
> **username_1**: Regarding "_I get syntax problem and can't figure out how to solve it._" can you describe that in more detail and provide more information?  

---

## 1 answers

### Answer by username_1 on 2022-12-28. Score: 7

It is assumed that you have a proper and valid inventory defined. Then, according Information about Ansible: magic variables

You can use the magic variable inventory_hostname, the name of the host as configured in your inventory, as an alternative to ansible_hostname when fact-gathering is disabled. If you have a long FQDN, you can use inventory_hostname_short, which contains the part up to the first period, without the rest of the domain.

If facts are gathered you can use additionally ansible_hostname or ansible_facts['nodename'].
A minimal example playbook
---
- hosts: localhost
  become: false
  gather_facts: true

  tasks:

  - name: Show hostname
    debug:
      msg: "Host: {{ ansible_hostname }} FQDN: {{ ansible_nodename }}"

will result into an output of
TASK [Show hostname] *********************
ok: [localhost] =>
  msg: 'Host: test FQDN: test.example.com'


You may then have a look into the following examples
  - name: Split nodename
    debug:
      msg: "{{ ansible_nodename.split('.')[0] }}"

  - name: Split nodename
    debug:
      msg: "{{ ansible_nodename | split('.') | first }}"

resulting both into an output of
TASK [Split nodename] ******
ok: [localhost] =>
  msg: test

Further Documentation

split filter – split a string into a list

Similar Q&A

Ansible string split
How to split value in Ansible with delimiter?
What's the difference between inventory_hostname and ansible_hostname?


---

