Search code examples
automationansibledevopsansible-role

Common use case of ansible roles


I just initialize ansible project and not sure which approach is better to create ansible roles. Should I create one usecase per roles. Or it is better to create multiple usecase inside one roles.

For example, I have this multiple roles which represent each usecase (install, add_client) in same context (VPN).

- roles
  - vpn_install
    - tasks
      - main.yml
  - vpn_add_client
     - tasks
       - main.yml
      

or I have this role directory which contains multiple usercase (install, add client) and will be imported in main.yml. and each usecase will be seperated by tags. So I can run it individually by only specifying the tags.

- roles
  - vpn
    - tasks
      - main.yml
      - installation
        - install.yml
      - add_vpn_client
        - add-vpn-client.yml

      

Which one is better for long term project (project size will be increasing overtime) or which one is most common to be used?


Solution

  • By definition

    Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.

    You can define a role by each usecase you have, when you have in this way you can reuse it and share it. If you use a role with multiples playbooks and use tags to execute the use case you need It wont be maintainable overtime or it'll be so difficult. In my experience to have a role by each use case it makes easier changes that you could have.

    In this link you can find best practices for managing roles.

    https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html