selectattr 返回生成器行并且不能将结果用作字典

发布于 2025-01-11 12:39:54 字数 1736 浏览 0 评论 0原文

解决方案: 我不知道 python -m pip install ansibleapt 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2025-01-18 12:39:55

关于

过去我只能将这些结果用作字典......

它是比 v2.9.6 更新的 Ansible 版本吗?

我知道我可以添加| list 在我的过滤器末尾,但随后我也无法使用 new_fact.credentials 然后

因为你也会得到一个列表,所以有必要指定元素

  - name: Filter rows
    set_fact:
      new_fact: "{{ csv_output.list | selectattr('env', 'contains', 'tst') | list }}"

  - debug:
      msg: "{{ new_fact[0].credentials }}"

来循环结果,或仅拾取一个元素。

  - name: Filter rows
    set_fact:
      new_fact: "{{ csv_output.list | selectattr('env', 'contains', 'tst') | first }}"

  - debug:
      msg: "{{ new_fact.credentials }}"

Regarding

In the past I was able to just use these results as a dict ...

was it a much newer version of Ansible than v2.9.6?

I know I can add | list at the end of my filter but then I also cannot user new_fact.credentials then

Since you will get a list too, it will be necessary to specify the element

  - name: Filter rows
    set_fact:
      new_fact: "{{ csv_output.list | selectattr('env', 'contains', 'tst') | list }}"

  - debug:
      msg: "{{ new_fact[0].credentials }}"

to loop over the result, or pickup one element only.

  - name: Filter rows
    set_fact:
      new_fact: "{{ csv_output.list | selectattr('env', 'contains', 'tst') | first }}"

  - debug:
      msg: "{{ new_fact.credentials }}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文