ansible filter list when matches

2019-07-23 17:05发布

问题:

With groups.all that returns a list with all hosts in inventory, is it possible to extract the ones contains unique string into another list.

When I debug groups.all I get following list with all hosts in inventory.

"groups.all": [
    "host-1-unique",
    "host-2",
    "host-3",
    "host-4-unique",
    "host-5",
    "host-6-unique",
    "host-7"
],

In my main.yml

- set_fact:
  new_list: []

- set_fact: "{{ new_list }} + [ '{{ item }}' ]"
  with_items: groups.all
  when: 'unique' in groups.all

and I get following result:

{
    "skipped": true,
    "_ansible_no_log": false,
    "skip_reason": "Conditional result was False",
    "_ansible_item_result": true,
    "item": "groups.all",
    "changed": false
}

Does anyone know why this's been skipped? How do I extract from a list, that returns list of elements with unique string?

回答1:

You may want to learn about Jinja2 filters (built-in and Ansible supplied).

- set_fact:
    new_list: "{{ groups['all'] | select('search','unique') | list }}"


标签: ansible