Chapter-2: How to write a simple YAML Playbook to run on Localhost, in 2 easy steps

'Playbook' is Ansible's instruction manual. It has the list of tasks to be executed on the target(s). It is written in YAML.

'Inventory' is a list of hosts that are target computers where the tasks are run.

Objective: In this chapter we will write a simple playbook to run on localhost.

Prerequisites:
1. You have set up the Ansible Control Machine. Refer Chapter-1 otherwise.

Step1: Create a new YAML file as shown in the screenshot. Name it conveniently as local_playbook.yml. I used vi editor.
           
NOTE: Carefully follow the indentation so the YAML parser doesn't complain.
            We can discuss YAML syntax and indentation requirements later.


  • What we wrote above is a play.
  • We are about to run it on localhost.
  • We are executing two tasks in this play; one executes native shell command to fetch current date. The other executes another native shell command to fetch hostname.

Step2: From the console run the following command. If your playbook is not in the current directory give absolute path.

ansible-playbook  -v  local_playbook.yml




Notice the two warnings in pink. First one complains about a missing inventory file parameter. Second says it cannot find non local targets and it is running on localhost.

Both task outputs are listed in detail. The play recap is at the end.

Your result for each task is what you get for stdout. Pipe this output to a file. Parse it and use it.


Comments

Popular posts from this blog

Chapter3: How to run an Ansible Playbook on Linux target (s); in 5 easy steps

Chapter1: How to deploy Ansible on a Linux instance, in 3 easy steps