Learning Ansible 2.7(Third Edition)
上QQ阅读APP看书,第一时间看更新

Adding a customized MOTD

To add the MOTD, we will need a template that will be the same for all servers, and a task to use the template.

I find it very useful to add a MOTD to every server. It's even more useful if you use Ansible, because you can use it to warn your users that changes to the system could be overwritten by Ansible. My usual template is called motd, and has this content:

                This system is managed by Ansible 
  Any change done on this system could be overwritten by Ansible 
  
OS: {{ ansible_distribution }} {{ ansible_distribution_version }} 
Hostname: {{ inventory_hostname }} 
eth0 address: {{ ansible_eth0.ipv4.address }} 
 
            All connections are monitored and recorded 
     Disconnect IMMEDIATELY if you are not an authorized user

This is a jinja2 template, and it allows us to use every variable set in the playbooks. This also allows us to use complex syntax for conditionals and cycles that we will see later in this chapter. To populate a file from a template in Ansible, we will need to use the following:

- name: Ensure the MOTD file is present and updated 
  template: 
    src: motd 
    dest: /etc/motd 
    owner: root 
    group: root 
    mode: 0644 
  become: True 

The template module allows us to specify a local file (src) that will be interpreted by jinja2, and the output of this operation will be saved on the remote machine in a specific path (dest), will be owned by a specific user (owner) and group (group), and will have a specific access mode (mode).