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!
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
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.
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.
Dear friend,
sorry for my incomplete answere above .I have attach the images and edited the answer.please check it.Hope this help.
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!
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
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.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.