ansible / molecule

Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
https://ansible.readthedocs.io/projects/molecule/
MIT License
3.87k stars 659 forks source link

replace molecule.yml file with Ansible vars/defaults #3921

Closed ssbarnea closed 1 year ago

ssbarnea commented 1 year ago

Instead of using a molecule.yml file format that is not directly readable by ansible for defining a test scenario, we plan to remove the need for one. All the extra configuration needed should be doable using vars/ or defaults/ near the testing playbook.

If we are successful with this approach, the simple presence of a playbook file inside a specific location on disc, would mark it as a valid molecule scenario.

Our plan is to keep the command line "api" compatible with older version, so user could still run the same basic command.

apatard commented 1 year ago

Not really an objection but imho using a molecule.yml file is providing clear separation between test definition and test environment/variables. Using vars file will make things "blurry". Moreover, when looking that a git repository content, if I see a directory with a molecule.yml, it's clear to me that it's a molecule test scenario. Not sure how my limited mind will be able to do that without having to look at the file content.

I'm also a little bit afraid on how ansible will search the vars files (I'm thinking on troubles when using import_playbooks and search paths) but I guess it should not be a big worry.

geerlingguy commented 1 year ago

I have a similar concern as @apatard regarding the clean separation of test environment configuration. It would be nice to not have that mixed in.

robertdebock commented 1 year ago

I think the proposal is to move some Molecule configurations to vars and defaults in the molecule/* directory. You'd end up with this structure.

vars
vars/main.yml
tasks
tasks/main.yml
defaults
defaults/main.yml
molecule
molecule/default
molecule/default/vars
molecule/default/vars/main.yml # <- This is the new file.
molecule/default/defaults
molecule/default/defaults/main.yml # <- This is also new.

I'm not sure if I'm understanding this correctly though.

geerlingguy commented 1 year ago

@robertdebock ahh... if that's the case, then I don't have an issue, that could make more sense conceptually. Though maybe it would be in molecule/default/meta/main.yml since it's not pertinent to an Ansible playbook structure, but rather the tool that is running the playbook?

Not sure... for Ansible itself, we're used to an ansible.cfg file.

kksat commented 1 year ago

I do not quite understand why do you say that molecule.yml is not readable by ansible. molecule.yml structured exactly as var file. You can even import it with include_vars, no? After import variables: dependency, driver, platforms, scenario, provisioner, verifier will be defined. By default all these variables are available in molecule in molecule_yml dictionary. So what difference will it make to have basically same variables in differently named files (defaults/main.yml and vars/main.yml)?

If we are successful with this approach, the simple presence of a playbook file inside a specific location on disc, would mark it as a valid molecule scenario.

Making all the playbooks molecule test scenarios? This does not sound right to me. Not every playbook is a test scenario, with molecule.yml every folder is scenario.

How these commands are different when?

molecule test -s <scenarion_name>
ansible-playbook <scenarion_name>.yml

Could you please also elaborate on relationships between defaults/main.yml and vars/main.yml variable files in your sugession? I am very interested to have default values for certain molecule.yml parameters. So deep down I like this suggestion.

At the moment we have implemented following as part of create_azure playbook in our molecule driver.

  tasks:
    - name: Include default variables
      ansible.builtin.include_vars:
        file: azure/defaults/main.yml

This includes default values from collection (not from molecule scenario). So if anyone wants to change default values, he(she) has to change collection - and this is not perfect. And I am looking for solution.

ssbarnea commented 1 year ago

Molecule also have a default molecule yml file that can be used to define implicit values. If one uses Ansible to load the molecule.yml it will not have the implicit values loaded.

Anyway this change is likely to remain on hold for now due to lack of time combined with its breaking nature.

ssbarnea commented 1 year ago

I will close the ticket for now as wonfix.