I am trying develop a cookbook to make a flask app work with gunicorn and nginx. I have been successful to the point that the app is running very well with a local sqlite database, see my cookbook at https://github.com/harrywang/flasky-cookbook. The flask app uses environment variables for sending emails such as: MAIL_USERNAME = os.environ.get('MAIL_USERNAME'), how I can pass those environment variables to the ubuntu virtual machines using test kitchen during kitchen converge?
Asked
Active
Viewed 8,627 times
8
-
NOTE: when use env in gunicorn.conf `env MAIL_USERNAME="AKIAJ6NFIS3"` double quotes must be used - single quotes do not work, while using --env single quotes work - to be safe: always use double quotes. – dami.max Jan 28 '16 at 17:12
2 Answers
18
You can use Erb formatting in your .kitchen.yml
:
provisioner:
name: chef-solo
attributes:
mycookbook:
mail_username: <%= ENV['MAIL_USERNAME'] %>
And then use node['mycookbook']['mail_username']
in your recipe to pass the value to the application.

coderanger
- 52,400
- 4
- 52
- 75
-
I am confused: how can I "use node['mycookbook']['mail_username'] in your recipe to pass the value to the application"? Is your solution passing my host (macbook pro) env variables to guest (ubuntu virtual box instance)? I am very new to Chef - sorry if I am asking stupid questions :) – dami.max Jan 26 '16 at 02:01
-
You'll need to fork the `flasky` cookbook and add something to this template file: https://github.com/harrywang/flasky-cookbook/blob/master/templates/default/flasky-gunicorn.conf.erb to set the environment variable. – coderanger Jan 26 '16 at 02:26
-
Also check out https://github.com/poise/application_python/blob/master/test/cookbooks/application_python_test/recipes/flask.rb for a simpler Flask deployment example. – coderanger Jan 26 '16 at 02:26
-
thanks a lot for your help. adding something to the template is similar to the solution given by zuazo. The application cookbook seems great but do you know any detailed tutorials on that? The README is too brief for beginners like me. – dami.max Jan 26 '16 at 13:57
-3
There is no way to pass environment variables using .kitchen.yml configuration file (see test-kitchen/test-kitchen#662 issue).
I recommend you to set the environment variables in the gunicorn.conf.erb template using the --env
argument:
exec gunicorn --env SECRET_KEY=<%= @secret_key %> --env [...] --workers 3 --bind unix:<%= node['flasky-cookbook']['gunicorn_socket'] %> -m 007 --log-file <%= node['flasky-cookbook']['gunicorn_logfile']%> manage:app

zuazo
- 5,398
- 2
- 23
- 22
-
I tried `exec gunicorn --env MAIL_SERVER='email-smtp.us-east-1.amazonaws.com',MAIL_USERNAME='',MAIL_PASSWORD='-redacted-',FLASK_ADMIN='-redacted-' --workers 3 --bind unix:<%= node['flasky-cookbook']['gunicorn_socket'] %> -m 007 --log-file <%= node['flasky-cookbook']['gunicorn_logfile']%> manage:app` but did not work - I `kitchen login` and `printenv` but cannot see those env variables. Any thought? – dami.max Jan 26 '16 at 01:58
-
@coderanger I don't think this is entirely incorrect. I know that you can read ENV variables from the *.kitchen.yml* file, but I was talking about the other part of the problem: passing the variable to the application. I assumed, perhaps wrongly, that he knew how to do the first part. If you still think this answer is wrong, I will delete it. – zuazo Jan 26 '16 at 02:32
-
@harryw [seems like you can also use `env` to set environment variables](http://stackoverflow.com/a/33653041/3284723). These variables will only be defined inside the application run with gunicorn. Not in your kitchen shell. – zuazo Jan 26 '16 at 02:47
-
I was mostly referring to the first part, you can absolutely pass through environment variables. – coderanger Jan 26 '16 at 02:52
-
OK, @coderanger. Thanks. I removed the first part. I didn't know that, sorry. I know, your answer is better :-P – zuazo Jan 26 '16 at 02:58
-
@zuazo thanks a lot for your help. how can I check those env variables have been successfully passed in if I cannot use printenv? Is my way of passing multiple env variable by putting them together with comma correct? I am asking because I could not make flasky email function to work :(. – dami.max Jan 26 '16 at 13:59
-
@harryw You are welcome ;-) You can get the pid of the gunicorn process and look at its environment *file* in `/proc`. For example with: `cat /proc/$(pidof gunicorn)/environ`. For the variables, try passing multiple `--env` options or use multiple `env SECRET_KEY="..."` lines before calling gunicorn. – zuazo Jan 26 '16 at 14:07