style_guide.adoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // vim: ft=asciidoc
  2. = openshift-ansible Style Guide
  3. The purpose of this guide is to describe the preferred coding conventions used in this repository (both in ansible and python).
  4. It is important to note that this repository may not currently comply with all style guide rules, but the intention is that it will.
  5. All new pull requests created against this repository MUST comply with this guide.
  6. This style guide complies with https://www.ietf.org/rfc/rfc2119.txt[RFC2119].
  7. == Python
  8. === Python Maximum Line Length
  9. .Context:
  10. * https://www.python.org/dev/peps/pep-0008/#maximum-line-length[Python Pep8 Line Length]
  11. '''
  12. [[All-lines-SHOULD-be-no-longer-than-80-characters]]
  13. [cols="2v,v"]
  14. |===
  15. | <<All-lines-SHOULD-be-no-longer-than-80-characters, Rule>>
  16. | All lines SHOULD be no longer than 80 characters.
  17. |===
  18. Every attempt SHOULD be made to comply with this soft line length limit, and only when it makes the code more readable should this be violated.
  19. Code readability is subjective, therefore pull-requests SHOULD still be merged, even if they violate this soft limit as it is up to the individual contributor to determine if they should violate the 80 character soft limit.
  20. '''
  21. [[All-lines-MUST-be-no-longer-than-120-characters]]
  22. [cols="2v,v"]
  23. |===
  24. | <<All-lines-MUST-be-no-longer-than-120-characters, Rule>>
  25. | All lines MUST be no longer than 120 characters.
  26. |===
  27. This is a hard limit and is enforced by the build bot. This check MUST NOT be disabled.
  28. == Ansible
  29. === Ansible Yaml file extension
  30. '''
  31. [[All-Ansible-Yaml-files-MUST-have-a-yml-extension-and-NOT-YML-yaml-etc]]
  32. [cols="2v,v"]
  33. |===
  34. | <<All-Ansible-Yaml-files-MUST-have-a-yml-extension-and-NOT-YML-yaml-etc, Rule>>
  35. | All Ansible Yaml files MUST have a .yml extension (and NOT .YML, .yaml etc).
  36. |===
  37. Ansible tooling (like `ansible-galaxy init`) create files with a .yml extension. Also, the Ansible documentation website references files with a .yml extension several times. Because of this, it is normal in the Ansible community to use a .yml extension for all Ansible Yaml files.
  38. Example: `tasks.yml`
  39. === Ansible CLI Variables
  40. '''
  41. [[Variables-meant-to-be-passed-in-from-the-ansible-CLI-MUST-have-a-prefix-of-cli]]
  42. [cols="2v,v"]
  43. |===
  44. | <<Variables-meant-to-be-passed-in-from-the-ansible-CLI-MUST-have-a-prefix-of-cli, Rule>>
  45. | Variables meant to be passed in from the ansible CLI MUST have a prefix of cli_
  46. |===
  47. Ansible allows variables to be passed in on the command line using the `-e` option. These variables MUST have a prefix of cli_ so that it's clear where they came from, and that they could be exploited.
  48. .Example:
  49. [source]
  50. ----
  51. ansible-playbook -e cli_foo=bar someplays.yml
  52. ----
  53. === Ansible Global Variables
  54. '''
  55. [[Global-variables-MUST-have-a-prefix-of-g]]
  56. [cols="2v,v"]
  57. |===
  58. | <<Global-variables-MUST-have-a-prefix-of-g, Rule>>
  59. | Global variables MUST have a prefix of g_
  60. |===
  61. Ansible global variables are defined as any variables outside of ansible roles. Examples include playbook variables, variables passed in on the cli, etc.
  62. .Example:
  63. [source]
  64. ----
  65. g_environment: someval
  66. ----
  67. === Ansible Role Variables
  68. Ansible role variables are defined as variables contained in (or passed into) a role.
  69. '''
  70. [[Role-variables-MUST-have-a-prefix-of-atleast-3-characters-See.below.for.specific.naming.rules]]
  71. [cols="2v,v"]
  72. |===
  73. | <<Role-variables-MUST-have-a-prefix-of-atleast-3-characters-See.below.for.specific.naming.rules, Rule>>
  74. | Role variables MUST have a prefix of at least 3 characters. See below for specific naming rules.
  75. |===
  76. ==== Role with 3 (or more) words in the name
  77. Take the first letter of each of the words.
  78. .3 word example:
  79. * Role name: made_up_role
  80. * Prefix: mur
  81. [source]
  82. ----
  83. mur_var1: value_one
  84. ----
  85. .4 word example:
  86. * Role name: totally_made_up_role
  87. * Prefix: tmur
  88. [source]
  89. ----
  90. tmur_var1: value_one
  91. ----
  92. ==== Role with 2 (or less) words in the name
  93. Make up a prefix that makes sense.
  94. .1 word example:
  95. * Role name: ansible
  96. * Prefix: ans
  97. [source]
  98. ----
  99. ans_var1: value_one
  100. ----
  101. .2 word example:
  102. * Role name: ansible_tower
  103. * Prefix: tow
  104. [source]
  105. ----
  106. tow_var1: value_one
  107. ----
  108. ==== Role name prefix conflicts
  109. If two role names contain words that start with the same letters, it will seem like their prefixes would conflict.
  110. Role variables are confined to the roles themselves, so this is actually only a problem if one of the roles depends on the other role (or uses includes into the other role).
  111. .Same prefix example:
  112. * First Role Name: made_up_role
  113. * First Role Prefix: mur
  114. * Second Role Name: my_uber_role
  115. * Second Role Prefix: mur
  116. [source]
  117. ----
  118. - hosts: localhost
  119. roles:
  120. - { role: made_up_role, mur_var1: val1 }
  121. - { role: my_uber_role, mur_var1: val2 }
  122. ----
  123. Even though both roles have the same prefix (mur), and even though both roles have a variable named mur_var1, these two variables never exist outside of their respective roles. This means that this is not a problem.
  124. This would only be a problem if my_uber_role depended on made_up_role, or vice versa. Or if either of these two roles included things from the other.
  125. This is enough of a corner case that it is unlikely to happen. If it does, it will be addressed on a case by case basis.