Behavior change with union(query('vars', item)) when used with loop|list or loop|list vs loop|flatten?

7 views
Skip to first unread message

Bob Tanner

unread,
May 24, 2019, 2:49:52 PM5/24/19
to Ansible Project
Back in May of 2018 I asked "How to use group_vars when managed host is in multiple groups?" (see https://groups.google.com/d/msg/ansible-project/Tzb_cNpytD0/fB-xDIRFBAAJ) and Brian Coca present an excellent solution.

I've been using this solution since May 2018 but in the last 2 weeks I'm seeing different behavior.

Here are the tasks:

- name: set chocolatey packages to remove
  set_fact
:
    chocolatey_remove_packages
: "{{ chocolatey_remove_packages|union(query('vars', item)) }}"
  loop
: "{{ hostvars[inventory_hostname]|select('match','.*chocolatey_remove_packages$') | list }}"
  tags
: win_chocolatey

- debug:
    msg
: "{{ chocolatey_remove_packages }}"
  tags
: win_chocolatey


And the debug is below

    "msg": [
       
[],
       
[
           
"ccleaner",
           
"cura",
           
"edrawings-viewer",
           
"edrawings-viewer-2018",
           
"malwarebytes",
           
"uninstalltool",
           
"veeam-endpoint-backup-free",
           
"edrawings-viewer"
       
],
       
[
           
"3cx",
           
"firefox"
       
]
   
]

I am pretty sure I used to get

    "msg": [
       
[
           
"ccleaner",
           
"cura",
           
"edrawings-viewer",
           
"edrawings-viewer-2018",
           
"malwarebytes",
           
"uninstalltool",
           
"veeam-endpoint-backup-free",
           
"edrawings-viewer",
           
"3cx",
           
"firefox"
       
]
   
]

I think the empty list element is from the role/win_chocolatey/vars/main.yml where I initialize the chocolatey_remove_packages list and that never showed up until recently

---
# vars file for roles/win_chocolatey


# Initialize variable
#
chocolatey_remove_packages
: []

And the 

        [
           
"ccleaner",
           
"cura",
           
"edrawings-viewer",
           
"edrawings-viewer-2018",
           
"malwarebytes",
           
"uninstalltool",
           
"veeam-endpoint-backup-free",
           
"edrawings-viewer"
       
],


Is from my corporate wide variable and 

        [
           
"3cx",
           
"firefox"
       
]

    
Is from my group_vars

So my question is did something change with union(query('vars', item))  when used with loop | list ?

I've tried loop | flatten(levels=1) no difference.

Things continue to work with unusual output.

TASK [win_chocolatey : remove old/deprecated packages (choco)] *****************
ok
: [appserver] => (item=[])
ok
: [appserver] => (item=[u'ccleaner', u'cura', u'edrawings-viewer', u'edrawings-viewer-2018', u'malwarebytes', u'uninstalltool', u'veeam-endpoint-backup-free', u'edrawings-viewer'])
ok
: [appserver] => (item=[u'3cx', u'firefox'])

Changing the task that actually removed the package to use flatten(levels=1) 

- name: remove old/deprecated packages (choco)
  win_chocolatey
:
    name
: "{{ item }}"
    state
: "absent"
 
when:
   
- chocolatey_remove_packages is defined
  loop
:  "{{ chocolatey_remove_packages|flatten(levels=1) }}"

Givens me the output I've been used to.

Just looking for an explanation on what changed and where I missed this change in behavior.

Thanks.

Reply all
Reply to author
Forward
0 new messages