Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use dotenv with ansible? #175

Closed
npearson72 opened this issue Mar 11, 2015 · 6 comments
Closed

Is it possible to use dotenv with ansible? #175

npearson72 opened this issue Mar 11, 2015 · 6 comments

Comments

@npearson72
Copy link

I currently have variables in a yaml file that ansible uses. I'd like to consolidate all my variables into a common format, and am looking into dotenv. Does anyone know if it's possible for ansible to access the dotenv variables?

In the documentation it mentioned using the dotenv executable. Is that a way to instantiate the variables in memory for whatever script I'm running -- even if it's not a ruby script?

@nickhammond
Copy link

Ansible is very flexible and powerful with the way it uses variables. I love dotenv but I wouldn't recommend it for managing variables with Ansible. There's a huge advantage to using Ansible's Vault as well as the various ways to set and load variables. If the variables are specific to an application you could use host specific variables or group specific variables which is what I typically use.

@bkeepers
Copy link
Owner

I've never used Ansible, so I'll defer to @nickhammond.

You can use the dotenv executable to set variables for any other command:

dotenv -f path/to/.env other-command

@nickhammond
Copy link

@npearson72 If you do end up needing to do this, you'll just need to use the lookup method(example), something like this:

.env

FOO=bar

ansible playbook:

debug: var={{ lookup('env', 'FOO') }}
$ dotenv ansible-playbook playbook.yml -i inventory

@npearson72
Copy link
Author

I've bee really busy lately, so have just know had a chance to try this out. Works like a charm. Thanks guys!

@wayne-o
Copy link

wayne-o commented Aug 31, 2020

I get this:

vagrant@ubuntu-bionic:~/init-cluster/ansible$ dotenv ansible-playbook -i hosts.yml --become --become-user=root finalise-cluster.yml
Usage: dotenv [OPTIONS] COMMAND [ARGS]...

Error: No such command "ansible-playbook".

@nickhammond
Copy link

@wayne-o Does running just ansible-playbook work fine? Can you run dotenv --version or provide some additional environment information?

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

No branches or pull requests

4 participants