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: centos1
  become: true
  tasks:
    - name: Create multiple users
      user:
        name: "{{ item.key }}"
        comment: "{{ item.value.comment }}"
      with_dict:
        john:
          comment: This is John
        mary:
          comment: This is Mary

Tip

The with_dict loop is particularly useful and frequently used in Ansible Playbooks.

Execute the following command

ansible-playbook playbook.yml