ansible (best) practices - developermarch · ansible (best) practices raju gandhi. raju gandhi...

65

Upload: others

Post on 09-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread
Page 2: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

ANSIBLE (BEST) PRACTICES

Raju Gandhi

Page 3: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

Raju Gandhi @Looselytyped

Page 4: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

IDEMPOTENCY!

Page 5: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread
Page 6: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

VARIABLES

Page 7: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

VARIABLES

SAY WHAT NOW?

Page 8: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

VARIABLES VARIABLE SPREAD

USE CONSISTENT NAMING

USE SCALAR FORMAT

Page 9: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

USE GROUP_VARS MORE THAN HOST_VARS

Page 10: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

VARIABLES VARIABLE SPREAD

USE CONSISTENT NAMING

USE SCALAR FORMAT

Page 11: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

VARIABLES VARIABLE SPREAD

USE CONSISTENT NAMING

USE SCALAR FORMAT

Page 12: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

# avoid java: Xmx: 512m Xms: 256m

# use java_Xmx: 512m java_Xms: 256m

Page 13: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

NAME ALL THE TASKS!TIP

Page 14: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

PROJECT LAYOUT

Page 15: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

START SIMPLE

Page 16: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

. ├── environments/ │   └── localhost/ │   ├── group_vars/ │   │   ├── all │   │   └── app │   └── inventory ├── playbooks/ ├── roles/ │   ├── tomcat/ │   └── java/ └── ansible.cfg

Page 17: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

START SIMPLEThen Refactor

Page 18: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

. ├── environments/ │   └── localhost/ │   ├── group_vars/ │   │   ├── all │   │   └── app │   └── inventory ├── playbooks/ ├── roles/ │   ├── requirements.yml │   ├── external/ │   └── internal/ └── ansible.cfg

Page 19: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

ROLES LAYOUT

Page 20: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

> ansible-galaxy init <role-name> <role-name> ├── defaults/ │   └── main.yml ├── files/ ├── handlers/ │   └── main.yml ├── meta/ │   └── main.yml ├── tasks/ │   └── main.yml ├── templates/ ├── tests/ │   ├── inventory │   └── test.yml ├── vars/ │   └── main.yml └── README.md

Page 21: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

> ansible-galaxy init <role-name> <role-name> ├── defaults/ │   └── main.yml ├── files/ ├── handlers/ │   └── main.yml ├── meta/ │   └── main.yml ├── tasks/ │   └── main.yml ├── templates/ ├── tests/ │   ├── inventory │   └── test.yml ├── vars/ │   └── main.yml └── README.md

Page 22: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TIPUSE THE VERBOSITY FLAG FOR DEBUG

Page 23: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

PLAYBOOKS

Page 24: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

PLAYBOOKS SIMPLE

PRE_TASKS, TASKS, POST_TASKS

USE ROLES INSTEAD

Page 25: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

PLAYBOOKS SIMPLE

PRE_TASKS, TASKS, POST_TASKS

USE ROLES INSTEAD

Page 26: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

PLAYBOOKS SIMPLE

PRE_TASKS, TASKS, POST_TASKS

USE ROLES INSTEAD

Page 27: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

PLAYBOOKS SIMPLE

PRE_TASKS, TASKS, POST_TASKS

USE ROLES INSTEAD

Page 28: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TIPAVOID COMMAND AND SHELL MODULES

Page 29: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

ROLES

Page 30: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

ROLES SMALL

DO ONE THING

PREFIX VARIABLES WITH ROLE NAME

LIMIT ROLE DEPENDENCIES

IDEMPOTENT!

Page 31: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TIPROLES API

are yourVARIABLES

Page 32: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

FILES/TEMPLATES

Page 33: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

FILES / TEMPLATES

TEMPLATE ALL* THE FILES

USE ANSIBLE_MANAGED

REFLECT DEST PATH IN SOURCE

Page 34: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

FILES / TEMPLATES

TEMPLATE ALL* THE FILES

USE ANSIBLE_MANAGED

REFLECT DEST PATH IN SOURCE

Page 35: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

FILES / TEMPLATES

TEMPLATE ALL* THE FILES

USE ANSIBLE_MANAGED

REFLECT DEST PATH IN SOURCE

Page 36: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

