tech:notes_ansible_jinja2
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:notes_ansible_jinja2 [2025/03/31 14:25] – Jean-Baptiste | tech:notes_ansible_jinja2 [2025/09/22 22:52] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| {{tag> | {{tag> | ||
| - | = Notes Ansible Jinja2 | + | # Notes Ansible Jinja2 |
| Ligne 11: | Ligne 12: | ||
| * https:// | * https:// | ||
| * j2cli | * j2cli | ||
| + | * [Mastering loops with Jinja templates in Ansible](https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | |||
| A la place du module **template** il est possible d' | A la place du module **template** il est possible d' | ||
| Ansible M(template) M(copy) | Ansible M(template) M(copy) | ||
| - | < | + | ~~~yaml |
| - name: Generate new resolv.conf | - name: Generate new resolv.conf | ||
| | | ||
| Ligne 23: | Ligne 29: | ||
| {% endfor %} | {% endfor %} | ||
| dest: resolv.conf.test | dest: resolv.conf.test | ||
| - | </ | + | ~~~ |
| + | ### Ansible jinja version | ||
| + | ~~~ | ||
| + | $ ansible --version |grep jinja | ||
| + | jinja version = 3.1.6 | ||
| + | ~~~ | ||
| - | === Jinja2 en ligne de commande j2cli j2 | ||
| - | ==== Install | + | ### Jinja2 en ligne de commande j2cli j2 |
| - | < | + | #### Install |
| + | |||
| + | ~~~bash | ||
| apt-get install j2cli | apt-get install j2cli | ||
| - | </ | + | ~~~ |
| ou | ou | ||
| - | < | + | ~~~bash |
| pip3 install jinja2-cli[yaml, | pip3 install jinja2-cli[yaml, | ||
| - | </ | + | ~~~ |
| - | ==== Exemples | + | #### Exemples |
| Exemple 1 | Exemple 1 | ||
| - | <code c nginx.conf.j2> | + | '' |
| + | ~~~nginx | ||
| server { | server { | ||
| listen 80; | listen 80; | ||
| Ligne 52: | Ligne 65: | ||
| index index.htm; | index index.htm; | ||
| } | } | ||
| - | </ | + | ~~~ |
| - | <code yaml nginx.yaml> | + | '' |
| + | ~~~yaml | ||
| --- | --- | ||
| Ligne 61: | Ligne 75: | ||
| webroot: "/ | webroot: "/ | ||
| - | </ | + | ~~~ |
| - | < | + | ~~~bash |
| # j2 -f json nginx.conf.j2 nginx.json > nginx.conf | # j2 -f json nginx.conf.j2 nginx.json > nginx.conf | ||
| j2 nginx.conf.j2 nginx.yaml > nginx.conf | j2 nginx.conf.j2 nginx.yaml > nginx.conf | ||
| - | </ | + | ~~~ |
| Exemple 2 | Exemple 2 | ||
| - | <code - timezone.j2> | + | '' |
| + | ~~~ | ||
| {{ TIMEZONE }} | {{ TIMEZONE }} | ||
| - | </ | + | ~~~ |
| - | <code bash ENV> | + | '' |
| + | ~~~bash | ||
| TIMEZONE=Europe/ | TIMEZONE=Europe/ | ||
| - | </ | + | ~~~ |
| - | < | + | ~~~bash |
| j2 -f env timezone.j2 ENV > timezone | j2 -f env timezone.j2 ENV > timezone | ||
| - | </ | + | ~~~ |
| - | === Regex | + | ### Regex |
| - | <code -> | + | ~~~ |
| {{ requete_conteneur.stdout | regex_replace(' | {{ requete_conteneur.stdout | regex_replace(' | ||
| - | </ | + | ~~~ |
| Exclure un élément d'une liste / enlever un élément d'une liste. | Exclure un élément d'une liste / enlever un élément d'une liste. | ||
| - | < | + | ~~~yaml |
| - name: DEBUG | - name: DEBUG | ||
| debug: msg=' | debug: msg=' | ||
| with_items: '{{ ansible_interfaces |difference([" | with_items: '{{ ansible_interfaces |difference([" | ||
| - | </ | + | ~~~ |
| - | === Condition IF | + | ### Condition IF |
| - | <code - / | + | '' |
| + | ~~~ | ||
| {% if ansible_distribution_version == ' | {% if ansible_distribution_version == ' | ||
| [mirorlinux-rh7.2-x86_64] | [mirorlinux-rh7.2-x86_64] | ||
| Ligne 108: | Ligne 125: | ||
| enabled=1 | enabled=1 | ||
| {% endif %} | {% endif %} | ||
| - | </ | + | ~~~ |
| - | <code -> | + | ~~~ |
| {% for user in users if not user.hidden %} | {% for user in users if not user.hidden %} | ||
| < | < | ||
| {% endfor %} | {% endfor %} | ||
| - | </ | + | ~~~ |
| - | === Valeur par défaut - variable undef | + | ### Valeur par défaut - variable undef |
| Erreur | Erreur | ||
| - | <code -> | + | ~~~ |
| fatal: [vmware-local]: | fatal: [vmware-local]: | ||
| - | </ | + | ~~~ |
| Code Ansible | Code Ansible | ||
| - | < | + | ~~~yaml |
| - name: conf / | - name: conf / | ||
| lineinfile: dest=/ | lineinfile: dest=/ | ||
| Ligne 131: | Ligne 148: | ||
| - "{{ servers }}" | - "{{ servers }}" | ||
| - 172.18.32.3 | - 172.18.32.3 | ||
| - | </ | + | ~~~ |
| Code Ansible corrigé | Code Ansible corrigé | ||
| - | < | + | ~~~yaml |
| - name: conf / | - name: conf / | ||
| lineinfile: dest=/ | lineinfile: dest=/ | ||
| Ligne 141: | Ligne 158: | ||
| - "{{ servers |default([]) }}" | - "{{ servers |default([]) }}" | ||
| - 172.18.32.3 | - 172.18.32.3 | ||
| - | </ | + | ~~~ |
| Liste vide avec item undef - with_item undef / empty : | Liste vide avec item undef - with_item undef / empty : | ||
| - | < | + | ~~~ |
| {{ item |default([]) }}'' | {{ item |default([]) }}'' | ||
| - | </ | + | ~~~ |
| + | |||
| + | |||
| + | ### Affichage - Alignement | ||
| + | |||
| + | Avec la méthode '' | ||
| + | |||
| + | ~~~jinja | ||
| + | {% for HOST in hosts %} | ||
| + | define host { | ||
| + | {% for k, v in HOST.items() %} | ||
| + | {{ k.ljust(20) }} {{ v }} | ||
| + | {% endfor %} | ||
| + | } | ||
| + | |||
| + | {% endfor %} | ||
| + | ~~~ | ||
| - | === Lever une exeption | + | ### Lever une exeption |
| Ligne 155: | Ligne 188: | ||
| Avec Ansibe | Avec Ansibe | ||
| - | <code -> | + | ~~~ |
| {{ ('OK text' if condition_ok) | mandatory(' | {{ ('OK text' if condition_ok) | mandatory(' | ||
| {{ ('' | {{ ('' | ||
| - | </ | + | ~~~ |
| Ou sinon plus simple : | Ou sinon plus simple : | ||
| - | <code -> | + | ~~~ |
| {{ 0/0 }} | {{ 0/0 }} | ||
| - | </ | + | ~~~ |
| - | == Linter | + | ## Linter |
| Voir : | Voir : | ||
| * https:// | * https:// | ||
| - | < | + | ~~~bash |
| sudo apt-get install python3-pip | sudo apt-get install python3-pip | ||
| - | </ | + | ~~~ |
| - | < | + | ~~~bash |
| python3 -m venv djlint | python3 -m venv djlint | ||
| source bin/ | source bin/ | ||
| pip install djlint | pip install djlint | ||
| - | </ | + | ~~~ |
| - | <code -> | + | ~~~ |
| $ ./ | $ ./ | ||
| Ligne 196: | Ligne 229: | ||
| Linted 1 file, found 2 errors. | Linted 1 file, found 2 errors. | ||
| - | </ | + | ~~~ |
| - | < | + | ~~~bash |
| djlint . --extension=html.j2 --lint | djlint . --extension=html.j2 --lint | ||
| Ligne 204: | Ligne 237: | ||
| djlint . --extension=html.j2 --reformat | djlint . --extension=html.j2 --reformat | ||
| - | </ | + | ~~~ |
| - | == Exemples | + | ## Exemples |
| - | === Variables - Whitespace Control - Exemple de contrôles de sauts de ligne et d' | + | ### Variables - Whitespace Control - Exemple de contrôles de sauts de ligne et d' |
| Voir : https:// | Voir : https:// | ||
| - | ==== Méthode 1 | + | #### Méthode 1 |
| * Utilisation de '' | * Utilisation de '' | ||
| Ligne 219: | Ligne 252: | ||
| * A la fin de la dernière condition ('' | * A la fin de la dernière condition ('' | ||
| - | <code -> | + | ~~~ |
| {%+ for KEY in CONF.SYS_GROUPS +%} | {%+ for KEY in CONF.SYS_GROUPS +%} | ||
| {%- if CONF.SYS_GROUPS[KEY].GID is defined -%} | {%- if CONF.SYS_GROUPS[KEY].GID is defined -%} | ||
| Ligne 227: | Ligne 260: | ||
| {%- endif +%} | {%- endif +%} | ||
| {%+ endfor +%} | {%+ endfor +%} | ||
| - | </ | + | ~~~ |
| - | ==== Méthode 2 | + | #### Méthode 2 |
| * Mettre des '' | * Mettre des '' | ||
| * A chaque fin de ligne utiliser : | * A chaque fin de ligne utiliser : | ||
| - | <code ->{{ ' | + | ~~~ |
| + | {{ ' | ||
| + | ~~~ | ||
| - | <code -> | + | ~~~ |
| {% for KEY in CONF.SYS_GROUPS -%} | {% for KEY in CONF.SYS_GROUPS -%} | ||
| {%- if CONF.SYS_GROUPS[KEY].GID is defined -%} | {%- if CONF.SYS_GROUPS[KEY].GID is defined -%} | ||
| Ligne 245: | Ligne 280: | ||
| {%- endif -%} | {%- endif -%} | ||
| {%- endfor %} | {%- endfor %} | ||
| - | </ | + | ~~~ |
| - | == Ansible Templating all j2 files | + | ## Ansible Templating all j2 files |
| - | < | + | ~~~yaml |
| - name: Templating all j2 files with config.yaml | - name: Templating all j2 files with config.yaml | ||
| gather_facts: | gather_facts: | ||
| Ligne 281: | Ligne 316: | ||
| vars: | vars: | ||
| dest: "{{ item | regex_replace(' | dest: "{{ item | regex_replace(' | ||
| - | </ | + | ~~~ |
| - | == Pb | + | ## Pb |
| - | === Err template error while templating string: no filter named ' | + | ### Err template error while templating string: no filter named ' |
| - | <code -> | + | ~~~ |
| fatal: [localhost]: | fatal: [localhost]: | ||
| - | </ | + | ~~~ |
| La version d' | La version d' | ||
| Ligne 298: | Ligne 333: | ||
| A la place du filtre '' | A la place du filtre '' | ||
| - | < | + | ~~~yaml |
| - name: Set facts - get values - resolv.conf | - name: Set facts - get values - resolv.conf | ||
| ansible.builtin.set_fact: | ansible.builtin.set_fact: | ||
| # resolv_lines_domains: | # resolv_lines_domains: | ||
| resolv_lines_domains: | resolv_lines_domains: | ||
| - | </ | + | ~~~ |
tech/notes_ansible_jinja2.1743423945.txt.gz · Dernière modification : de Jean-Baptiste
