ansible-community / molecule-goss

Molecule Goss Verifier
https://github.com/ansible-community/molecule-plugins
MIT License
12 stars 11 forks source link

Gain access to role vars in Goss test files #3

Closed decentral1se closed 3 years ago

decentral1se commented 4 years ago

Transferring https://github.com/ansible/molecule/issues/2375 to here.


We need to pass the role defaults/main.yml and vars/main.yml into the verifier (like goss). If I have in my defaults/main.yml:

---
myfile: /etc/foo

And then a Goss test like:

---
file:
    /etc/foo:
        exists: true

My tests and my vars can quickly get out of sync when I change my vars.

How can we receive vars from Ansible in the context of Molecule?

t2d commented 4 years ago

I have been using j2cli for this purpose. Since I want to use the goss test anyway in production, I have it as a template and the vars in default/main.yml

I can then update my development goss tests by running (e.g. in nginx role)

j2 --customize ~/custom-j2.py templates/test_nginx.yml.j2 defaults/main.yml > molecule/default/tests/test_default.yml

We just need to adapt j2cli to the ansible jinja2 config

# custom-j2.py
def j2_environment_params():
    """ Extra parameters for the Jinja2 Environment """
    # Jinja2 Environment configuration
    # http://jinja.pocoo.org/docs/2.10/api/#jinja2.Environment
    return dict(
        # Remove whitespace around blocks
        trim_blocks=True,
    )

Does this somehow help? Can it be integrated into molecule?

belfast77 commented 4 years ago

@t2d have you got a working example on Github that we can take a look at please ?

t2d commented 4 years ago

https://github.com/systemli/ansible-role-sshd

jobcespedes commented 4 years ago

Would it be a good idea to put a verifier task as a yml file inside role tasks? Then you can execute it with molecule verify.yml. In that way you could access inventory and role vars. Also run it in other deployments.

abtreece commented 3 years ago

@decentral1se I think it's possible this request was addressed by @markwell-ch via PR #13

Can you verify?

t2d commented 3 years ago

Awesome, that works like a charm! https://github.com/systemli/ansible-role-sshd/commit/abc61369364d42416e4958d18da6e705985250d0 Thanks @markwell-ch !