There are 2 issues:. 

   1. The loop can become costly as the number of entries rise: I have 
   files with hundreds (potentially thousands) of servers/workstations.
   2. it also results in as many answers as the number of entries, although 
   only one is non empty. I don't know how to set a variable with the single 
   right answer.

```
        - debug:
                msg: "{{ item.workstations|
                           selectattr('name', 'eq', os_name)|
                           selectattr('revision', 'eq', os_revision)|
                           map(attribute= 'url')|
                           list }}"
          loop: "{{ domain_definition }}"
          when: item.workstations|length > 0
          vars:
                os_name: ubuntu1
                os_revision: '2017-08-16'
```
leads to:
```
ok: [localhost] => (item={'name': 'server01', 'cluster': 'cluster0', 
'port': '8070', 'workstations': [{'name': 'name_default', 'revision': 
'revision_default', 'url': 'url_default'}]}) => {
    "msg": []
}
ok: [localhost] => (item={'name': 'server11', 'cluster': 'cluster1', 
'port': '8080', 'workstations': [{'name': 'debian1', 'revision': 
'2016-06-21', 'url': 'http://debian1.example.com:9911/2016-06-21'}, 
{'name': 'ubuntu1', 'revision': '2017-08-16', 'url': 
'http://ubuntu1.example.com:9912/2017-08-16'}]}) => {
    "msg": [
        "http://ubuntu1.example.com:9912/2017-08-16";
    ]
}
ok: [localhost] => (item={'name': 'server12', 'cluster': 'cluster2', 
'port': '8090', 'workstations': [{'name': 'debian2', 'revision': 
'2018-04-15', 'url': 'http://debian2.example.com:9921/2018-04-15'}, 
{'name': 'ubuntu2', 'revision': '2020-01-12', 'url': 
'http://ubuntu2.example.com:9922/2020-01-12'}]}) => {
    "msg": []
}
```
Isn't there a loopless solution? 

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d64886dc-1d71-45dd-8ce9-56b5a5b78c90n%40googlegroups.com.

Reply via email to