Outils pour utilisateurs

Outils du site


tech:notes_ansible_map_select_reject

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
tech:notes_ansible_map_select_reject [2025/04/27 17:13] Jean-Baptistetech:notes_ansible_map_select_reject [2026/06/07 19:12] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{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
 +~~~yaml
 +
 +liste: {{ liste1 | difference([`]) }}
 +
 +# ou
 +liste: {{ liste1 | select | list }}
 +
 +# Ce qui est équivalent à 
 +liste: {{ liste1 | select('!=', `) | list }}
 +
 +# et à 
 +liste: {{ liste1 | reject('==', `) | list }}
 +~~~
 +
 +
 +Faire un grep
 +~~~yaml
 +- 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
 +~~~yaml
 +msg: '{{ foo | select("defined") }}'
 +~~~
 +
 +Voir aussi 
 +* `select('match', 'plop')`
 +* selectattr, rejectattr
 +
 +
 +## map
 +
 +Émuler `any` et `all`
 +
 +~~~yaml
 +- 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 :
 +~~~yaml
 +vars:
 +  any: my_list | map('bool') | max
 +  all: my_list | map('bool') | min
 +~~~
 +
 +Faire un sed
 +~~~yaml
 +      - 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
 +
 +~~~yaml
 +# La ligne ci-dessous :
 +mounts: "{{ ansible_mounts | map(attribute='mount') | list }}"
 +# équivaut à :
 +mounts: "{{ ansible_mounts | json_query('[*].mount') }}"
 +~~~
 +
 +## Autres
 +
 +A tester
 +~~~yaml
 +- name: Convert
 +  shell: python -c "print [x for b in {{ servers }}['servers']['results'] for x in b['tagged_instances']]"
 +  register: my_list_of_dicts
 +~~~
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki