ansible / proposals

Repository for sharing and tracking progress on enhancement proposals for Ansible.
Creative Commons Zero v1.0 Universal
93 stars 19 forks source link

redesign variable interface #127

Open bcoca opened 6 years ago

bcoca commented 6 years ago

Proposal: redesign vars

Author: Brian Coca <@bcoca> IRC: bcoca

Date: 2018/31/31

Motivation

Simplify variables for both users and developers

Problems

Solution proposal

Redesign the variable system

Accessible naked and/or via specific dicts:

Documentation (optional)

Also much simpler than current, hopefully the new system is much more intuitive

barlik commented 6 years ago

This is such an amazing proposal.

Variable scoping is IMHO the weakest point of Ansible. I hate the fact that I can't set a local variable inside a role (E.g. using register on a task or using set_fact module) and I agree that variable precedence rules are error prone and confusing for everybody.

Make role vars private by default: Add facility for roles to ‘import vars to global or play or 1st container’?

For this, I would prefer having an option to return a variable from a role:

For example:

- include_role:
    name: myrole
  vars:
    myrole_variable1: var1
  register: myrole_out

- set: varname=play_var value=myrole_out.private_var1  
alikins commented 6 years ago

https://github.com/ansible/ansible/pull/28477 is a pr that implemented tracking of where variables "came from" and displaying that info at higher verbosity levels for troubleshooting. It's not an implementation of the proposal here, but it was rooted in the frustration that the variable scoping and precedence can cause.

The two main aspects of it were:

  1. Specifying a _'scopename' when using combinevars() to update variable manager. Additional info about the scope to provide extra context via the 'scopeinfo'. For ex, for a variable that comes from defaults/main.yml, the _'scopename' is 'play_rolesdefaults', and the 'scopeinfo' is the dict {'role_name': 'myrole', 'role_path': '/etc/ansible/roles/myrole'}

  2. A data structure to keep track of where the variables come from and the scope_name and scope_info associated with them. The tracking data knows all the scopes that provided a value for a variable and what the the final result was. For an implementation more tightly integrated into variable_manager, this could be done in variable_manager itself.

That branch is intended as a way to debug and troubleshoot the existing very complicated variable precedence rules. A simpler and more straightforward variable precedence scheme would be better in general, but it could still be useful to support introspection of the origins of the final set of variables.

eugene-bright commented 5 years ago

Relates to ansible/ansible#6189.