Ansible Best Practices Roles & Modules
Ansible Best Practices Roles & Modules
4
COMPLEXITY KILLS PRODUCTIVITY.
That's not just a marketing slogan. We really mean it and
believe that. We strive to reduce complexity in how we've
designed Ansible tools and encourage you to do the
same. Strive for simplification in what you automate.
5
OPTIMIZE FOR READABILITY.
If done properly, it can be the documentation of your
workflow automation.
6
THINK DECLARATIVELY.
Ansible is a desired state engine by design. If you're
trying to "write code" in your plays and roles, you're
setting yourself up for failure. Our YAML-based
playbooks were never meant to be for programming.
7
ROLES + MODULES
ROLES MODULES
• Self-contained portable units • Small “programs” that
of Ansible automation perform actions on remote
• Expressed in YAML and hosts or on their behalf
bundled with associated • Expressed as code
assets – i.e. Python, PowerShell
• Decoupled from assumptions • Called by an Ansible task
made by plays • Modules do all of the heavy
lifting in Ansible
8
ROLES + MODULES
ROLES MODULES
• Reuse and collaboration of • Sophisticated interactions
common automation and logic of a unit of work
workflows & configurations usually with a command line
• Provide full life-cycle tool or APIs
management of a service, • Abstract complexity away
microservice or container from users to make powerful
• “De-facto” enforcement of automation simple
standards and policies
9
ROLES + MODULES
ROLES MODULES
10
BEST PRACTICES: ROLES
11
ROLES ARE ANSIBLE CONTENT
12
ROLE DESIGN
13
EXHIBIT A EXHIBIT B
# blackbox_role_playbook.yml # componentized_roles_playbook.yml
--- ---
- hosts: all - hosts: localhost
roles: roles:
- umbrella_corp_stack - azure_provisioner
- hosts: all
roles:
- system_security
- hosts: webservers
roles:
- python_common
- python_django
- nginx_uwsgi
- racoon_app
- hosts: databases
roles:
- pgsql-replication
14
ROLE DESIGN
# requirements.yml
• Use ansible-galaxy to
---
install your roles - src: nginxinc.nginx
• Use a roles files (i.e. version: 0.8.0
- src: samdoran.pgsql-replication
requirements.yml) to version: b5013e6
manifest your project roles - src: geerlingguy.firewall
version: 2.4.0
• When using a shared role
always declare a specific
version such as a tag or
commit
16
ROLE USABILITY
17
EXHIBIT A EXHIBIT B
# defaults_no_playbook.yml # defaults_yes_playbook.yml
--- ---
- hosts: webservers - hosts: webservers
roles: roles:
- role: apache_simple - role: apache_simple
apache_http_port: 80 - role: apache_simple
apache_doc_root: /var/www/html apache_http_port: 8080
apache_user: apache apache_doc_root: /www/example.com
apache_group: apache
- role: apache_simple
apache_http_port: 8080 # default/main.yml
apache_doc_root: /www/example.com ---
apache_user: apache apache_http_port: 80
apache_group: apache apache_doc_root: /var/www/html
apache_user: apache
apache_group: apache
18
ROLE USABILITY
19
# default/main.yml # vars/main.yml
--- ---
apache_http_port: 80 apache_packages:
apache_doc_root: /var/www/html redhat:
apache_user: apache - httpd
apache_group: apache - mod_wsgi
debian:
- apache2
- libapache2-mod-wsgi
20
ROLE USABILITY
https://github.com/ansible/molecule
22
ROLE USABILITY
https://github.com/ansible/ansible-lint
24
ROLE READABILITY
25
BEST PRACTICES: MODULES
26
MODULE DESIGN
27
MODULE IMPLEMENTATION
28
MODULES INTERFACE
update_cache # YES
UpdateCache # NO
updateCache # NO
29
MODULES INTERFACE
31
MODULES IN THE WILD
• kubernetes
– monolithic and requires expert knowledge of k8s
• ansible-kubernetes-modules
– fine grained API mapping that is autogenerated
• k8s
– better implementation but complex parameters abound and expert knowledge
still required
• k8s_scale
– more focused on a specific task — more of this please
32
MODULE RESPONSES
33
MODULE EXCEPTION HANDLING
34
MODULE IMPLEMENTATION
36
MODULE DOCUMENTATION
Documentation is a requirement
• Examples should include the most common and real world usage
• Examples should be in native YAML syntax
• Return responses must be included and described
• Document your dependencies in the requirements section
37
MODULE TESTING
38
MODULES IN THE WILD
• sysctl
– a master class in writing a “best practice” module
• ping
– the hello world of Ansible
• cron
– module implementing an interface to a command line tool
• get_url
– module implementing an interface to a python library
39
MODULE & ACTION PLUGINS
40
Thanks
41
MORE RESOURCES
43