How to keep collection alongside playbook and push to own github repo?

Viewed 364

My current playbook is structured this way

projectroot
 |
 |--ubuntu2004
       |
       |--00_setup
             |
             |--vars
             |--playbook.yml
             |--readme.md

Because my playbook uses ansible.posix and I also commit my playbook into a github repo. I was hoping if there's a way to include the required collection in this case ansible.posix as a requirement and how do I install it?

I saw that there are multiple ways https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#installing-collections

I was wondering what's the best practice way that makes sense when using a github repository as version control for the playbook?

1 Answers

There's a few ways that you could do this. I'd suggest a requirements file since it's the easiest to set up and to manage.

  1. Create a requirements file that you use to install the required modules.

    The best way would be to create a requirements file that references the collection(s) your playbook needs. Which you can then use to install the required collection(s) and / or role(s).

    ---
    collections:
      - name: ansible.posix
    
  2. Store the module in a repository and install it through ansible-galaxy.

    You could upload the module to a git repository and then install it. I wouldn't recommend this as storing dependencies in your source-code isn't considered a good practise when there's a tool to manage dependencies available.

    ansible-galaxy collection install 
    git+https://github.com/organization/repo_name.git,devel
    
  3. Install the module through a playbook

    I've set up a master node to run playbooks on with Ansible before and installed modules through the command task in a playbook. As long as you don't reference / include any tasks or plays that use the module, this will work fine.

    - name: Install ansible posix module
      command: ansible-galaxy collection install ansible.posix
    

The ideal way would be to have to playbook install the required module(s) prior to executing the tasks. However it's not possible to ignore the error that is thrown when a module is missing, so you would have to have to create a play that doesn't include / reference any of the tasks using the module.

Related