Host Description/Comment onto hosts via Ansible?

Hi all,
I’m wondering if it’s possible to include the Description field in “Hosts -> Content Hosts” as a variable?

I’d like to use that in a motd controlled by an Ansible module.

Is that even possible?
Is there some sort of host specific variables/facts that can be referenced in Ansible modules? (like description,memory, cpus etc)

Full disclosure: My customer site uses Satellite, my office uses Foreman/Katello. I’ve asked Redhat if it’s possible and they’ve said that its not available to any method. They themselves shown it was possible via the hammer command - only after I highlighted the field on one of their screenshots (sigh), so I’m beginning to doubt their knowledge.

Thanks all!

This should already be available as {{ foreman.comment }}, we render a lot of foreman data under hostvars in the inventory. You can see some of them at foreman/static_info.rb at 3347fa49d500964f0209122d8d36c920d1feafcc · theforeman/foreman · GitHub.

We’re working on adding the inventory preview on a new host detail page, for now the best list is on the host detail page, when you click the YAML button. Note that this is Puppet ENC, so only the structured under parameters is applicable.

Could you please privately send me the customer case / bz you opened? It may depend on the Satellite version, which is always slightly behind the upstream.

Hi Marek,
thanks for coming back to me!
The agent I’ve been working with has pointed me to the documentation. I’m now assuming that the {{ @host.comment }} variable can be used in provisioning templates.

Satellite version is 6.7.4, I’ll send you the case number shortly.

So let me explain in more details. In provisioning template we use ERB, meaning everything that’s wrapped in opening an closing tag is rendered through Ruby. @host refers to the host you render the template for. So the following <%= @host.comment %> would be replaced by the actual host comment/description in the resulting file, e.g. a kickstart. Methods that can be called on @host have to be whitelisted as safe, which sadly this method is not as of today, even though it is definitely safe. It’s easy to extend such list, I’ll open a PR for that soon. After that’s added

You could add

echo "<%= @host.comment %>" > /etc/motd

in your kickstart in the %post section to set the MOTD during the provisioning.

However if you prefer to use Ansible to configure your MOTD, you could use this data in your ansible role template too. Ansible uses jinja templating mechanism (python vs ruby) so the syntax is a bit different, though the principle is the same. When I tested this I figure we don’t expose host comment in there for some reason. So it’s not possible at this moment, I’ll open an RFE for that too. In the jinja template you’d then use {{ foreman.comment }}, but not until we start adding it to the invetory as a host variable.

In case you’d like to achieve the same with puppet, this should already be possible. The comment is exposed in the ENC if set. So in the puppet module, you could create a template for your motd file, with something like <%= @comment %>, note the puppet also use ERB templating so it’s similar to the first case. Here all parameters are exposed as variables denoted by @.

I hope that demystifies few things. I’ll add links to the RFEs here once I open them. Feel free to ask further if something is not clear :slight_smile:

I was acutally wrong about Ansible. It is already possible, the host must have the comment set though, otherwise it’s not set. Therefore you can define a task like this

- name: Update the message of the day
    src: motd.j2
    dest: /etc/motd
    owner: root
    group: root
    mode: 0644

and then define a motd.j2 tempalte like this

This system {{ ansible_facts.fqdn }} is managed by Foreman Ansible.

{% if 'comment' in foreman %}
The host description: {{ foreman.comment }}
{% endif %}

Note that there’s if condition for case the host does not have any comment set.

Similarly you can access any other information provided by Foreman including all host parameters (also inherited from host groups, OS, subnet, domain, org, loc, …).

Using the comment in the provisioning template is tracked under at Feature #32858: Allow using host comment in the safe mode - Foreman and the fix is proposed here Fixes #32858 - allow host comment in safe mode by ares · Pull Request #8617 · theforeman/foreman · GitHub

1 Like

Oh such as simple things and it gets even more complicated. Turns out when Katello plugin is installed, the comment is even allowed in the safe mode. But this functionality clearly belongs to the Foreman core, so we’re moving it there. What it means, if you have katello 3.14 or newer, you can even use the comment in the provisioning template.

Thanks Marek, greatly appreciated.

So just to clarify the availability:

Puppet: Yes, use the notation <%= @host.comment %>
Ansible: Yes, use the notation {{ foreman.comment }}
Provisioning: Not yet, will be in a future release (#32858)


I’ll look at getting something built up.

At the moment I have a post provisioning script that handles various aspects that haven’t made it into Satellite yet. One of them is the motd file (included information such as cpus count, memory, business unit, and a description). I can now at least build something like that into an Ansible playbook.

As things developed, I’d readjust this to:

Puppet: Yes, use the notation <%= @comment %>
Ansible: Yes, use the notation {{ foreman.comment }}
Provisioning: Yes, use the notation <%= @host.comment %> (if you have Katello 3.14 or newer)

If you want to includes cpus count, memory and other system facts, use puppet’s native facter or ansible facts to get them. For ansible, this is a good resource Using Variables — Ansible Documentation, you can read more about Ansible templates at Templating (Jinja2) — Ansible Documentation

Fantastic! Thanks Marek!

1 Like