I have seen multiple Ansible examples on github and in the ansible docs, e.g.:
---
# this might be in a file like handlers/handlers.yml
- name: restart apache
service: name=apache state=restarted
The following example contains both a comment as a name
.
# Make sure Jenkins starts, then configure Jenkins.
- name: Ensure Jenkins is started and runs on startup.
service: name=jenkins state=started enabled=yes
Discussion
A name
would be sufficient right or should a comment be used?
Should it be:
- name: Symlink RabbitMQ bin to sbin
file: state=link src=/usr/lib/rabbitmq/bin dest=/usr/lib/rabbitmq/sbin
or:
#Symlink RabbitMQ bin to sbin
file:
state: link
src: /usr/lib/rabbitmq/binhttp://docs.ansible.com/ansible/YAMLSyntax.html
dest: /usr/lib/rabbitmq/sbin
When YAML Lint is consulted as recommended by the Ansible YAML syntax doc both snippets seem to be valid YAML. Although both snippets seem to be valid YAML the visual structure is different.
Questions
- Should name (
name
) be used or a comment (#
)? - Should it be
file: state=link src=/usr/lib/rabbitmq/bin dest=/usr/lib/rabbitmq/sbin
or should the elemented by split up, e.g.state:
Please understand I believe my answer to be highly subjective. Internally my team loosely agrees with my opinions on this. But we've not draft any "formatting policy" for playbooks.
We only include comments if it is useful to explain the "why?" of the particular task.
name
is always present. The value ofname
will be displayed during the playbook run. In cases where a role is used as a dependency, I often parameterizedname
. A couple examples.Parameterized
name
example, from roles/some_container/meta/main.ymlroles/remove_container/tasks/main.yml
Comments as complimentary to
name
. roles/remove_image/tasks/main.ymlI always use the 'k: v' syntax. Additionally I break separate values with a new line. When reading a play where someone has stuffed many 'k=v' on a single line my brain gets twisted up. I find it very difficult to juggle all the keys/values as I read along for the ones I'm interested.
Which is easier to read? I think the second example.
I also make judicious use of white space at times.
That is up to your own preference.
Comments like
# Make sure Jenkins starts, then configure Jenkins.
don't make much sense as they don't add more information.Inline
syntax is supported byYAML
to be compatible withJSON
.Outline
syntax however should be preferred because, the code is better readable and code changes can be reviewed better with diff.