'selectattr returns generator rows and cannot use results as a dict
SOLUTION:
I Don't know what is the exact difference between python -m pip install ansible or apt install ansible but when I installed python -m pip ansible-core==2.10.4 it works fine.
I have CSV file which looks like:
id;env;credentials;path
1;tst;userA;/tmpA
2;dev;userB;/tmpB
3;dev;userB;/tmpC
4;acc;userB;/tmpD
5;prd;userC;/tmpE
I read this file using read_csv module and then I'm filtering using selectattr:
- name: Read CSV
read_csv:
path: "/tmp/example.csv"
delimiter: ';'
register: csv_output
- name: Filter rows
set_fact:
new_fact: "{{ csv_output.list | selectattr('env', 'equalto', tst) }}"
In the past I was able to just use these results as a dict so for example:
- debug:
msg: "{{ new_fact }}"
ok: [ansible_main] => {
"msg": [
{
"id": "1",
"env": "tst",
"credentials": "userA",
"path": "/tmpA"
}
]
}
but when I try to print new_fact on my local machine I see only generator:
ok: [ansible_main] => {
"msg": "<generator object select_or_reject at 0x7f2e4e8847b0>"
}
and I cannot use new_fact.credentials variable... Do you know how can I fix it? I know I can add | list at the end of my filter but then I also cannot use new_fact.credentials
Details of my installation:
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/userA/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
