{{tag>Brouillon CA}} = Notes Ansible map select reject Map reduce filter avec Ansible Voir : * ''selectattr'' * https://networktocode.com/blog/jinja-map-review/ * https://runebook.dev/fr/docs/ansible/user_guide/complex_data_manipulation * https://docs.ansible.com/ansible/latest/user_guide/complex_data_manipulation.html * https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html * https://medium.com/opsops/how-to-filter-a-dictionary-in-ansible-b5dad74a6f66 * https://linuxhint.com/ansible-filter/ * https://www.middlewareinventory.com/blog/ansible-map/ Liste des filtres Jinja2 * https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/filter/core.py Développer ses propres filtres : * https://www.coveros.com/jinja2-filter/ == select (filter) ''select'' l'équivalent Ansible / Jinja à ''filter'' de Python Exemple : Enlever les éléments vides d'une liste # liste: {{ liste1 | difference(['']) }} # ou liste: {{ liste1 | select | list }} # Ce qui est équivalent à liste: {{ liste1 | select('!=', '') | list }} # et à liste: {{ liste1 | reject('==', '') | list }} Faire un grep - name: assert ansible-lint duplicate dict key assert: that: - ansible_lint_ref.rc != 1 msg: "{{ ansible_lint_ref.stderr_lines | select('regex', 'duplicate' ) | list }}" # Show only lines with "duplicate" keywork, because error msg is too much verbose Nettoyer une liste des éléments undef msg: '{{ foo | select("defined") }}' Voir aussi * ''select('match', 'plop')'' * selectattr, rejectattr == map Émuler ''any'' et ''all'' - hosts: localhost tasks: - assert: that: - mixed | any - not (mixed | all) - all_true | any - all_true | all - not (all_false | any) - not (all_false | all) vars: mixed: - false - true - false all_true: - true - true - true all_false: - false - false - false Sur ma version d'Ansible je n'ai pas ''any'' ni ''all'' Solution : vars: any: my_list | map('bool') | max all: my_list | map('bool') | min Faire un sed - name: Copy a glob of files based on a list of groups copy: src: "{{ item }}" dest: "/tmp/{{ item }}" loop: '{{ q("fileglob", *globlist) }}' vars: globlist: '{{ mygroups | map("regex_replace", "^(.*)$", "files/\1/*.conf") | list }}' === map attribute # La ligne ci-dessous : mounts: "{{ ansible_mounts | map(attribute='mount') |list }}" # équivaut à : mounts: "{{ ansible_mounts | json_query('[*].mount') }}" == Autres A tester - name: Convert shell: python -c "print [x for b in {{ servers }}['servers']['results'] for x in b['tagged_instances']]" register: my_list_of_dicts