# Ansible syntax for outputing information

**URL:** <https://forum.ansible.com/t/ansible-syntax-for-outputing-information/10073>\
**Category:** Get Help\
**Created:** [October 8, 2024, 9:45pm UTC](https://forum.ansible.com/t/ansible-syntax-for-outputing-information/10073 "2024-10-08T21:45:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Budman](https://avatars.discourse-cdn.com/v4/letter/b/97f17d/32.png) [@Budman](https://forum.ansible.com/u/Budman)\
**Post date:** [October 8, 2024, 9:45pm UTC](https://forum.ansible.com/t/ansible-syntax-for-outputing-information/10073/1 "2024-10-08T21:45:49Z")

</div>

Hello,

I have ansible code that works quite well - logs on to the server I want and so forth.

The way it is designed, system asks for your username and password, then logs into the host.  
Using ansible code, I would like to gather information after I login and then output the results.

The information that I would like to gather:  
hostname  
ip address

So I would like output that says:

\<\>–\> \<\>

I would only like this information to be outputted to screen. Nothing else.

I am close as I have the following:

* * *

- name: “Output test”  
shell: |  
ip=$(ifconfig)  
hostn=$(hostname)  
echo “$hostn → $ip”  
register: out
- debug:  
msg: hostn ip

* * *

The output is not exactly in the format I am looking for. It is messy

I thought maybe msg parameter might work, but have not been able to figure out appropriate syntax

Can anyone guide me so I can just output

hostname —\> IP address

Thanks!

Mark

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 9, 2024, 12:32am UTC](https://forum.ansible.com/t/ansible-syntax-for-outputing-information/10073/2 "2024-10-09T00:32:06Z")

</div>

Welcome to the Ansible forum, @Budman .

Is it messy because `$ip` has extra content, or because the output of an Ansible job is an Ansible job log? I’m not sure what benefit you get by involving Ansible in the process. It seems like it would be simpler to say

```sh
$ ssh yourhost 'printf "%s --> %s\n" "$(hostname)" "$(ifconfig)"'

```

If the point is to make the output of Ansible look like something other than an Ansible job log, then I think a re-evaluation of the technique is in order.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [October 9, 2024, 1:10pm UTC](https://forum.ansible.com/t/ansible-syntax-for-outputing-information/10073/3 "2024-10-09T13:10:08Z")

</div>

You might want to look at a callback plugin, i’m not sure an existing one matches exactly what you want, but you can probably modify them to do so.

[https://docs.ansible.com/ansible/latest/plugins/callback.html](https://docs.ansible.com/ansible/latest/plugins/callback.html)  
[https://docs.ansible.com/ansible/latest/collections/index\_callback.html](https://docs.ansible.com/ansible/latest/collections/index_callback.html)

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [October 9, 2024, 2:36pm UTC](https://forum.ansible.com/t/ansible-syntax-for-outputing-information/10073/4 "2024-10-09T14:36:03Z")

</div>

It is unclear to me why you are using shell at all? Isn’t this information available as an Ansible Fact, so you should be able to use those to echo (debug-msg) however you want?

[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_vars\_facts.html](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html)

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [October 9, 2024, 2:50pm UTC](https://forum.ansible.com/t/ansible-syntax-for-outputing-information/10073/5 "2024-10-09T14:50:21Z")

</div>

As @jrglynn2 suggested, take advantage of the facts that Ansible gathers by default.

The following will display the short hostname, fully qualified domain name, as well as the default ipv4 and ipv6 addresses. It’s possible for IPv4/IPv6 to be undefined, so this snippet sets a default message instead of failing the debug task whenever that’s the case.

```yaml
---
- hosts: all
  gather_facts: true
  tasks:
    - name: show desired facts
      debug:
        msg:
          - "{{ ansible_hostname }}"
          - "{{ ansible_fqdn }}"
          - "{{ ansible_default_ipv4.address | default('No IPv4 address found') }}"
          - "{{ ansible_default_ipv6.address | default('No IPv6 address found') }}"

```

Based on what it seems you want in your original post, `msg: "{{ ansible_fqdn | default(ansible_hostname) }} --> {{ ansible_default_ipv4.address }}"` is probably what you’re looking for.
