I wrote a role to edit the motd when user log into the machine, but I want to personalize the motd to print the hostname of the machine

What variable do I use? or how do I do this? template? how? I used the copy module for the motd file

So for example I want to be able to say "welcome to $hostname" so how do I parse this hostname using ansible?

1 Answer 1


You have to use the template-module for this.

Here's an example task:

- name: Create motd
    src: "motd.j2"
    dest: "/etc/motd"

The file motd.j2 (placed in the templates-subdirectory of your role) could then look like this:

Welcome to host {{ansible_hostname}}!

{{ansible_hostname}} will then be replaced with the hostname.

Be sure to "gather facts" in your role, or else the variable will be empty.

  • 1
    How do i "gather facts"?..thanks Feb 16, 2015 at 14:55
  • works thanks..but what do you mean by "gather facts"? Feb 16, 2015 at 15:23
  • I added two links to the documentation for gathering facts.
    – rndmh3ro
    Feb 17, 2015 at 6:40
  • 2
    ansible_eth0.ipv4.address figured it out after some serious searching..which sucks...ansible should know there are very important variables that should be easier to find and be mentioned clearly on the doc site Feb 17, 2015 at 21:14
  • 1
    If you search for "ip address" in the ansible docs, you can easily find it in the first link. There's even a whole paragraph that shows two different ways on how to get the address of a host!
    – rndmh3ro
    Feb 18, 2015 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .