best_practices_guide.adoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // vim: ft=asciidoc
  2. = openshift-ansible Best Practices Guide
  3. The purpose of this guide is to describe the preferred patterns and best practices used in this repository (both in Ansible and Python).
  4. It is important to note that this repository may not currently comply with all best practices, but the intention is that it will.
  5. All new pull requests created against this repository MUST comply with this guide.
  6. This guide complies with https://www.ietf.org/rfc/rfc2119.txt[RFC2119].
  7. == Python
  8. === Method Signatures
  9. '''
  10. [[When-adding-a-new-parameter-to-an-existing-method-a-default-value-SHOULD-be-used]]
  11. [cols="2v,v"]
  12. |===
  13. | <<When-adding-a-new-parameter-to-an-existing-method-a-default-value-SHOULD-be-used, Rule>>
  14. | When adding a new parameter to an existing method, a default value SHOULD be used
  15. |===
  16. The purpose of this rule is to make it so that method signatures are backwards compatible.
  17. If this rule isn't followed, it will be necessary for the person who changed the method to search out all callers and make sure that they're able to use the new method signature.
  18. .Before:
  19. [source,python]
  20. ----
  21. def add_person(first_name, last_name):
  22. ----
  23. .After:
  24. [source,python]
  25. ----
  26. def add_person(first_name, last_name, age=None):
  27. ----
  28. === PyLint
  29. http://www.pylint.org/[PyLint] is used in an attempt to keep the Python code as clean and as manageable as possible. The build bot runs each pull request through PyLint and any warnings or errors cause the build bot to fail the pull request.
  30. '''
  31. [[PyLint-rules-MUST-NOT-be-disabled-on-a-whole-file]]
  32. [cols="2v,v"]
  33. |===
  34. | <<PyLint-rules-MUST-NOT-be-disabled-on-a-whole-file, Rule>>
  35. | PyLint rules MUST NOT be disabled on a whole file.
  36. |===
  37. Instead, http://docs.pylint.org/faq.html#is-it-possible-to-locally-disable-a-particular-message[disable the PyLint check on the line where PyLint is complaining].
  38. '''
  39. [[PyLint-rules-MUST-NOT-be-disabled-unless-they-meet-one-of-the-following-exceptions]]
  40. [cols="2v,v"]
  41. |===
  42. | <<PyLint-rules-MUST-NOT-be-disabled-unless-they-meet-one-of-the-following-exceptions, Rule>>
  43. | PyLint rules MUST NOT be disabled unless they meet one of the following exceptions
  44. |===
  45. .Exceptions:
  46. 1. When PyLint fails because of a dependency that can't be installed on the build bot
  47. 1. When PyLint fails because of including a module that is outside of control (like Ansible)
  48. 1. When PyLint fails, but the code makes more sense the way it is formatted (stylistic exception). For this exception, the description of the PyLint disable MUST state why the code is more clear, AND the person reviewing the PR will decide if they agree or not. The reviewer may reject the PR if they disagree with the reason for the disable.
  49. '''
  50. [[All-PyLint-rule-disables-MUST-be-documented-in-the-code]]
  51. [cols="2v,v"]
  52. |===
  53. | <<All-PyLint-rule-disables-MUST-be-documented-in-the-code, Rule>>
  54. | All PyLint rule disables MUST be documented in the code.
  55. |===
  56. The purpose of this rule is to inform future developers about the disable.
  57. .Specifically, the following MUST accompany every PyLint disable:
  58. 1. Why is the check being disabled?
  59. 1. Is disabling this check meant to be permanent or temporary?
  60. .Example:
  61. [source,python]
  62. ----
  63. # Reason: disable pylint maybe-no-member because overloaded use of
  64. # the module name causes pylint to not detect that 'results'
  65. # is an array or hash
  66. # Status: permanently disabled unless a way is found to fix this.
  67. # pylint: disable=maybe-no-member
  68. metadata[line] = results.pop()
  69. ----
  70. == Ansible
  71. === Yaml Files (Playbooks, Roles, Vars, etc)
  72. '''
  73. [[Ansible-files-SHOULD-NOT-use-JSON-use-pure-YAML-instead]]
  74. [cols="2v,v"]
  75. |===
  76. | <<Ansible-files-SHOULD-NOT-use-JSON-use-pure-YAML-instead, Rule>>
  77. | Ansible files SHOULD NOT use JSON (use pure YAML instead).
  78. |===
  79. YAML is a superset of JSON, which means that Ansible allows JSON syntax to be interspersed. Even though YAML (and by extension Ansible) allows for this, JSON SHOULD NOT be used.
  80. .Reasons:
  81. * Ansible is able to give clearer error messages when the files are pure YAML
  82. * YAML reads nicer (preference held by several team members)
  83. * YAML makes for nicer diffs as YAML tends to be multi-line, whereas JSON tends to be more concise
  84. .Exceptions:
  85. * Ansible static inventory files are INI files. To pass in variables for specific hosts, Ansible allows for these variables to be put inside of the static inventory files. These variables can be in JSON format, but can't be in YAML format. This is an acceptable use of JSON, as YAML is not allowed in this case.
  86. Every effort should be made to keep our Ansible YAML files in pure YAML.
  87. === Modules
  88. '''
  89. [[Custom-Ansible-modules-SHOULD-be-embedded-in-a-role]]
  90. [cols="2v,v"]
  91. |===
  92. | <<Custom-Ansible-modules-SHOULD-be-embedded-in-a-role, Rule>>
  93. | Custom Ansible modules SHOULD be embedded in a role.
  94. |===
  95. .Context
  96. * http://docs.ansible.com/ansible/playbooks_roles.html#embedding-modules-in-roles[Ansible doc on how to embed modules in roles]
  97. The purpose of this rule is to make it easy to include custom modules in our playbooks and share them on Ansible Galaxy.
  98. .Custom module `openshift_facts.py` is embedded in the `openshift_facts` role.
  99. ----
  100. > ll openshift-ansible/roles/openshift_facts/library/
  101. -rwxrwxr-x. 1 user group 33616 Jul 22 09:36 openshift_facts.py
  102. ----
  103. .Custom module `openshift_facts` can be used after `openshift_facts` role has been referenced.
  104. [source,yaml]
  105. ----
  106. - hosts: openshift_hosts
  107. gather_facts: no
  108. roles:
  109. - role: openshift_facts
  110. post_tasks:
  111. - openshift_facts
  112. role: common
  113. hostname: host
  114. public_hostname: host.example.com
  115. ----
  116. '''
  117. [[Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-format-when-3-or-more-parameters-are-being-passed]]
  118. [cols="2v,v"]
  119. |===
  120. | <<Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-format-when-3-or-more-parameters-are-being-passed, Rule>>
  121. | Parameters to Ansible modules SHOULD use the Yaml dictionary format when 3 or more parameters are being passed
  122. |===
  123. When a module has several parameters that are being passed in, it's hard to see exactly what value each parameter is getting. It is preferred to use the Ansible Yaml syntax to pass in parameters so that it's more clear what values are being passed for each parameter.
  124. .Bad:
  125. [source,yaml]
  126. ----
  127. - file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link
  128. ----
  129. .Good:
  130. [source,yaml]
  131. ----
  132. - file:
  133. src: /file/to/link/to
  134. dest: /path/to/symlink
  135. owner: foo
  136. group: foo
  137. state: link
  138. ----
  139. '''
  140. [[Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-format-when-the-line-length-exceeds-120-characters]]
  141. [cols="2v,v"]
  142. |===
  143. | <<Parameters-to-Ansible-modules-SHOULD-use-the-Yaml-dictionary-format-when-the-line-length-exceeds-120-characters, Rule>>
  144. | Parameters to Ansible modules SHOULD use the Yaml dictionary format when the line length exceeds 120 characters
  145. |===
  146. Lines that are long quickly become a wall of text that isn't easily parsable. It is preferred to use the Ansible Yaml syntax to pass in parameters so that it's more clear what values are being passed for each parameter.
  147. .Bad:
  148. [source,yaml]
  149. ----
  150. - get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
  151. ----
  152. .Good:
  153. [source,yaml]
  154. ----
  155. - get_url:
  156. url: http://example.com/path/file.conf
  157. dest: /etc/foo.conf
  158. sha256sum: b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
  159. ----
  160. '''
  161. [[The-Ansible-command-module-SHOULD-be-used-instead-of-the-Ansible-shell-module]]
  162. [cols="2v,v"]
  163. |===
  164. | <<The-Ansible-command-module-SHOULD-be-used-instead-of-the-Ansible-shell-module, Rule>>
  165. | The Ansible `command` module SHOULD be used instead of the Ansible `shell` module.
  166. |===
  167. .Context
  168. * http://docs.ansible.com/shell_module.html#notes[Ansible doc on why using the command module is a best practice]
  169. The Ansible `shell` module can run most commands that can be run from a bash CLI. This makes it extremely powerful, but it also opens our playbooks up to being exploited by attackers.
  170. .Bad:
  171. [source,yaml]
  172. ----
  173. - shell: "/bin/echo {{ cli_var }}"
  174. ----
  175. .Better:
  176. [source,yaml]
  177. ----
  178. - command: "/bin/echo {{ cli_var }}"
  179. ----
  180. '''
  181. [[The-Ansible-quote-filter-MUST-be-used-with-any-variable-passed-into-the-shell-module]]
  182. [cols="2v,v"]
  183. |===
  184. | <<The-Ansible-quote-filter-MUST-be-used-with-any-variable-passed-into-the-shell-module, Rule>>
  185. | The Ansible `quote` filter MUST be used with any variable passed into the shell module.
  186. |===
  187. .Context
  188. * http://docs.ansible.com/shell_module.html#notes[Ansible doc describing why to use the quote filter]
  189. It is recommended not to use the `shell` module. However, if it absolutely must be used, all variables passed into the `shell` module MUST use the `quote` filter to ensure they are shell safe.
  190. .Bad:
  191. [source,yaml]
  192. ----
  193. - shell: "/bin/echo {{ cli_var }}"
  194. ----
  195. .Good:
  196. [source,yaml]
  197. ----
  198. - shell: "/bin/echo {{ cli_var | quote }}"
  199. ----
  200. === Defensive Programming
  201. .Context
  202. * http://docs.ansible.com/fail_module.html[Ansible Fail Module]
  203. '''
  204. [[Ansible-playbooks-MUST-begin-with-checks-for-any-variables-that-they-require]]
  205. [cols="2v,v"]
  206. |===
  207. | <<Ansible-playbooks-MUST-begin-with-checks-for-any-variables-that-they-require, Rule>>
  208. | Ansible playbooks MUST begin with checks for any variables that they require.
  209. |===
  210. If an Ansible playbook requires certain variables to be set, it's best to check for these up front before any other actions have been performed. In this way, the user knows exactly what needs to be passed into the playbook.
  211. .Example:
  212. [source,yaml]
  213. ----
  214. ---
  215. - hosts: localhost
  216. gather_facts: no
  217. tasks:
  218. - fail: msg="This playbook requires g_environment to be set and non empty"
  219. when: g_environment is not defined or g_environment == ''
  220. ----
  221. '''
  222. [[Ansible-roles-tasks-main-yml-file-MUST-begin-with-checks-for-any-variables-that-they-require]]
  223. [cols="2v,v"]
  224. |===
  225. | <<Ansible-roles-tasks-main-yml-file-MUST-begin-with-checks-for-any-variables-that-they-require, Rule>>
  226. | Ansible roles tasks/main.yml file MUST begin with checks for any variables that they require.
  227. |===
  228. If an Ansible role requires certain variables to be set, it's best to check for these up front before any other actions have been performed. In this way, the user knows exactly what needs to be passed into the role.
  229. .Example:
  230. [source,yaml]
  231. ----
  232. ---
  233. # tasks/main.yml
  234. - fail: msg="This role requires arl_environment to be set and non empty"
  235. when: arl_environment is not defined or arl_environment == ''
  236. ----
  237. === Tasks
  238. '''
  239. [[Ansible-tasks-SHOULD-NOT-be-used-in-ansible-playbooks-Instead-use-pre_tasks-and-post_tasks]]
  240. [cols="2v,v"]
  241. |===
  242. | <<Ansible-tasks-SHOULD-NOT-be-used-in-ansible-playbooks-Instead-use-pre_tasks-and-post_tasks, Rule>>
  243. | Ansible tasks SHOULD NOT be used in Ansible playbooks. Instead, use pre_tasks and post_tasks.
  244. |===
  245. An Ansible play is defined as a Yaml dictionary. Because of that, Ansible doesn't know if the play's tasks list or roles list was specified first. Therefore Ansible always runs tasks after roles.
  246. This can be quite confusing if the tasks list is defined in the playbook before the roles list because people assume in order execution in Ansible.
  247. Therefore, we SHOULD use pre_tasks and post_tasks to make it more clear when the tasks will be run.
  248. .Context
  249. * https://docs.ansible.com/playbooks_roles.html[Ansible documentation on pre_tasks and post_tasks]
  250. .Bad:
  251. [source,yaml]
  252. ----
  253. ---
  254. # playbook.yml
  255. - hosts: localhost
  256. gather_facts: no
  257. tasks:
  258. - name: This will execute AFTER the example_role, so it's confusing
  259. debug: msg="in tasks list"
  260. roles:
  261. - role: example_role
  262. # roles/example_role/tasks/main.yml
  263. - debug: msg="in example_role"
  264. ----
  265. .Good:
  266. [source,yaml]
  267. ----
  268. ---
  269. # playbook.yml
  270. - hosts: localhost
  271. gather_facts: no
  272. pre_tasks:
  273. - name: This will execute BEFORE the example_role, so it makes sense
  274. debug: msg="in pre_tasks list"
  275. roles:
  276. - role: example_role
  277. # roles/example_role/tasks/main.yml
  278. - debug: msg="in example_role"
  279. ----
  280. === Roles
  281. '''
  282. [[All-tasks-in-a-role-SHOULD-be-tagged-with-the-role-name]]
  283. [cols="2v,v"]
  284. |===
  285. | <<All-tasks-in-a-role-SHOULD-be-tagged-with-the-role-name, Rule>>
  286. | All tasks in a role SHOULD be tagged with the role name.
  287. |===
  288. .Context
  289. * http://docs.ansible.com/playbooks_tags.html[Ansible doc explaining tags]
  290. Ansible tasks can be tagged, and then these tags can be used to either _run_ or _skip_ the tagged tasks using the `--tags` and `--skip-tags` ansible-playbook options respectively.
  291. This is very useful when developing and debugging new tasks. It can also significantly speed up playbook runs if the user specifies only the roles that changed.
  292. .Example:
  293. [source,yaml]
  294. ----
  295. ---
  296. # roles/example_role/tasks/main.yml
  297. - debug: msg="in example_role"
  298. tags:
  299. - example_role
  300. ----
  301. '''
  302. [[The-Ansible-roles-directory-MUST-maintain-a-flat-structure]]
  303. [cols="2v,v"]
  304. |===
  305. | <<The-Ansible-roles-directory-MUST-maintain-a-flat-structure, Rule>>
  306. | The Ansible roles directory MUST maintain a flat structure.
  307. |===
  308. .Context
  309. * http://docs.ansible.com/playbooks_best_practices.html#directory-layout[Ansible Suggested Directory Layout]
  310. .The purpose of this rule is to:
  311. * Comply with the upstream best practices
  312. * Make it familiar for new contributors
  313. * Make it compatible with Ansible Galaxy
  314. '''
  315. [[Ansible-Roles-SHOULD-be-named-like-technology_component_subcomponent]]
  316. [cols="2v,v"]
  317. |===
  318. | <<Ansible-Roles-SHOULD-be-named-like-technology_component_subcomponent, Rule>>
  319. | Ansible Roles SHOULD be named like technology_component[_subcomponent].
  320. |===
  321. For consistency, role names SHOULD follow the above naming pattern. It is important to note that this is a recommendation for role naming, and follows the pattern used by upstream.
  322. Many times the `technology` portion of the pattern will line up with a package name. It is advised that whenever possible, the package name should be used.
  323. .Examples:
  324. * The role to configure a master is called `openshift_control_plane`
  325. * The role to configure OpenShift specific yum repositories is called `openshift_repos`
  326. === Filters
  327. .Context:
  328. * https://docs.ansible.com/playbooks_filters.html[Ansible Playbook Filters]
  329. * http://jinja.pocoo.org/docs/dev/templates/#builtin-filters[Jinja2 Builtin Filters]
  330. '''
  331. [[The-default-filter-SHOULD-replace-empty-strings-lists-etc]]
  332. [cols="2v,v"]
  333. |===
  334. | <<The-default-filter-SHOULD-replace-empty-strings-lists-etc, Rule>>
  335. | The `default` filter SHOULD replace empty strings, lists, etc.
  336. |===
  337. When using the jinja2 `default` filter, unless the variable is a boolean, specify `true` as the second parameter. This will cause the default filter to replace empty strings, lists, etc with the provided default.
  338. This is because it is preferable to either have a sane default set than to have an empty string, list, etc. For example, it is preferable to have a config value set to a sane default than to have it simply set as an empty string.
  339. .From the http://jinja.pocoo.org/docs/dev/templates/[Jinja2 Docs]:
  340. [quote]
  341. If you want to use default with variables that evaluate to false you have to set the second parameter to true
  342. .Example:
  343. [source,yaml]
  344. ----
  345. ---
  346. - hosts: localhost
  347. gather_facts: no
  348. vars:
  349. somevar: ''
  350. tasks:
  351. - debug: var=somevar
  352. - name: "Will output 'somevar: []'"
  353. debug: "msg='somevar: [{{ somevar | default('the string was empty') }}]'"
  354. - name: "Will output 'somevar: [the string was empty]'"
  355. debug: "msg='somevar: [{{ somevar | default('the string was empty', true) }}]'"
  356. ----
  357. In other words, normally the `default` filter will only replace the value if it's undefined. By setting the second parameter to `true`, it will also replace the value if it defaults to a false value in Python, so None, empty list, empty string, etc.
  358. This is almost always more desirable than an empty list, string, etc.
  359. === Yum and DNF
  360. '''
  361. [[Package-installation-MUST-use-ansible-package-module-to-abstract-away-dnf-yum]]
  362. [cols="2v,v"]
  363. |===
  364. | <<Package-installation-MUST-use-ansible-package-module-to-abstract-away-dnf-yum, Rule>>
  365. | Package installation MUST use Ansible `package` module to abstract away dnf/yum.
  366. |===
  367. The Ansible `package` module calls the associated package manager for the underlying OS.
  368. .Reference
  369. * https://docs.ansible.com/ansible/package_module.html[Ansible package module]
  370. .Bad:
  371. [source,yaml]
  372. ----
  373. ---
  374. # tasks.yml
  375. - name: Install etcd (for etcdctl)
  376. yum: name=etcd state=latest
  377. when: ansible_pkg_mgr == yum
  378. register: install_result
  379. - name: Install etcd (for etcdctl)
  380. dnf: name=etcd state=latest
  381. when: ansible_pkg_mgr == dnf
  382. register: install_result
  383. ----
  384. .Good:
  385. [source,yaml]
  386. ----
  387. ---
  388. # tasks.yml
  389. - name: Install etcd (for etcdctl)
  390. package:
  391. name: etcd
  392. state: latest
  393. register: install_result
  394. ----