ara icon indicating copy to clipboard operation
ara copied to clipboard

Templating values in a playbook's vars for ara_playbook_labels doesn't work

Open dmsimard opened this issue 5 years ago • 5 comments

What is the issue ?

It doesn't look possible to use templated values in ara_playbook_labels right now, for example:

- name: Test playbook
  hosts: localhost
  gather_facts: no
  vars:
    test_label: "test_label"
    ara_playbook_labels:
      - foo
      - "{{ test_label }}"
  tasks:
    - name: Print ara_playbook_labels
      debug:
        var: ara_playbook_labels

    - name: Retrieve the current playbook so we can inspect labels
      ara_playbook:
      register: playbook_query

    - name: Print actual playbook labels
      debug:
        msg: "{{ playbook_query.playbook.labels }}"

Returns:

> ansible-playbook -i 'localhost,' -c local tests/integration/labels.yaml
Operations to perform:
  Apply all migrations: admin, api, auth, contenttypes, db, sessions
Running migrations:
  No migrations to apply.

PLAY [Test playbook] **************************************************************************************************

TASK [Print ara_playbook_labels] **************************************************************************************
ok: [localhost] => {
    "ara_playbook_labels": [
        "foo",
        "test_label"
    ]
}

TASK [Retrieve the current playbook so we can inspect labels] *********************************************************
ok: [localhost]

TASK [Print actual playbook labels] ***********************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "id": 12,
            "name": "check:False"
        },
        {
            "id": 15,
            "name": "tags:all"
        },
        {
            "id": 29,
            "name": "foo"
        },
        {
            "id": 30,
            "name": "{{ test_label }}"
        }
    ]
}

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

I guess we'd expect the variables to be templated by the time the play begins and where we set the labels: https://github.com/ansible-community/ara/blob/40894c7027144c4d875e5d5a97e5cfebf75ec318/ara/plugins/callback/ara_default.py#L277-L289

What should be happening ?

It would be useful to be able to template labels so it'd be great if this worked :)

dmsimard avatar Oct 08 '20 14:10 dmsimard

I went asking on irc about this, and got some important info:

          svg | When using playbook vars that have jinja templates, in ca callback plugin, those vars don't get templated. I presume the callback plugin must make sure those get templated? I was  
              | looking for existing callback plugins, but none of them seem to do any templating, afaics. Anyone knows more about that?                                                            
       @bcoca | callback plugins are not supposed to get vars                                                                                                                                       
 felixfontein | svg: in an action plugin I wrote, I use the following to get the actual value of a variable name: `self._templar.template(variable_name, convert_bare=True,                         
              | preserve_trailing_newlines=False)`                                                                                                                                                  
       @bcoca | they should only get 'results' from the action                                                                                                                                      
            ⤷ | felixfontein: action plugins should not need to do that as the 'variables' they get should be templated                                                                             
 felixfontein | bcoca: it's for getting values of variables that are not arguments for the action plugin                                                                                            
       @bcoca | let me reprhase, the variables that get passed in as arguments are templated                                                                                                        
 felixfontein | yep :)                                                                                                                                                                              
       @bcoca | felixfontein: i would not consider access to other vars a 'feature' they should have and might not be able to do so in future                                                       
 felixfontein | my action plugin is a very-special-case plugin which replaces a specific `template` call by something that evaluates 100x faster, and for that I need to evaluate some variables    
       @bcoca | same with callbacks, right now i know you can access via 'object tree spelunking' .. but that is something we intend to remove for many reasons                                     
          svg | This is actually about https://github.com/ansible-community/ara/issues/180                                                                                                          
       @bcoca | felixfontein: 100x faster ... shouldnt we just use that for templating?                                                                                                             
 felixfontein | bcoca: the template is replaced by Python code which produces the desired output. it's not a generic templating system :)                                                           
       @bcoca | ;-p                                                                                                                                                                                 
          svg | in this case def v2_playbook_on_play_start(self, play)  play has the play_vars                                                                                                      
       @bcoca | svg: a) that is not 'full vars' b) those are teh unprocesseed values  c) probably wont have any at all in the future                                                                
 felixfontein | bcoca: for this special case, I think the hack is worth it. I wouldn't really do that in generic plugins, mostly because this is kind of "magic" since the plugin's behavior should 
              | not depend on things that are not explicitly passed in.                                                                                                                             
       @bcoca | felixfontein: exactly                                                                                                                                                               
            ⤷ | but as is .. we left a lot of holes in intial design, which creates many issues, performance and security wise                                                                      
            ⤷ | goal is to in the end remove those 'extra things' that get passed, it will increase both a lot                                                                                      
 felixfontein | performance usually doesn't increase when doing more encapsulation :)                                                                                                               
       @bcoca | the upcoming 'move templating to main proc' will probably kill all these 'extra stuff' being passed to most plugins                                                                 
            ⤷ | felixfontein: it does when you stop serializing and passing back and forth huge ammounts of data that are not needed                                                                
 felixfontein | bcoca: yep, that's true. in general it's not easy to know which precise data is needed though.                                                                                      
       @bcoca | consider that if inventory + vars == 2gb of data, that is being serialized and passed right now to every worker, while worker normaly needs < 5% of said data                       
            ⤷ |  felixfontein but we do!  its the arguments passed to the action                                                                                                                    
            ⤷ | and the action definition itself (already templated, so no need for extras)                                                                                                         
 felixfontein | well, not for the `template` action plugin :)                                                                                                                                       
       @bcoca | well, template needs variables as an extra, but that is the one special case                                                                                                        
          svg | felixfontein, how do you initialize Templar?                                                                                                                                        
       @bcoca | and easy to deal with                                                                                                                                                               
       @bcoca | svg: can look at template action for example                                                                                                                                        
            ⤷ | but dont count on it working in future versions                                                                                                                                     
 felixfontein | svg: action plugins get it for free. not sure how to get it running for callbacks.                                                                                                  
       @bcoca | callbacks dont get it, also you need more vars than you get from play_vars for all templating to work                                                                               
 felixfontein | bcoca: the template action doesn't need to set up the templar, since it is already provided for action plugins                                                                      
          svg | I see. ARA Might need a different implementation for what it's trying to do here.                                                                                                   
       @bcoca | felixfontein: as is loader                                                                                                                                                          
       @bcoca | svg: probably                                                                                                                                                                       
          svg | dmsimard, ^^                                                                                                                                                                        
       @bcoca | svg: if you want to record all vars used on task/play , you might want to look at overriding normal.py                                                                              
            ⤷ | be advised, that will probably be huge performance hit and will have many security issues attached                                                                                  

srgvg avatar Oct 08 '20 14:10 srgvg

Thanks for lodging this as an issue. If there is some way to do this without being too computationally costly in some future version of ARA, it would be cool if 'ara_playbook_name' defaulted to the special variable 'ansible_play_name'.

kfiresmith avatar Apr 05 '21 13:04 kfiresmith

Hello

any news about this feature request?

it would be nice when the vars ara_playbook_name & ara_playbook_labels will resolved like

vars: ara_playbook_name: "{{ application }}" ara_playbook_labels: - "env:{{ env }}"

Thanks

christianwohlgemuth avatar Apr 05 '22 13:04 christianwohlgemuth

I haven't revisited this issue in a while but I'm not against the feature though it depends what is necessary to implement it.

It is not one of my priorities right now so if someone wants to tackle it, feel free.

dmsimard avatar Apr 08 '22 18:04 dmsimard