# No current variable in Ansible (like $\_ in perl)

**URL:** <https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371>\
**Category:** Ansible Developer\
**Created:** [June 1, 2015, 7:36pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371 "2015-06-01T19:36:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannig\_Perre](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannig\_Perre](https://forum.ansible.com/u/Yannig_Perre)\
**Post date:** [June 1, 2015, 7:36pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/1 "2015-06-01T19:36:56Z")

</div>

Hi all,

FIrst, thanks all for this great piece of software!

I have started an issue about a missing feature: there’s no current value in ansible playbook. When I talk about current value, I refer to $\_ in Perl. Usually, you use this variable when you don’t want to use a temporary variable. You can have a look at the following url: [https://github.com/ansible/ansible/issues/11107](https://github.com/ansible/ansible/issues/11107) You’ll find an example and the patch needed to enable this little feature.

Here is a little exemple of what I mean:

- Look if a path is a directory using a variable ‘p’:

```auto
- name: check a directory exist
  stat: path=/path/to/something
  register: p
  failed_when: 'p.stat.isdir is defined and p.stat.isdir'

```

- Same thing with the patch:

```auto
- name: check a directory exist
  stat: path=/path/to/something
  failed_when: '_.stat.isdir is defined and _.stat.isdir'

```

Mainly, you will not need register keyword anymore when you just don’t wan’t to give a name to a temp variable.

After some little exchange with bcoca and srvg, bcoca tails me to push this discussion on the mailing list to have more feedback about this variable name. Bcoca talks about the possibility that ‘_’ might be interfered and that maybe 'ansible_’ could be a better alternative. srvg talk about ‘\_reg’.

So here it is: what’s your opinion about that?

Regards,

Yannig

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [June 1, 2015, 8:00pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/2 "2015-06-01T20:00:43Z")

</div>

I agree that this is useful, mainly because we don't want to use these  
results outside of this task and don't want lots of temporary  
variables living outside their intended scope. But also because naming  
things is hard 🙂

In Perl it's called the "topic" and is equivalent to the English  
pronoun "it". I don't like "ansible\_" because it's too long (one of  
the chief benefits of "it" is that it's always shorter than the  
original thing it's referring to). Would you be open to using another  
symbol? I guess we'd be limited to what jinja supports, but I'm not  
seeing an obvious document for what that is.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 1, 2015, 8:06pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/3 "2015-06-01T20:06:03Z")

</div>

jinja2 supports any variable naming python does, the \_ is overloaded  
with several libs/meanings, so we want to avoid that.

The reason we want an 'ansible\_current' is to keep the ansible\_  
namespace that should indicate "if you tamper with this you are  
affecting the internal namespace and can expect weird stuff if you are  
not sure of what you are doing".

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [June 1, 2015, 8:12pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/4 "2015-06-01T20:12:03Z")

</div>

Can we build that warning into additional "namespaces" as well? Like  
anything starting with an \_ ?

---

<div class="post-metadata">

**Author:** ![Andreas\_Harpfer](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andreas\_Harpfer](https://forum.ansible.com/u/Andreas_Harpfer)\
**Post date:** [June 1, 2015, 8:17pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/5 "2015-06-01T20:17:49Z")

</div>

> Hi all,
> 
> FIrst, thanks all for this great piece of software!
> 
> I have started an issue about a missing feature: there's no current value in ansible playbook. When I talk about current value, I refer to $\_ in Perl.

[…]

> So here it is: what's your opinion about that?

I'd rather go with the Zen of Python and the notion that  
explicit is better than implicit. I think an explicit  
"register: p" is ways clearer than any behind the scenes  
"it"-magic. And Perl isn't partucularly notorious for it's  
readability … 😉

Of course these are just my personal 2¢.

Regards  
-Andi

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 1, 2015, 8:21pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/6 "2015-06-01T20:21:16Z")

</div>

Does anyone else think this is a bit ‘programming languagey’?

I take the point that naming things well is not easy but I like my playbooks best when they are readable by non programmers.

If you have a lot of processing to do on array members… well its doable in script or in templates.

Maybe I’m not seeing all the places this could be useful.

Jon

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 1, 2015, 9:03pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/7 "2015-06-01T21:03:49Z")

</div>

I am with Andi on this one. I am not a huge fan of the idea and its implicitness. I’d much prefer keeping it explicit.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 1, 2015, 9:37pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/8 "2015-06-01T21:37:28Z")

</div>

As much as i like typing less and the shorthand (yes, I'm the guy that  
enjoys using Perl), this is magic that does make ansible itself more  
cryptic and requires more knowledge.

The current explicit way of doing it, might be cumbersome to some, but  
will be very easy to read for all.

---

<div class="post-metadata">

**Author:** ![Jeff\_Reter](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@Jeff\_Reter](https://forum.ansible.com/u/Jeff_Reter)\
**Post date:** [June 2, 2015, 10:42am UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/9 "2015-06-02T10:42:49Z")

</div>

it’s not only about typing,  
its about scope,  
all this “all global” causes problems in big playbooks (many roles)

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [June 2, 2015, 11:44am UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/10 "2015-06-02T11:44:21Z")

</div>

+1 to keep thing explicit instead of implicit and writing playbook readable by non-programers as much as possible

---

<div class="post-metadata">

**Author:** ![mark.kusch](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@mark.kusch](https://forum.ansible.com/u/mark.kusch)\
**Post date:** [June 2, 2015, 11:59am UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/11 "2015-06-02T11:59:28Z")

</div>

Hi,

I really like the idea but I like beeing as explicit as possible.  
Anyway, I post because of the 'namespacing'.

I don't think that this is "not easy".

All roles I write have all variables prefixed with the roles' name,  
e.g. tomcat role, all variables are prefixed with tomcat\_.  
All dynamic variables generated with register: have two prefixes,  
the roles name and 'registered', e.g. tomcat\_registered\_.

This is a lot of typing, but this makes code readable and explicit  
and tries to reduce chance of clashing with other roles' variables.

Just another cents,

# kraM

---

<div class="post-metadata">

**Author:** ![Dann\_Bohn](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Dann\_Bohn](https://forum.ansible.com/u/Dann_Bohn)\
**Post date:** [June 2, 2015, 12:05pm UTC](https://forum.ansible.com/t/no-current-variable-in-ansible-like-in-perl/18371/12 "2015-06-02T12:05:19Z")

</div>

+1 if you’re afrid that a variable may have been set prior, you can always default it to NULL in the role, or use more descriptive names.
