Skip to content

Example 1

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- name: Example playbook
  hosts: all
  become: true
  tasks:
    - name: Set SSH message of the day (motd)
      copy:
        content: "Working on: {{ ansible_hostname }}\n"
        dest: /etc/motd
      notify: Set MOTD
  handlers:
    - name: Set MOTD
      debug:
        msg: The MOTD was set

Tip

By default Ansible matches the facts gathered by the setup module with the corresponding hosts. Looping over the hosts and referencing the gathered facts will yield host-specific values.

Execute the following command

ansible-playbook playbook.yml

What is the expected result of the command

Each host will have a different MOTD based on its inventory hostname.