layout: true name: title_layout class: title_slide --- layout: true name: section_layout class: section_slide --- layout: true name: agenda_layout class: content_slide agenda_slide --- layout: true name: thank_you class: thank_you --- layout: true name: content_layout class: content_slide --- template: title_layout # Collections in Ansible 2.9+ Daniel Mellado - Senior Software Engineer - Red Hat
Ansible Automates Zagreb, 11.12.2019
--- template: agenda_layout # Agenda 1. What's a Collection? 3. Demo Time! Slides available at: **https://danielmellado.github.io/ansible_automates_zagreb** --- template: section_layout What's a Collection? --- # Collections ## What's a Collection? * A new wat to distribute Ansible content * An artifact format for consistent content structure * Contains 1-N of roles, modules, plugins and module utilities * Enables versioning of external content * Enables consistent delivery independent of Ansible distributions * Installable as system, user or project resources * Immediate use of the content found within the artifact * Namespacing support built into the collection --- # Collections ## What problems do collections solve?: * Difficult distribution of non-role content -- * Plugin/role name collisions -- * Difficult code-sharing for most plugins --- # Collections ## Problem 1: Content Distribution * Multiple roles with deps? Good luck. * Roles already distribute plugins- done? * Requires role to be "entered" (see also ansible-wtf) * Plugins are not "first-class citizens" * Versioning/maintenance issues --- # Collections ## Content Distribution with Collections * Collection may contain 1-N: * Roles/Modules/Plugins * Playbooks (future/TBD) * Collection is the unit of distribution and versioning * Installable: * globally * per-user * content-adjacent (ie per-project) --- # Collections ## Installing collections content * Default paths (searched in this order, configurable) * (current playbook)/collections * ~/.ansible/collections * usr/share/ansible/collections * Mazer/ansible-galaxy * Content Hub --- # Collections ## Problem 2: Name Collisions * Ansible has a global(ish) flat plugin namespace * Role1 and Role2 both distribute my_module * Which one will get used? (ha!) * Can I use them both in the same run? (no * What if core later includes a my_module? (screwed) * What if the project has in library/ ? * What if the user has in ~/.ansible/? * What if the system has in /usr/share/ansible ? * Issue exists for roles and all modules/plugins --- # Collections ## Namespacing with Collections * Collections have a namespace and name * For distribution, namespace == Galaxy user/org * Content in collections is accessed by namespace * mynamespace.mycollection.my_module * f5.bigip.provisioning_role * Built-in collections * ansible.builtin * ansible.legacy --- # Collections ## Sample Playbook ```yaml - hosts: somehosts tasks: - myns.mycollection.athing: - ansible.builtin.ping: # use only the ping packaged in core - ansible.legacy.ping: # use core or library(etc)/ping.py when: thing | myns.mycollection.filter == 42 - ping: # still works, == ansible.legacy.ping: ``` --- template: section_layout That's awfully verbose --- # Collections ## Enter the collections keyword ```yaml - hosts: somehosts collections: - ansible.builtin - myns.mycollection - otherns.othercollection tasks: - mymodule: # first found in the list of collections - otherns.othercollection.mymodule: # fully-qualified is fine ``` --- # Collections ## Problem 3: Code Sharing * module_utils only works for modules * unless you're "in the box" * perverse incentives * eg, Azure/AWS want to share utility code between * modules/actions/inventory/lookup plugins * Just use pip! --- # Collections ## Code sharing in Collections * Entire collection is a Python namespace package * All installed collection content is accessible * even across collections! * `ansible_collections` root Python namespace * `ansible_collections.mynamespace.mycollection` --- template: section_layout Demo time! --- template: section_layout  .medium-text[ Slides are available on http://creativecommons.org/licenses/by/4.0/ ] --- template: thank_you # Q&A ## Thank you! **Slides can be found at:** - https://github.com/danielmellado/ansible_automates_zagreb **Daniel Mellado**  [dmellado@redhat.com](mailto:dmellado@redhat.com)  [dmellado (freenode)](irc://chat.freenode.net/dmellado,isnick)