Is "environment" a reserved variable name?

Hi,

When I set the variable name “environment” and use it in a playbook, it is empty. Is it a reserved variable name?

Regards,
Joost

Environment is a keyword for playbooks/tasks, however I don't think there's
a direct issue with using it as a variable name. To be safe, you should
probably use a different variable name anyway as it could be confused. If
you change the name and still have issues, please post a sample of the
playbook you're having problems with as well as information about the
ansible version you're running (the output of "ansible --version").

See http://www.ansibleworks.com/docs/playbooks_environment.html for more
info about setting environment variables, if that's what you're trying to
do.

Well, I am using the word environment in the sense of “deployment environment”, but the term may be a bit general. Anyway, this is the playbook:

test.yml:

Ahh, you can’t do multiple “-e” options, just combine them: -e “foo=bar,environment=qux”

Are you sure? This makes the output change into:

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [debug msg="{{environment}}"] *******************************************
ok: [localhost] => {"msg": "{}"}

TASK: [debug msg="bar,environment=qux"] ***************************************
ok: [localhost] => {"msg": "bar,environment=qux"}

PLAY RECAP ********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0

But when I remove foo from the equation, environment is still empty:

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [debug msg="qux"] *******************************************************
ok: [localhost] => {"msg": "{}"}

PLAY RECAP ********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

Regards,
Joost

Yes, environment is a restricted keyword. Initially specific within playbooks, but it gets injected in the runner:

inject[‘environment’] = self.environment

so it will erase whatever value was set by inventory of vars.

Oops, my mistake, the variables should be space-separated, not comma-separated. So -e “foo=bar environment=qux”

Thanks for the info. What is the runner environment?

And could I have found this out myself?

Regards,
Joost

> Yes, environment is a restricted keyword. [...]

Thanks for the info. What is the runner environment?

An ansible internal, it's the basic building block that "runs a task on a
host"

And could I have found this out myself?

​Depends if you are familiar with the ansible source code. I basically did
a "grep environment​ lib/ -r" to double check this, you can find it in
lib/ansible/runner/__init__.py

​If you have further questions on code, be sure to switch to the
ansible-devel mailing list.

Serge​

​That is actually quite confusing. Even as i use this a lot and have been
for quite a while, I keep forgetting these details myself.
I wonder what was the rationale to not allow for multiple -e options, which
seem ​a lot easier to me.

Serge

> Yes, environment is a restricted keyword. [...]

Thanks for the info. What is the runner environment?

An ansible internal, it's the basic building block that "runs a task on a
host"

So is this an internal structure that is leaking out? Or is this
something that could be interesting for a task?

And could I have found this out myself?

Depends if you are familiar with the ansible source code. [...]

If you have further questions on code, be sure to switch to the
ansible-devel mailing list.

Although the Ansible code is interesting, I was asking from a user's
perspective. I was wondering if I overlooked some part of the
documentation.

Regards,
Joost

So is this an internal structure that is leaking out? Or is this
something that could be interesting for a task?

Not really leaking, no, it's just the structure that is using 'environment'
and hence restriciting it for use to end users.
​​

Although the Ansible code is interesting, I was asking from a user's
perspective. I was wondering if I overlooked some part of the
documentation.

Sorry, I overshoot that one :slight_smile:

I see it's mentioned here:

http://www.ansibleworks.com/docs/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts
​​

Serge

Thanks for the link, Serge!

Joost

Let me correct this:

-e “foo=bar env=qux”

It’s key value pairs, just like the rest of ansible.

Also, don’t use environment, as it’s a reserved word :slight_smile:

I think it would be nice if the system raised a warning when a variable named env was set, someone should perhaps file a ticket so we can add that :slight_smile:

I am assuming you meant "environment" instead of "env", as the latter
is not reserved and created a ticket:

https://github.com/ansible/ansible/issues/4816

Hope I got that right. Thanks!

Regards,
Joost

yep, thanks!