Is "environment" a reserved variable name?

2,814 views
Skip to first unread message

Joost Cassee

unread,
Nov 4, 2013, 9:06:36 AM11/4/13
to ansible...@googlegroups.com
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

James Cammarata

unread,
Nov 4, 2013, 9:35:48 AM11/4/13
to ansible...@googlegroups.com
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

James Cammarata <jcamm...@ansibleworks.com>
Sr. Software Engineer, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Joost Cassee

unread,
Nov 4, 2013, 9:42:36 AM11/4/13
to ansible...@googlegroups.com
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:
---
- hosts: all
  tasks:
  - debug: msg="{{ environment }}"
  - debug: msg="{{ foo }}"

This is the inventory:

hosts:
[test]
localhost

When I run this command:
ansible-playbook test.yml -i hosts -c local -e foo=bar -e environment=qux

This is the output:

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

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

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

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

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

Note that the variable is interpolated in the debug task header!

I am using Ansible 1.3.4.

Regards,
Joost

Op maandag 4 november 2013 15:35:48 UTC+1 schreef James Cammarata:

James Cammarata

unread,
Nov 4, 2013, 9:49:07 AM11/4/13
to ansible...@googlegroups.com
Ahh, you can't do multiple "-e" options, just combine them: -e "foo=bar,environment=qux"

Joost Cassee

unread,
Nov 4, 2013, 9:55:17 AM11/4/13
to ansible...@googlegroups.com
2013/11/4 James Cammarata <jcamm...@ansibleworks.com>:
> 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

--
Joost Cassee
http://joost.cassee.net

Serge van Ginderachter

unread,
Nov 4, 2013, 9:57:10 AM11/4/13
to ansible...@googlegroups.com
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.



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

Note that the variable is interpolated in the debug task header!

Yes, as that happens when the playbook tasks are parsed, way before the runner is initialized.


Serge

James Cammarata

unread,
Nov 4, 2013, 9:59:16 AM11/4/13
to ansible...@googlegroups.com
Oops, my mistake, the variables should be space-separated, not comma-separated. So -e "foo=bar environment=qux"


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Joost Cassee

unread,
Nov 4, 2013, 10:01:03 AM11/4/13
to ansible...@googlegroups.com
2013/11/4 Serge van Ginderachter <se...@vanginderachter.be>:
> Yes, environment is a restricted keyword. [...]

Thanks for the info. What is the runner environment?

And could I have found this out myself?

Serge van Ginderachter

unread,
Nov 4, 2013, 10:16:14 AM11/4/13
to ansible...@googlegroups.com
On 4 November 2013 16:01, Joost Cassee <jo...@cassee.net> wrote:
> 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​

Serge van Ginderachter

unread,
Nov 4, 2013, 10:16:43 AM11/4/13
to ansible...@googlegroups.com

On 4 November 2013 15:59, James Cammarata <jcamm...@ansibleworks.com> wrote:
Oops, my mistake, the variables should be space-separated, not comma-separated. So -e "foo=bar environment=qux"


On Mon, Nov 4, 2013 at 8:55 AM, Joost Cassee <jo...@cassee.net> wrote:
2013/11/4 James Cammarata <jcamm...@ansibleworks.com>:
> Ahh, you can't do multiple "-e" options, just combine them: -e
> "foo=bar,environment=qux"


​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

Joost Cassee

unread,
Nov 4, 2013, 10:20:54 AM11/4/13
to ansible...@googlegroups.com
2013/11/4 Serge van Ginderachter <se...@vanginderachter.be>:
> On 4 November 2013 16:01, Joost Cassee <jo...@cassee.net> wrote:
>> > 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.

Serge van Ginderachter

unread,
Nov 4, 2013, 10:35:31 AM11/4/13
to ansible...@googlegroups.com
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.

Joost Cassee

unread,
Nov 4, 2013, 1:33:38 PM11/4/13
to ansible...@googlegroups.com

Thanks for the link, Serge!

Joost

Op 4 nov. 2013 16:35 schreef "Serge van Ginderachter" <se...@vanginderachter.be>:
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/fP0hX2Za4I0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Michael DeHaan

unread,
Nov 4, 2013, 6:33:13 PM11/4/13
to ansible...@googlegroups.com
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 :)

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 :)




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Joost Cassee

unread,
Nov 5, 2013, 6:52:21 PM11/5/13
to ansible...@googlegroups.com
2013/11/5 Michael DeHaan <mic...@ansibleworks.com>:
> 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
> :)

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!

Michael DeHaan

unread,
Nov 6, 2013, 9:10:42 AM11/6/13
to ansible...@googlegroups.com
yep, thanks!


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages