TExcelTAD
Home › Configuration Management: Ansible › Collections & FQCNs

Collections & FQCNs

Since Ansible 2.10, modules, plugins and roles are distributed in collections: versioned packages named namespace.collection, such as community.general, ansible.posix or amazon.aws. A module's full name is its FQCN (fully qualified collection name): namespace.collection.module.

FQCN Collection What
ansible.builtin.copy ansible.builtin (part of ansible-core) core modules
ansible.posix.sysctl ansible.posix POSIX system modules
community.general.ufw community.general hundreds of community modules
community.docker.docker_container community.docker Docker
kubernetes.core.k8s kubernetes.core Kubernetes objects
amazon.aws.ec2_instance amazon.aws AWS

Why always write the FQCN

Short names (copy, ufw) are looked up through a search list, and two collections can contain a module with the same short name. The FQCN is unambiguous, documents where the module comes from, and is what ansible-lint requires (rule fqcn). This course uses FQCNs everywhere.

What is installed

bash · try it yourself
ansible-galaxy collection list 2>/dev/null | grep -E '^(ansible\.(posix|utils)|community\.(general|docker|crypto)|kubernetes\.core|amazon\.aws) '
echo "=== modules in ansible.posix"
ansible-doc -l ansible.posix | cut -c1-60
amazon.aws                               11.4.0
ansible.posix                            2.2.2
ansible.utils                            6.1.1
community.crypto                         3.5.0
community.docker                         5.4.0
community.general                        13.5.0
kubernetes.core                          6.6.0
=== modules in ansible.posix
ansible.posix.acl                Set and retrieve file ACL i
ansible.posix.at                 Schedule the execution of a
ansible.posix.authorized_key     Adds or removes an SSH auth
ansible.posix.firewalld          Manage arbitrary ports/serv
ansible.posix.firewalld_info     Gather information about fi
ansible.posix.mount              Control active and configur
ansible.posix.patch              Apply patch files using the
ansible.posix.rhel_facts         Facts module to set or over
ansible.posix.rhel_rpm_ostree    Ensure packages exist in a
ansible.posix.rpm_ostree_upgrade Manage rpm-ostree upgrade t
ansible.posix.seboolean          Toggles SELinux booleans
ansible.posix.selinux            Change policy and state of
ansible.posix.synchronize        A wrapper around rsync to m
ansible.posix.sysctl             Manage entries in sysctl.co

Collections are searched in collections_path (default ~/.ansible/collections and /usr/share/ansible/collections) and in the Python environment of the ansible package, which is where this sandbox's bundle lives. A version installed into ./collections/ of your project wins when you put it first in collections_path.

Using a collection module

Nothing to import: write the FQCN.

bash · try it yourself
cat > crypto.yml <<'YML'
- name: Modules from collections
  hosts: localhost
  gather_facts: false
  tasks:
    - name: An ed25519 key pair (community.crypto)
      community.crypto.openssh_keypair:
        path: deploy_key
        type: ed25519
        comment: deploy@ci

    - name: A key=value file edit (community.general)
      community.general.ini_file:
        path: /home/learner/app.ini
        section: server
        option: workers
        value: "4"
        mode: "0644"
YML
ansible-playbook crypto.yml | grep -E '^(TASK|ok|changed)'
ls deploy_key*; cat app.ini; cut -d' ' -f1,3 deploy_key.pub
TASK [An ed25519 key pair (community.crypto)] **********************************
changed: [localhost]
TASK [A key=value file edit (community.general)] *******************************
changed: [localhost]
deploy_key
deploy_key.pub

[server]
workers = 4
ssh-ed25519 deploy@ci

The openssh_keypair module generated the key with Python's cryptography library, because the sandbox has no ssh-keygen; on a normal host it uses whichever backend is available. One trap found while writing this lesson: ini_file with a relative path that doesn't exist yet fails with No such file or directory: '' in this collection version. Use absolute paths in tasks ({{ playbook_dir }}/… or a variable), as real roles do anyway.

The collections: keyword

A play or role can declare collections: [community.general] to make short names resolve there. It saves typing but brings back the ambiguity; prefer FQCNs.

Structure of a collection

When your team shares several roles plus custom modules or filters, package them as your own collection:

text
acme/platform/            # namespace acme, collection platform
├── galaxy.yml            # name, version, dependencies, authors
├── roles/
│   ├── nginx_site/
│   └── hardening/
├── plugins/
│   ├── modules/          # custom modules: acme.platform.my_module
│   └── filter/           # custom filters: acme.platform.my_filter
├── playbooks/            # playbooks callable as acme.platform.site
└── meta/runtime.yml      # requires_ansible, redirects, deprecations

ansible-galaxy collection init acme.platform creates the skeleton, ansible-galaxy collection build makes acme-platform-1.2.0.tar.gz, and you publish it to Galaxy, to a private Automation Hub, or simply install it from Git. Roles inside it are used as acme.platform.hardening.

Test yourself

What is the FQCN of the copy module?

  1. copy
  2. ansible.copy
  3. ansible.builtin.copy
  4. builtin.copy

Why write FQCNs instead of short module names?

  1. They run faster
  2. They are unambiguous when two collections have a module of the same name
  3. Short names are removed
  4. Galaxy requires them

Which file describes a collection's name, version and dependencies?

  1. meta/main.yml
  2. galaxy.yml
  3. requirements.yml
  4. ansible.cfg