{"id":16409851,"url":"https://github.com/hyperized/ansible-code-conventions","last_synced_at":"2025-10-26T17:32:40.454Z","repository":{"id":85138511,"uuid":"62789651","full_name":"hyperized/ansible-code-conventions","owner":"hyperized","description":"My guide on how to write effective and fault tolerant Ansible code","archived":false,"fork":false,"pushed_at":"2016-10-11T12:26:40.000Z","size":5,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-07T23:11:28.963Z","etag":null,"topics":["ansible"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hyperized.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-07T08:33:41.000Z","updated_at":"2023-12-18T11:41:51.000Z","dependencies_parsed_at":"2023-06-29T11:00:54.441Z","dependency_job_id":null,"html_url":"https://github.com/hyperized/ansible-code-conventions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hyperized/ansible-code-conventions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperized%2Fansible-code-conventions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperized%2Fansible-code-conventions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperized%2Fansible-code-conventions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperized%2Fansible-code-conventions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyperized","download_url":"https://codeload.github.com/hyperized/ansible-code-conventions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperized%2Fansible-code-conventions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281141150,"owners_count":26450601,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-26T02:00:06.575Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible"],"created_at":"2024-10-11T06:21:42.062Z","updated_at":"2025-10-26T17:32:40.449Z","avatar_url":"https://github.com/hyperized.png","language":null,"readme":"# ansible-code-conventions\nA guide on how to write effective and fault tolerant Ansible code.\n\nAnsible code convention:\n\n- Descriptions are not capitalized\n\n```yaml\n- name: ensuring something\n```\n\n- The indentation is 2 (two) spaces, for vim you can use [vim-ansible-yaml](https://github.com/chase/vim-ansible-yaml)\n- Use dict notation, not in-line, this prevents errors later on, example:\n\n```yaml\nbad: this=command\n\ngood:\n  this: command\n```\n- When a module does require '=' parameters, use the following:\n\n```yaml\nmoduleX:\n  args:\n    var: 'something'\n```\n- When a module does call something using '=' parameters, use the following:\n\n```yaml\nmoduleX: \u003e\n  var='something'\n```\n\n- Playbooks should be used for role assignments.\n\t- Use tagging to call a role:\n\n```yaml\nroles:\n  - { role: myrole, tags: ['myrole', 'optional_alias_myrole'] }\n```\n\n- Roles should be used for functional blocks.\n    - All templates must contain a `{{ ansible_managed }}` header.\n    - Ensure `ansible_managed` header is statically set in `ansible.cfg` or it breaks idempotency.\n\t- Every role has a self documenting `defaults/main.yml` file that contains all set-able variables.\n\t- All variables in the role scope must be named `role_variable` with the option of spaces being replaced by `_` hyphens.\n\t- The use of dict style variable settings `role.variable` is discouraged as it complicates overriding.\n\t- Every variable on a new line for readability.\n\t- Role must be a complete basic implementation without requiring further variables.\n\t- Role must be idempotent, meaning: including the role must result in OK, not changed or error when nothing has been changed on the target host.\n\t- Try to stow everything in `main.yml`, only use include when it makes sense, which is:\n\t\t- when you need to support multiple platforms, then include each in `main.yml`\n\t- Use of comments indicate bad code.\n- Completeness (especially idempotency) on the target platform is preferred over portability when starting out a new role.\n- Don't use: `backup: yes`, rely on git instead!\n\n## How-to `when` conditions\n\nGeneral rule for `when` conditions is that variables do not have to be surrounded like `{{ var }}`. Below are some general use-cases for when conditions:\n\n- When dealing with multiple suitors for a single condition:\n\n```yaml\n- set_fact:\n    hello: 'world'\n\n- name: do task\n  command: echo hello world!\n  when:\n    - hello | intersect( ['world', 'universe'] )\n```\n- When dealing with multiple `when` conditions that need to match in an `OR` fashion:\n\n```yaml\n- name: do task\n  command: echo hello world!\n  when:\n    - ((skip_install != True) or (ansible_os_family == 'Debian'))\n```\n\n- When dealing with multiple `when` conditions that need to match in an `AND` fashion:\n\n```yaml\n- name: do task\n  command: echo hello world!\n  when:\n    - (skip_install != True)\n    - (ansible_os_family == 'Debian')\n    - (command_result.changed == True)\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperized%2Fansible-code-conventions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyperized%2Fansible-code-conventions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperized%2Fansible-code-conventions/lists"}