style_guide.adoc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 our 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. == Ansible Variable Naming
  8. === Global Variables
  9. Ansible global variables are defined as any variables outside of ansible roles. Examples include playbook variables, variables passed in on the cli, etc.
  10. '''
  11. [cols="2v,v"]
  12. |===
  13. | **Rule**
  14. | Global variables MUST have a prefix of g_
  15. |===
  16. Example:
  17. [source]
  18. ----
  19. g_environment: someval
  20. ----
  21. ==== Role Variables
  22. Ansible role variables are defined as variables contained in (or passed into) a role.
  23. '''
  24. [cols="2v,v"]
  25. |===
  26. | **Rule**
  27. | Role variables MUST have a prefix of atleast 3 characters. See below for specific naming rules.
  28. |===
  29. ===== Role with 3 (or more) words in the name
  30. Take the first letter of each of the words.
  31. .3 word example:
  32. * Role name: made_up_role
  33. * Prefix: mur
  34. [source]
  35. ----
  36. mur_var1: value_one
  37. ----
  38. .4 word example:
  39. * Role name: totally_made_up_role
  40. * Prefix: tmur
  41. [source]
  42. ----
  43. tmur_var1: value_one
  44. ----
  45. ===== Role with 2 (or less) words in the name
  46. Make up a prefix that makes sense.
  47. .1 word example:
  48. * Role name: ansible
  49. * Prefix: ans
  50. [source]
  51. ----
  52. ans_var1: value_one
  53. ----
  54. .2 word example:
  55. * Role name: ansible_tower
  56. * Prefix: tow
  57. [source]
  58. ----
  59. tow_var1: value_one
  60. ----
  61. ===== Role name prefix conflicts
  62. If two role names contain words that start with the same letters, it will seem like their prefixes would conflict.
  63. 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).
  64. .Same prefix example:
  65. * First Role Name: made_up_role
  66. * First Role Prefix: mur
  67. * Second Role Name: my_uber_role
  68. * Second Role Prefix: mur
  69. [source]
  70. ----
  71. - hosts: localhost
  72. roles:
  73. - { role: made_up_role, mur_var1: val1 }
  74. - { role: my_uber_role, mur_var1: val2 }
  75. ----
  76. 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.
  77. 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.
  78. 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.