Trevor
Flight Engineer
Flight Engineer
  • 409 Views

Ansible Playbook Syntax

Got a question about the syntax used in an Ansible playbook. Take a look at the two play declarations below:

The differences are subtle, but nevertheless still syntactically different.

1)

---
- name: “Create a directory”
hosts: localhost

 

2)

---
name: “Create a directory”
hosts: localhost

 

My question: Is there anything FUNCTIONALLY different between the two declarations? If the remainder of the playbook is a mirror image, will the outcome be the same?

Hope everyone is safe and well!

Trevor "Red Hat Evangelist" Chandler
Labels (1)
0 Kudos
6 Replies
charith1994
Flight Engineer Flight Engineer
Flight Engineer
  • 355 Views

Re: Ansible Playbook Syntax

Dear friend,

use a yaml to python or yaml to json converter to see how playbooks are structured.As finally althose content in the playbook is understand as a python dictionary (ansible is written in python).if you check the final dictionary you can see what kind of differences there can be.For above question your first one probably give a syntax error if you start your playbook with that.I have fix it in the pic below(ansible couldn't convert it to a python dictionary).and the second one will work fine when converting to json format but as ansible playbook  must be a list of plays it will generate a error.I have attached the two different errors  please check it 

first example working as it is valid yaml  as well as written as a list of playsfirst example working as it is valid yaml as well as written as a list of playssecond example will generate this error it will convert to valid json but ansible can not undestand it as a valid play.it should be written  as list of plays with a "-" in the frontsecond example will generate this error it will convert to valid json but ansible can not undestand it as a valid play.it should be written as list of plays with a "-" in the frontI have change the second example give a little indent.Now it is not consider as a valid json or yaml.I have change the second example give a little indent.Now it is not consider as a valid json or yaml. So Even though your second one is valid yaml and can be converted to json it can not consider as a valid play due to lack of the "-" in the front which make it a component of a list.

 

Reply
Loading...
Trevor
Flight Engineer
Flight Engineer
  • 325 Views

Re: Ansible Playbook Syntax

Thank you for your response.

Your reply that the second syntax is okay, is not in agreement with another responder.  I'm not attempting to make my query your problem, but this is part of my dilema - lack of consistency in agreements.

I'm sure I did a dry run on both syntax formats, but I don't recall receiving any errors.  I'll certainly take another look at it.

I'll continue to probe this matter.

Thanks for taking the time to respond to my query.  Hope you're
safe and well.

 

 

Trevor "Red Hat Evangelist" Chandler
0 Kudos
Reply
Loading...
charith1994
Flight Engineer Flight Engineer
Flight Engineer
  • 308 Views

Re: Ansible Playbook Syntax

Dear friend,

sorry for my incomplete answere above .I have attach the images and edited the answer.please check it.Hope this help.

Reply
Loading...
Trevor
Flight Engineer
Flight Engineer
  • 298 Views

Re: Ansible Playbook Syntax

Hello Charith,

You shouldn't apologize for attempting to assist someone with something that's not your responsibility.  You're being very gracious and generous to use your personal time to assist a complete and total stranger.  If any apologies should flow, they should be from my direction.

I do owe you an apology because apparently I didn't perform a dry run using both syntax formats, because if I had done so, I would have seen the same thing that I witnessed a moment ago.

Here are my findings:

1)

- name: Trevor's Playbook
  hosts: localhost

Outcome:   No errors/issues


2) 

- name: Trevor's Playbook
hosts: localhost

Outcome:  The word "error" is everywhere!!!!


3)

name: Trevor's Playbook
hosts: localhost

Outcome:  Again, that word "error" shows up.

 

Okay, so again, I owe you an apology because I didn't perform my complete due diligence. Had I don't so, I could have used your bandwidth for something else. Well, I'll certainly be more judicious with any future queries - and there will be some of those :-)

Thanks again Charith for your willingness to assist.  Also, thank you for the screenshots. Very, very helpful!

 

 

Trevor "Red Hat Evangelist" Chandler
Reply
Loading...
matteoclc
Flight Engineer
Flight Engineer
  • 345 Views

Re: Ansible Playbook Syntax

Hello Trevor,

both syntaxes are invalid since the first is missing indentation of "hosts" and the second is missing the hyphen too. A playbook file is a list of play, so every play is an element of that list and must be defined with a "hyphen". 

The working syntax is

 

Screenshot_20210131_221319.png

--
Matteo Calcagnini
Reply
Loading...
Trevor
Flight Engineer
Flight Engineer
  • 327 Views

Re: Ansible Playbook Syntax

Thank you for your response.  The lack of indentation on that "hosts" tag in that first example was my error.  I apologize for that one.  

On both syntax, I'm pretty sure I did a dry run on both, and I don't recall getting any error/failure output.  I'll take another look at it though.

Thanks again for taking the time to provide a reply.

 

 

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