On 07.08.2019 20:07, David Reagan wrote:
> How can I return a list from a dict based on a sub items value?
> 
> If I have a dictionary like this:
> 
> ospackages:
>>    fail2ban:
>>      state: "present"
>>      Ubuntu:
>>        1604: "fail2ban"
>>        1804: "fail2ban"
>>      Debian:
>>        9: "fail2ban"
>>      CentOS:
>>        7: "fail2ban"
>>      OracleLinux:
>>        7: "fail2ban"
>>    curl:
>>      state: "present"
>>      Ubuntu:
>>        1604: "curl"
>>        1804: "curl"
>>      Debian:
>>        9: "curl"
>>      CentOS:
>>        7: "curl"
>>      OracleLinux:
>>        7: "curl"
>>    locate:
>>      state: "absent"
>>      Ubuntu:
>>        1604: "mlocate"
>>        1804: "mlocate"
>>      Debian:
>>        9: "mlocate"
>>      CentOS:
>>        7: "mlocate"
>>      OracleLinux:
>>        7: "mlocate"
>>    ftp:
>>      state: "absent"
>>      Ubuntu:
>>        1604: "ftp"
>>        1804: "ftp"
>>      Debian:
>>        9: "ftp"
>>      CentOS:
>>        7: "ftp"
>>      OracleLinux:
>>        7: "ftp"
>>
> 
> I'd like select only the items from that dictionary based on the value of
> "state", and/or if a specific sub-dict has values.
> 
> I initially thought of this as a lookup plugin.
> 
> Something like:
> 
> lookup('dict_cond', ospackages, 'item.value.state == "present" and
> item.value.Ubuntu is defined and item.value.Ubuntu.1804 is defined', item.
> value.Ubuntu.1804)
> 
> 
> would return:
> 
> [ fail2ban, curl ]

I would not recommend using dict in this matter, a easier solution is to use 
include_vars
https://docs.ansible.com/ansible/latest/modules/include_vars_module.html

If you still wanna use dist it will look something like this

  {{ ospackages | dict2items | selectattr('value.state', 'eq', 'present') | 
list | map(attribute='value.Ubuntu.1804') | list }}


-- 
Kai Stian Olstad

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1202fd61-f6a3-b4d2-46ea-69464de83aee%40olstad.com.

Reply via email to