Skip to content
Welcome
  • Home
  • about the author
The OpsCorner
The OpsCorner
  • Home
  • about the author

The OpsCorner

A documentation repository of loosely related topics

  • Home
  • about the author
Software / Technology

Ansible Playbook Basics

- October 22, 2019 | March 20, 2020 - Bryan McClellan

Basic on Playbooks

– Plays are a collection of key value pairs. Keys in the same pair should share the same indentation. Playbooks are YAML files with the .yml file extension.

Indentation is super important and the indention should be configured in vim. The indentation is based on two spaces.

– For better vim editing with yaml add the following to the ~/.vimrc file

autocmd FileType yaml setlocal ai ts=2 sw=2 et
set cursorcolumn

Details on the autocmd

  • #ai = auto indent
  • #ts = tab stop
  • #sw = Shift Width
  • #et = expansetab

– Check syntax before starting

ansible-playbook playbook.yml –syntax-check

– Dry run the playbook

ansible-playbook playbook.yml -C

-v for verbose.. Up to 4v's

ansible-playbook playbook.yml "-vvvv"

 – Starts with three dashes "—" and can end with three dots "…"

Sample playbook with name, hosts and tasks

  • #*****
  • ---
  • - name: Playbook description
  •   hosts: hosts
  •   tasks:
  •     - first
  •     - second
  •     - third
  • #*******

– Playbook tasks are ran in order

– Example playbook with task shown below

– "Print the output now" task details..

  • Task uses the command module
  • Registers the output of the module to the command_result variable
  • Uses this variable to check the result and determine if it has failed

The second two register and failed_when are optional. These are best practice when working with the command module especially. Since the command module itself does not consider the command that is passed failing you will want to check the output for something to verify that the command succeeded or failed. You can also do something similar with changed_when for command and shell.

– Basic Tasks..

yum, copy, service, command, shell, etc……

ansible-doc task for more information on tasks

– Playbooks can have multiple plays

  • - name
  •   hosts:
  •   tasks:
  •      -  task1_play1
  • - name
  •   hosts:
  •   tasks:
  •      - task1_play2

Post navigation

Running Ad-hoc Commands in Ansible Tower
vRealize Orchestrator – RestAPI Action to Input Form Field

Recent Posts

  • Dell OpenManage Enterprise API PowerShell Scripts September 18, 2020
  • Ansible Workshop from Anywhere August 31, 2020
  • VMware Webinar Series July 27, 2020
  • Using the default VMware-Privileged PodSecurityPolicies on TKG June 30, 2020
  • VM Template Updater with Ansible April 24, 2020

Tags

Active Directory Ansible Automation azure-devops Brocade Dell Device42 Fiber Channel Free Code Kelverion Kubernetes NSX NSX VMware powershell Python SANnav Scripts servicenow source-control Tanzu TKG Tower Vagrant VMware vRo VSCode vsphere

Site Pages

  • about the author
© 2026 The OpsCorner | Built with Xblog Plus free WordPress theme by wpthemespace.com