Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Gain access to role vars in Goss test files #3

Closed
decentral1se opened this issue Oct 25, 2019 · 6 comments
Closed

Gain access to role vars in Goss test files #3

decentral1se opened this issue Oct 25, 2019 · 6 comments

Comments

@decentral1se
Copy link
Member

Transferring ansible/molecule#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
Copy link
Contributor

t2d commented Mar 27, 2020

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
Copy link
Collaborator

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

@t2d
Copy link
Contributor

t2d commented Apr 1, 2020

@jobcespedes
Copy link

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
Copy link
Collaborator

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

Can you verify?

@t2d
Copy link
Contributor

t2d commented Jan 22, 2021

Awesome, that works like a charm! systemli/ansible-role-sshd@abc6136 Thanks @markwell-ch !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants