selectattr 返回生成器行并且不能将结果用作字典
解决方案: 我不知道 python -m pip install ansible
或 apt install ansible
之间的确切区别是什么,但是当我安装 python -m pip ansible-core= =2.10.4
工作正常。
我有一个 CSV 文件,如下所示:
id;env;credentials;path
1;tst;userA;/tmpA
2;dev;userB;/tmpB
3;dev;userB;/tmpC
4;acc;userB;/tmpD
5;prd;userC;/tmpE
我使用 read_csv
模块读取此文件,然后使用 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) }}"
在过去,我只能将这些结果用作一个字典,例如:
- debug:
msg: "{{ new_fact }}"
ok: [ansible_main] => {
"msg": [
{
"id": "1",
"env": "tst",
"credentials": "userA",
"path": "/tmpA"
}
]
}
但是当我尝试在本地计算机上打印 new_fact
时,我只看到生成器:
ok: [ansible_main] => {
"msg": "<generator object select_or_reject at 0x7f2e4e8847b0>"
}
并且我无法使用 new_fact.credentials
变量...你知道吗我该如何修复它?我知道我可以添加 | list
在我的过滤器末尾,但我也无法使用 new_fact.credentials
我的安装详细信息:
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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于
它是比 v2.9.6 更新的 Ansible 版本吗?
因为你也会得到一个列表,所以有必要指定元素
来循环结果,或仅拾取一个元素。
Regarding
was it a much newer version of Ansible than v2.9.6?
Since you will get a list too, it will be necessary to specify the element
to loop over the result, or pickup one element only.