Bootstrap FreeKB - Ansible - Getting Started with Collections
Ansible - Getting Started with Collections

Updated:   |  Ansible articles

Collections contain the various modules and plugins that can be used. For example, the file module is part of the ansible.builtin collection, whereas the archive module is part of the community.general collection.

When using a module or collection in a playbook, the full path to the collection and module can be specified. In this example, the full path ansible.builtin.file is used.

---
- hosts: localhost
  tasks:
  - name: mkdir /tmp/example
    ansible.builtin.file:
      path: /tmp/example
      state: directory
...

 

Or, as long as the collection is installed, just the name of the module or plugin can be used. In this example, just file is used instead of ansible.builtin.file.

---
- hosts: localhost
  tasks:
  - name: mkdir /tmp/example
    file:
      path: /tmp/example
      state: directory
...

 

If you attempt to use a module or plugin that is part of a collection that has not been installed, something like this should be returned.

ERROR! couldn't resolve module/action 'community.general.xml'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/home/john.doe/testing.yml': line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - community.general.xml:
    ^ here

 

The ansible-galaxy collection install command can be used to install a collection. By default, this will install the collection to /home/username/.ansible/collections/ansible_collections/.

ansible-galaxy collection install community.general

 

The collections_path or collections_paths directive can be used to specify the directory or directories that Ansible will use for collections.

[defaults]
collections_path = /usr/local/ansible/collections,/usr/local/ansible_testing/collections

 

Or the ANSIBLE_COLLECTIONS_PATH or ANSIBLE_COLLECTIONS_PATHS variable can be used.

export ANSIBLE_COLLECTIONS_PATH = /usr/local/ansible/collections,/usr/local/ansible_testing/collections

 

And then the ansible --version command can be used to see the directory that Ansible will use for collections.

]$ ansible --version
ansible [core 2.14.0]
  ansible collection location = /usr/local/ansible/collections,/usr/local/ansible_test/collections

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 667dfb in the box below so that we can be sure you are a human.