cancel
Showing results for 
Search instead for 
Did you mean: 
AmeyaSathe
Mission Specialist
Mission Specialist
  • 1,084 Views

YAML Indentation of playbook

Jump to solution

Hello,

I do not understand the  indentation rules of the ansible playbook.

I have the following understanding
Within an ansible playbook, "tasks" is a list of modules.

As per YAML Syntax , "All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):"

 

 

---
- name: Play    # Dash is at first column followed by space and then "name"
  tasks:   # Modules is list of items
  - name:  # Hence, on this row, I pressed space bar 2 times to indent it.

 

 

Yet, the examples show indentation like,

 

 

---
- name: Deploy and start Apache HTTPD service
  tasks:
    - name: Required packages are installed and up to date

 

 

Please can anyone help me to understand the reason behind the hyphen starting below letter 's' of the tasks.

Regards,

Ameya

Labels (1)
Tags (3)
1 Solution

Accepted Solutions
Trevor
Starfighter Starfighter
Starfighter
  • 1,052 Views

Hello Ameya,

Let me begin by answering your question, and then I'll elaborate.  The short
answer to your question, "Please can anyone help me to understand the reason
behind the hyphen starting below letter 's' of the tasks."    -     style.

That dash (-) that identifies the first task, can appear under the letter 't' in the
word "task", or that dash (-) can appear under the letter 's' in the work 'task'.
Where that dash (-) appears is NOT significant.  What is significant/imperative
is the CONSISTENCY of where that dash (-) appears.  Let me provide an example
to convey my meaning.  

Below, I'm going to show you three (3) different playbooks.  Each playbook 
will be identical, in terms of content.  The ONLY difference in the playbooks will
be the formatting.  To enhance the point that I'm attempting to make, each
playbook will contain three (3) tasks.  There's nothing magical or mystical about
that number of tasks.  The emphasis is on the indentation of each task!!!!

Now you're going to have to look very closely at the indentation, because
the spacing is very subtle.  Those first two (2) playbooks will run/execute
without issue.  Again, if you look closely at the playbook content, it is identical,
but the indentation of the tasks in First and Second playbook are NOT the same.
There are more spaces from the beginning of each line, for each of the tasks in
the Second playbook, than in the First playbook.  Even with that being the case,
these two playbooks will execute without any issue.  One more time, it is NOT
the spacing alone that is important, it is the CONSISTENCY of the spacing that
is CRITICAL!!!  The format of that Third playbook makes this abundantly,
extremely, exceptionally clear.  

Note the inconsistency in the spacing/indentation of the beginning of each
task in the Third playbook.  The first and third tasks have the same spacing/inidentation,
but the second tasks begins with more spacing than the first and third task.  Don't
believe me?  Go ahead, and execute that third playbook, and when it fails to 
execute, take two aspirin, and call me in the morning

 

First playbook:

---
- name: Play1
hosts: localhost
tasks:

- name: Task1
    debug:
    msg: This is a message from Task1

- name: Task2
  debug:
    msg: This is a message from Task2

- name: Task3
  debug:
    msg: This is a message from Task3


Second playbook:

---
- name: Play1
hosts: localhost
tasks:

    - name: Task1
      debug:
        msg: This is a message from Task1

    - name: Task2
      debug:
        msg: This is a message from Task2

    - name: Task3
      debug:
        msg: This is a message from Task3

 

Third playbook:

---
- name: Play1
hosts: localhost
tasks:

- name: Task1
    debug:
    msg: This is a message from Task1

  - name: Task2
    debug:
      msg: This is a message from Task2

- name: Task3
  debug:
    msg: This is a message from Task3

 

Conclusion:

If you want to begin each task, with that dash (-) under the 't' in the word "task",
under the letter 's' in the word task, or 20 spaces from the beginning of the line,
just make sure that you are CONSISTENT in that spacing/indentation for each
task!!!!

It's unfortunate that you had to see this inconsistency somewhere.  Learning
Ansible can already be challenging enough without this spacing/indentation
nuance, but you'll get it.  Keep studying, and by all means keep the questions
coming!!!

