Skip to content

Commit db57866

Browse files
authored
Add ansible-styleguide (#40)
Signed-off-by: Ramona Beermann <[email protected]>
1 parent 0cc01d9 commit db57866

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Ansible Style Guide
2+
3+
We use nearly all default rules of ansible lint. A listing of all these rules can be found in the Ansible Lint documentation:
4+
<https://ansible.readthedocs.io/projects/lint/rules/>.
5+
Please always use the ansible linting to check if the code complies with the default linting rules.
6+
However, since in most cases we always use the latest version of packages and Ansible lint does not provide this, we decided to
7+
disable the package_latest rule.
8+
9+
## Task naming
10+
11+
* Tasks must always have names. The only exception allowed is for forked playbooks.
12+
* A name never starts with a small letter
13+
* Names are written in present tense
14+
* No punctuation is used in names
15+
16+
## Key Order
17+
18+
To check the key order we use our own rule. This can be found [here](https://github.com/osism/zuul-jobs/tree/main/roles/ansible-lint/files).
19+
20+
### Positioning and use of the become directive
21+
22+
The become directive is only set when needed and is always set explicitly for each task that needs it.
23+
24+
Blocks, roles or playbooks are never executed in a privileged mode.
25+
26+
We always insert the become directive between the name of a task and the task itself. This also applies to related directives
27+
like *become_user* or *become_flags*. This is for better visibility if a task is privileged or not.
28+
29+
```yaml
30+
- name: Copy hddtemp configuration file
31+
become: true
32+
ansible.builtin.copy:
33+
src: "{{ ansible_os_family }}/hddtemp"
34+
dest: "{{ hddtemp_conf_file }}"
35+
owner: root
36+
group: root
37+
mode: 0644
38+
notify: Restart hddtemp service
39+
```
40+
41+
### Position of the when condition
42+
43+
If you need to use the when condition please add this at the end-section from the task where it is needed. This makes the code
44+
easier to understand for others. Ansible lint provides the when condition under the task name for blocks. To keep the code clear
45+
we decided against it. Please disable this with a noqa if necessary. For example:
46+
47+
```yaml
48+
- name: "Archive existing {{ resolvconf_file }} file"
49+
become: true
50+
ansible.posix.synchronize:
51+
src: "/etc/resolv.conf"
52+
dest: "/etc/resolv.conf.{{ ansible_date_time.date }}"
53+
archive: true
54+
delegate_to: "{{ inventory_hostname }}"
55+
when: stat_resolvconf_file.stat.islnk is defined and not stat_resolvconf_file.stat.islnk
56+
```
57+
58+
## Usage of collections
59+
60+
Collections are always defined as in the following example.
61+
62+
**netbox.netbox** is here the collection that is used.
63+
64+
```yaml
65+
- name: Configure netbox manufacturers
66+
netbox.netbox.netbox_manufacturer:
67+
netbox_url: "{{ netbox_url }}"
68+
netbox_token: "{{ netbox_token }}"
69+
data:
70+
name: "{{ item.value.name }}"
71+
slug: "{{ item.value.slug }}"
72+
description: "{{ item.value.description | default('') }}"
73+
state: present
74+
with_dict: "{{ netbox_data_manufacturers }}"
75+
```
76+
77+
Please don´t declare it in this way!:
78+
79+
```yaml
80+
collections:
81+
- netbox.netbox
82+
83+
tasks:
84+
- name: Manage Discworld site
85+
netbox_site:
86+
netbox_url: "{{ netbox_url }}"
87+
netbox_token: "{{ netbox_token }}"
88+
validate_certs: false
89+
data:
90+
name: Discworld
91+
slug: discworld
92+
state: present
93+
```
94+
95+
If you have to use collections please define them in a requirements.yml.
96+
97+
Example yaml:
98+
99+
```yaml
100+
roles:
101+
- name: geerlingguy.certbot
102+
version: master
103+
type: git
104+
src: git+https://github.com/geerlingguy/ansible-role-certbot.git
105+
...
106+
107+
collections:
108+
- name: ansible.netcommon
109+
source: https://galaxy.ansible.com
110+
111+
- name: https://github.com/ansible-collections/ansible.posix.git
112+
type: git
113+
version: main
114+
```
115+
116+
## Usage of roles from other collections
117+
118+
If you want to reuse roles please do it in the following way:
119+
120+
First you have to declare the role or collection in the requirements.yml like shown in the example before.
121+
122+
Than you can use it in playbooks like this
123+
124+
```yaml
125+
roles:
126+
- role: osism.services.auditd
127+
```
128+
129+
## Parameters that offer lists
130+
131+
Parameters that provide a list are always defined as in the following example.
132+
133+
**docker_hosts_defaults** sets the defaults in the role. Overriding is only possible with the **ansible-defaults** repository.
134+
135+
In the configuration repository, docker_hosts_extra is then used to add additional items to the list.
136+
137+
**docker_hosts** itself is never modified from the outside.
138+
139+
```yaml
140+
docker_hosts_defaults:
141+
- "unix:///var/run/docker.sock"
142+
docker_hosts_extra: []
143+
docker_hosts: "{{ docker_hosts_defaults + docker_hosts_extra }}"
144+
```
145+
146+
## Usage of changed_when
147+
148+
Please think twice before turning off changed_when. It's a fairly simple yet safety-relevant linting rule and is quite easy to
149+
implement.
150+
151+
## Disable linting rules
152+
153+
In principle, it is only allowed to disable rules if there is really no other possibility.
154+
Please do not disable rules in general but only in individual cases via Noqa. Please use in this case the full rulename and not
155+
the numbers, because them are depricated. If it makes sense to ignore a rule, please open up an issue in the
156+
<https://github.com/osism/issues> repository with a label discussion.

0 commit comments

Comments
 (0)