Ansiblers: label your shit

I just dove into a 55,000-line bundle of Ansible and accompanying scripts. My team and I are just trying to get it to work.

What’s most frustrating to someone new to Ansible, or to a new bundle of playbooks and roles, is figuring out where a broken task is. Developers: name your shit.

Assume we have a collection of playbooks and roles:

$ tree
.
├── group_vars
│ └── web.yml
├── host_vars
│ └── web1.yml
├── hosts.py
├── playbooks
│ └── webservers.yml
└── roles
├── base
│ ├── defaults
│ │ └── main.yml
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ └── vars
│ └── main.yml
├── requirements.yml
└── webserver
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ ├── config_properties.yml
│ ├── install_nginx.yml
│ └── main.yml
└── vars
├── main.yml
└── properties.yml

Here’s an unhelpful way to label a task:

- name: Load variables describing config properties
include_vars:
file: "{{ _var_d }}/properties.yml"
... or even ...- include_vars:
file: "{{ _var_d }}/properties.yml"

Uhhh, what? Thanks a lot for being of ZERO help. Let’s try that again:

- name: config_properties | Load variables describing config properties
include_vars:
file: "{{ _var_d }}/properties.yml"
... let's run ansible-playbook ...TASK [webserver : config_properties | Load variables describing config properties]
...

See? I can tell, looking at Ansible’s logs, that it failed loading variables in the config_properties task, in the webserver role. Don’t make me use grep: label your tasks. While we’re at it, label your damned plays, too. Here’s an unhelpful play declaration:

# playbooks/webservers.yml
---
- hosts: webservers
roles:
- webserver
... let's run ansible-playbook ...PLAY [localhost] ***************************************************

Be better:

- name: playbooks | webservers | run webservers role on webservers
hosts: webservers
roles:
- webserver
.... let's run ansible-playbook ...PLAY [playbooks | webservers | run webservers role on webservers] **

Label your plays. While we’re still talking about this, label your damned variables correctly, too. WTF is this?

install_path: "/opt/my-webserver"
port: 3000

Label your variables. Sanely. If you have a collection of variables that are related, then give them a namespace:

nginx_install_path: "/opt/nginx"
nginx_port: 3000