13

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?

chicks
  • 3,639
  • 10
  • 26
  • 36
grant tailor
  • 495
  • 2
  • 6
  • 13

1 Answers1

22

You have to use the template-module for this.

Here's an example task:

- name: Create motd
  template:
    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.

Luminance
  • 103
  • 3
rndmh3ro
  • 346
  • 2
  • 4
  • How do i "gather facts"?..thanks – grant tailor Feb 16 '15 at 14:55
  • works thanks..but what do you mean by "gather facts"? – grant tailor Feb 16 '15 at 15:23
  • I added two links to the documentation for gathering facts. – rndmh3ro Feb 17 '15 at 06:40
  • what about ip address? can't seem to find that either? how do i grep the ip address? `ansible_ip` ? – grant tailor Feb 17 '15 at 19:12
  • 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 – grant tailor Feb 17 '15 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](http://docs.ansible.com/playbooks_variables.html#accessing-complex-variable-data) that shows two different ways on how to get the address of a host! – rndmh3ro Feb 18 '15 at 19:21