gpt4 book ai didi

ansible - 无法将属性与数字进行比较。错误 : "not supported between instances of ' AnsibleUnsafeText' and 'int' "

转载 作者:行者123 更新时间:2023-12-04 14:15:22 26 4
gpt4 key购买 nike

- getent:
database: passwd
- debug:
var: getent_passwd | dict2items | selectattr('value.1', '>=', 1000) | map(attribute='key') | list

输出是

TASK [debug] ******************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on
({{getent_passwd | dict2items | selectattr('value.1', '>=', 1000) | map(attribute='key') | list}}):
'>=' not supported between instances of 'AnsibleUnsafeText' and 'int'"}

如何将“value.1”更改为整数?

最佳答案

问题:如何将 value.1 更改为整数?

答:使用 json_query函数 to_number .例如

    - debug:
var: getent_passwd|
dict2items|
json_query('[?to_number(value[1]) >= `1000`].key')

问题:如何将 1000 变成一个变量?

答:替换也应该转换为数字。单独声明 query 是个好主意。例如

    - set_fact:
myusers: "{{ getent_passwd|dict2items|json_query(query) }}"
vars:
myuid: 1000
query: "[?to_number(value[1]) >= to_number('{{ myuid }}')].key"

问:如何向 json_query 函数添加更多条件?例如 selectattr('value.5', 'ne', '/sbin/nologin')

答:使用pipeand-expression .例如

    - getent:
database: passwd
- set_fact:
myusers: "{{ getent_passwd|dict2items|json_query(query) }}"
vars:
myuid: 1000
myshell: /usr/sbin/nologin
query: "[?to_number(value[1]) >= to_number('{{ myuid }}')] |
[?value[5] == '{{ myshell }}'].{user: key, uid: value[1], shell: value[5]}"
- debug:
var: myusers

给予

    "myusers": [
{
"user": "libvirt-qemu",
"shell": "/usr/sbin/nologin",
"uid": "64055"
},
{
"user": "nobody",
"shell": "/usr/sbin/nologin",
"uid": "65534"
}
]

拟合变量和 Comparison Operator满足您的需求。

json_query 中的管道可能被认为是一种反模式。因此and-expression应该使用而不是管道。例如

  query: "[?(to_number(value[1]) >= to_number('{{ myuid }}')) &&
(value[5] == '{{ myshell }}')].{user: key, uid: value[1], shell: value[5]}"

关于ansible - 无法将属性与数字进行比较。错误 : "not supported between instances of ' AnsibleUnsafeText' and 'int' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60751070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com