Trevor "Red Hat Evangelist" Chandler

View solution in original post

4 Replies
Chetan_Tiwary_
Moderator
Moderator
  • 1,055 Views

Hi @AmeyaSathe ,

Thanks for reaching out !


Please refer these articles : https://www.redhat.com/sysadmin/yaml-beginners 

and https://www.redhat.com/sysadmin/understanding-yaml-ansible , see if this helps you with your query.

Tags (3)
0 Kudos
Trevor
Starfighter Starfighter
Starfighter
  • 1,053 Views

Hello Ameya,

Let me begin by answering your question, and then I'll elaborate.  The short
answer to your question, "Please can anyone help me to understand the reason
behind the hyphen starting below letter 's' of the tasks."    -     style.

That dash (-) that identifies the first task, can appear under the letter 't' in the
word "task", or that dash (-) can appear under the letter 's' in the work 'task'.
Where that dash (-) appears is NOT significant.  What is significant/imperative
is the CONSISTENCY of where that dash (-) appears.  Let me provide an example
to convey my meaning.  

Below, I'm going to show you three (3) different playbooks.  Each playbook 
will be identical, in terms of content.  The ONLY difference in the playbooks will
be the formatting.  To enhance the point that I'm attempting to make, each
playbook will contain three (3) tasks.  There's nothing magical or mystical about
that number of tasks.  The emphasis is on the indentation of each task!!!!

Now you're going to have to look very closely at the indentation, because
the spacing is very subtle.  Those first two (2) playbooks will run/execute
without issue.  Again, if you look closely at the playbook content, it is identical,
but the indentation of the tasks in First and Second playbook are NOT the same.
There are more spaces from the beginning of each line, for each of the tasks in
the Second playbook, than in the First playbook.  Even with that being the case,
these two playbooks will execute without any issue.  One more time, it is NOT
the spacing alone that is important, it is the CONSISTENCY of the spacing that
is CRITICAL!!!  The format of that Third playbook makes this abundantly,
extremely, exceptionally clear.  

Note the inconsistency in the spacing/indentation of the beginning of each
task in the Third playbook.  The first and third tasks have the same spacing/inidentation,
but the second tasks begins with more spacing than the first and third task.  Don't
believe me?  Go ahead, and execute that third playbook, and when it fails to 
execute, take two aspirin, and call me in the morning

 

First playbook:

---
- name: Play1
hosts: localhost
tasks:

- name: Task1
    debug:
    msg: This is a message from Task1

- name: Task2
  debug:
    msg: This is a message from Task2

- name: Task3
  debug:
    msg: This is a message from Task3


Second playbook:

---
- name: Play1
hosts: localhost
tasks:

    - name: Task1
      debug:
        msg: This is a message from Task1

    - name: Task2
      debug:
        msg: This is a message from Task2

    - name: Task3
      debug:
        msg: This is a message from Task3

 

Third playbook:

---
- name: Play1
hosts: localhost
tasks:

- name: Task1
    debug:
    msg: This is a message from Task1

  - name: Task2
    debug:
      msg: This is a message from Task2

- name: Task3
  debug:
    msg: This is a message from Task3

 

Conclusion:

If you want to begin each task, with that dash (-) under the 't' in the word "task",
under the letter 's' in the word task, or 20 spaces from the beginning of the line,
just make sure that you are CONSISTENT in that spacing/indentation for each
task!!!!

It's unfortunate that you had to see this inconsistency somewhere.  Learning
Ansible can already be challenging enough without this spacing/indentation
nuance, but you'll get it.  Keep studying, and by all means keep the questions
coming!!!

Trevor "Red Hat Evangelist" Chandler
Chetan_Tiwary_
Moderator
Moderator
  • 1,050 Views

Thanks @Trevor  for such a nicely structured answer & the explanation !

Trevor
Starfighter Starfighter
Starfighter
  • 1,047 Views

Thank you Chetan for the very kind feedback!!!  Now, let's just hope that
my response to the query has a satisfactory outcome.

 

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.