在我的Ansible角色中,我有。
rcube_plugins_repo:
- name: 'account_details'
repo: 'https://github.com/texxasrulez/account_details'
config: 'yes'
- name: 'delete_old'
repo: 'https://github.com/ron4mac/roundcube_delete_old'
and so on...
在我的一个任务中,我需要检查一下 account_details
存在于 rcube_plugins_repo
我怎么做呢?
我想过这样的方法。
when: "rcube_plugins_repo.name['account_details'] is defined"
但这行不通,我也不知道该怎么写。
非常感谢!
rcube_plugins_repo
是一个列表 dict
所以 rcube_plugins_repo.name
首先就不能用。即使你得到了这样的值,你也必须与account_details进行比较,而不是检查一个 account_details
子元素。
selectattr
filter允许你根据过滤器过滤一个dicts列表。在这里,你想根据name属性的值进行过滤。
我尝试了下面的方法,似乎很有效。
when: "{{ rcube_plugins_repo|selectattr('name', 'eq', 'account_details')|list }}"
如果被过滤的列表是空的,那么条件就不会被满足.