USE ONLY STATIC VALUES IN ANSIBLE_MANAGED

TIP

Page 37: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

FILES / TEMPLATES

TEMPLATE ALL* THE FILES

USE ANSIBLE_MANAGED

REFLECT DEST PATH IN SOURCE

Page 38: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TAGS

Page 39: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TAGS HIGH LEVEL

TOO MANY TAGS

1 ROLE => 1-2 TAGS

Page 40: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TAGS HIGH LEVEL

TOO MANY TAGS

1 ROLE => 1-2 TAGS

Page 41: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TAGS HIGH LEVEL

TOO MANY TAGS

1 ROLE => 1-2 TAGS

Page 42: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TAGS HIGH LEVEL

TOO MANY TAGS

1 ROLE => 1-2 TAGS

Page 43: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

CODE FORMATTING

Page 44: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

CODE FORMATTING

USE FULL YAML NOTATION

TRUE/FALSE OR YES/NO

USE WITH_* TO AVOID DUPLICATION

DEFINE/ENFORCE A STYLE GUIDE

Page 45: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

CODE FORMATTING

USE FULL YAML NOTATION

TRUE/FALSE OR YES/NO

USE WITH_* TO AVOID DUPLICATION

DEFINE/ENFORCE A STYLE GUIDE

Page 46: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

# avoid - name: Change ownership of Tomcat installation file: path=/usr/share/tomcat/ owner=tomcat group=tomcat state=directory recurse=yes

# use - name: Change ownership of Tomcat installation file: path: /usr/share/tomcat/ owner: tomcat group: tomcat state: directory recurse: yes

Page 47: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

CODE FORMATTING

USE FULL YAML NOTATION

TRUE/FALSE OR YES/NO

USE WITH_* TO AVOID DUPLICATION

DEFINE/ENFORCE A STYLE GUIDE

Page 48: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

CODE FORMATTING

USE FULL YAML NOTATION

TRUE/FALSE OR YES/NO

USE WITH_* TO AVOID DUPLICATION

DEFINE/ENFORCE A STYLE GUIDE

Page 49: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

# avoid - name: Create required application etc directory file: path: /DATA/app/etc state: directory mode: 0755 - name: Create required application certs directory file: path: /DATA/app/certs state: directory mode: 0755 # use - name: Create required application directories file: path: /DATA/app/{{ item }} state: directory mode: 0755 with_items: - etc - "certs/{{ env }}"

Page 50: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

ANSIBLE 2.5+ USE LOOP

Page 51: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

CODE FORMATTING

USE FULL YAML NOTATION

TRUE/FALSE OR YES/NO

USE WITH_* TO AVOID DUPLICATION

DEFINE/ENFORCE A STYLE GUIDE

Page 52: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

OPTIMIZE FOR READABILITY

Page 53: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

TOOLS

Page 54: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

> ansible-playbook -i environments/localhost playbooks/010-echo.yml --syntax-check

Page 56: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

> pip2 install ansible-lint > # Install ansible-lint globally > > ansible-lint 020-dnf-nginx.yml [ANSIBLE0010] Package installs should not use latest 020-dnf-nginx.yml:8 Task/Handler: Install nginx

Page 58: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

USES ANSIBLE-LINT

Page 59: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

> pip install ansible-review > # Install ansible-lint globally > > find . -type f | xargs ansible-review WARN: Best practice "Commands should be idempotent" not met: ./010-echo.yml:8: [ANSIBLE0012] Commands should not change things if nothing needs doing WARN: Best practice "YAML should be correctly indented" not met: ./010-echo.yml:8: lines starting with '- ' should have same or less indentation than previous line

Page 61: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

> pip install ansible-inventory-grapher > # Install ansible-inventory-grapher globally > > ansible-inventory-grapher \ -i environments/localhost multi \ --format “multi.dot" > > dot -Tpng multi.dot

Page 62: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread
Page 63: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

CREDITS

RESOURCES

Theme - Dynamic Static Site Strategies by Phil Hawksworth

https://www.ansible.com/ansible-best-practices

Page 64: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread

THANKS!

@looselytyped

Page 65: ANSIBLE (BEST) PRACTICES - DeveloperMarch · ansible (best) practices raju gandhi. raju gandhi @looselytyped. idempotency! variables. variables say what now? variables variable spread