Creating a playbook

Playbooks are automation blueprints, in YAML format, that Ansible uses to deploy and configure managed nodes.


A list of plays that define the order in which Ansible performs operations, from top to bottom, to achieve an overall goal.


An ordered list of tasks that maps to managed nodes in an inventory.


A list of one or more modules that defines the operations that Ansible performs.


A unit of code or binary that Ansible runs on managed nodes. Ansible modules are grouped in collections with a Fully Qualified Collection Name (FQCN) for each module.

In the previous section, you used the ansible command to ping hosts in your inventory. Now let’s create a playbook that pings your hosts and also prints a “Hello world” message.

Complete the following steps:

  1. Open a terminal window on your control node.

  2. Create a new playbook file named playbook.yaml in any directory and open it for editing.

  3. Add the following content to playbook.yaml:

    - name: My first play
      hosts: virtualmachines
       - name: Ping my hosts
       - name: Print message
           msg: Hello world
  4. Run your playbook.

    ansible-playbook -i inventory.yaml playbook.yaml

Ansible returns the following output:

PLAY [My first play] **********************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [vm01]
ok: [vm02]
ok: [vm03]

TASK [Ping my hosts] **********************************************************************
ok: [vm01]
ok: [vm02]
ok: [vm03]

TASK [Print message] **********************************************************************
ok: [vm01] => {
    "msg": "Hello world"
ok: [vm02] => {
    "msg": "Hello world"
ok: [vm03] => {
    "msg": "Hello world"

PLAY RECAP ********************************************************************************
vm01: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
vm02: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
vm03: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

In this output you can see:

  • The names that you give the play and each task. You should always use descriptive names that make it easy to verify and troubleshoot playbooks.

  • The Gather Facts task runs implicitly. By default Ansible gathers information about your inventory that it can use in the playbook.

  • The status of each task. Each task has a status of ok which means it ran successfully.

  • The play recap that summarizes results of all tasks in the playbook per host. In this example, there are three tasks so ok=3 indicates that each task ran successfully.

Congratulations! You have just created your first Ansible playbook.

See also

Ansible playbooks

Start building playbooks for real world scenarios.

Working with playbooks

Go into more detail with Ansible playbooks.

Ansible tips and tricks

Get tips and tricks for using playbooks.

Discovering variables: facts and magic variables

Learn more about the gather_facts keyword in playbooks.