Bootstrap FreeKB - Ansible - Getting Started with playbooks
Ansible - Getting Started with playbooks

Updated:   |  Ansible articles

The ansible-playbook command is used to perform the tasks in a playbook. A playbook is a YAML file, such as foo.yml or bar.yaml. The YAML file will contain a series of tasks. For example, the httpd.yml playbook would probably consists of tasks to download the HTTPD installation files, to install HTTPD, and to then start and enable the HTTPD web server.

Since a playbook is a YAML file, you'll simply use the touch command to create the YAML file. The file can use the .yml or .yaml file extension - both a valid, and this just comes down to personal preference.

touch playbook.yml

 

Typically, a YAML file begins with three dashs (---) to indicate the beginning of the file and ends with three dots (...) to indicate the end of the file. These markers are completely optional.

---
# Hello World
...

 

Quiet often, the very first line in a playbook is - hosts: all. The leading dash (-) identifies a list, followed by a key (hosts) and value (all). The syntax here is very strict. Any alterations to this syntax will return an error when attempting to invoke the playbook.

---
- hosts: all
...

 

Expanding upon this example, the playbook may next contain the tasks key. In this example, the debug module is used to output "Hello World". At this point, we've a functional playbook.

---
- hosts: all
  tasks:
    - name: "Greetings"
      debug:
        msg: "Hello World"
...

 

The ansible-playbook command could then be used to run this playbook.

ansible-playbook playbook.yml

 

Assuming you've defined your inventory, which is the target systems that the playbook will be run on (see Getting Started with static inventory or Getting Started with dynamic inventory), something like this should be returned.

Check out this article if you want to know what Gathering Facts is all about.

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]

TASK [Greeting]
ok: [server1.example.com] => {
    "msg": "Hello World"
}

PLAY RECAP
server1.example.com : ok=1  changed=0  unreacable=0  failed=0

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2f0ece in the box below so that we can be sure you are a human.