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: "Host system: {{ item }}\n"
        dest: /etc/motd
      notify: Set MOTD
      with_items:
        - CentOS
        - Ubuntu
      when: ansible_distribution == item
  handlers:
    - name: Set MOTD
      debug:
        msg: The MOTD was set

Tip

You can use the with_<lookup> loop syntax to iterate over different data types and perform more complex operations.

Execute the following command

ansible-playbook playbook.yml