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

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

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

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.

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".

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

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 … :wink:

Of course these are just my personal 2¢.

Regards
-Andi

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

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.

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.

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

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

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

+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.