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/04/04 20:11] – Jean-Baptiste | tech:notes_ansible_jinja2 [2025/09/22 22:52] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes Ansible Jinja2 | ||
| + | |||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * j2cli | ||
| + | * [Mastering loops with Jinja templates in Ansible](https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | A la place du module **template** il est possible d' | ||
| + | Ansible M(template) M(copy) | ||
| + | ~~~yaml | ||
| + | - name: Generate new resolv.conf | ||
| + | | ||
| + | mode: "{{ stat_resolv_conf.stat.mode }}" | ||
| + | | ||
| + | {% for nameserver in nameservers %} | ||
| + | | ||
| + | {% endfor %} | ||
| + | dest: resolv.conf.test | ||
| + | ~~~ | ||
| + | |||
| + | ### Ansible jinja version | ||
| + | |||
| + | ~~~ | ||
| + | $ ansible --version |grep jinja | ||
| + | jinja version = 3.1.6 | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Jinja2 en ligne de commande j2cli j2 | ||
| + | |||
| + | #### Install | ||
| + | |||
| + | ~~~bash | ||
| + | apt-get install j2cli | ||
| + | ~~~ | ||
| + | |||
| + | ou | ||
| + | ~~~bash | ||
| + | pip3 install jinja2-cli[yaml, | ||
| + | ~~~ | ||
| + | |||
| + | #### Exemples | ||
| + | |||
| + | Exemple 1 | ||
| + | |||
| + | '' | ||
| + | ~~~nginx | ||
| + | server { | ||
| + | listen 80; | ||
| + | server_name {{ nginx.hostname }}; | ||
| + | |||
| + | root {{ nginx.webroot }}; | ||
| + | index index.htm; | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | '' | ||
| + | ~~~yaml | ||
| + | --- | ||
| + | |||
| + | nginx: | ||
| + | hostname: localhost | ||
| + | webroot: "/ | ||
| + | |||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | # j2 -f json nginx.conf.j2 nginx.json > nginx.conf | ||
| + | j2 nginx.conf.j2 nginx.yaml > nginx.conf | ||
| + | ~~~ | ||
| + | |||
| + | Exemple 2 | ||
| + | |||
| + | '' | ||
| + | ~~~ | ||
| + | {{ TIMEZONE }} | ||
| + | ~~~ | ||
| + | |||
| + | '' | ||
| + | ~~~bash | ||
| + | TIMEZONE=Europe/ | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | j2 -f env timezone.j2 ENV > timezone | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ### Regex | ||
| + | |||
| + | ~~~ | ||
| + | {{ requete_conteneur.stdout | regex_replace(' | ||
| + | ~~~ | ||
| + | |||
| + | Exclure un élément d'une liste / enlever un élément d'une liste. | ||
| + | |||
| + | ~~~yaml | ||
| + | - name: DEBUG | ||
| + | debug: msg=' | ||
| + | with_items: '{{ ansible_interfaces |difference([" | ||
| + | ~~~ | ||
| + | |||
| + | ### Condition IF | ||
| + | |||
| + | ''/ | ||
| + | ~~~ | ||
| + | {% if ansible_distribution_version == ' | ||
| + | [mirorlinux-rh7.2-x86_64] | ||
| + | name=Plop RH 7.2 | ||
| + | baseurl=http:// | ||
| + | gpgcheck=0 | ||
| + | enabled=1 | ||
| + | {% endif %} | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | {% for user in users if not user.hidden %} | ||
| + | < | ||
| + | {% endfor %} | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Valeur par défaut - variable undef | ||
| + | |||
| + | Erreur | ||
| + | ~~~ | ||
| + | fatal: [vmware-local]: | ||
| + | ~~~ | ||
| + | |||
| + | Code Ansible | ||
| + | ~~~yaml | ||
| + | - name: conf / | ||
| + | lineinfile: dest=/ | ||
| + | with_items: | ||
| + | - "{{ servers }}" | ||
| + | - 172.18.32.3 | ||
| + | ~~~ | ||
| + | |||
| + | Code Ansible corrigé | ||
| + | ~~~yaml | ||
| + | - name: conf / | ||
| + | lineinfile: dest=/ | ||
| + | with_items: | ||
| + | #- "{{ servers |default('' | ||
| + | - "{{ servers |default([]) }}" | ||
| + | - 172.18.32.3 | ||
| + | ~~~ | ||
| + | |||
| + | Liste vide avec item undef - with_item undef / empty : | ||
| + | ~~~ | ||
| + | {{ 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 | ||
| + | |||
| + | |||
| + | Source https:// | ||
| + | |||
| + | Avec Ansibe | ||
| + | ~~~ | ||
| + | {{ ('OK text' if condition_ok) | mandatory(' | ||
| + | |||
| + | {{ ('' | ||
| + | ~~~ | ||
| + | |||
| + | Ou sinon plus simple : | ||
| + | ~~~ | ||
| + | {{ 0/0 }} | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ## Linter | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | ~~~bash | ||
| + | sudo apt-get install python3-pip | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | python3 -m venv djlint | ||
| + | source bin/ | ||
| + | pip install djlint | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | $ ./ | ||
| + | |||
| + | Linting 1/1 files ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 00:00 | ||
| + | |||
| + | |||
| + | Dockerfile.j2 | ||
| + | ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | ||
| + | H025 19:9 Tag seems to be an orphan. <EOF > | ||
| + | H014 132:27 Found extra blank lines. e/pcc/logs | ||
| + | |||
| + | Linted 1 file, found 2 errors. | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | djlint . --extension=html.j2 --lint | ||
| + | |||
| + | djlint . --extension=html.j2 --check | ||
| + | |||
| + | djlint . --extension=html.j2 --reformat | ||
| + | ~~~ | ||
| + | |||
| + | ## Exemples | ||
| + | |||
| + | ### Variables - Whitespace Control - Exemple de contrôles de sauts de ligne et d' | ||
| + | |||
| + | Voir : https:// | ||
| + | |||
| + | |||
| + | #### Méthode 1 | ||
| + | |||
| + | * Utilisation de '' | ||
| + | * Utilisation de '' | ||
| + | * A la fin de la dernière condition ('' | ||
| + | |||
| + | ~~~ | ||
| + | {%+ for KEY in CONF.SYS_GROUPS +%} | ||
| + | {%- if CONF.SYS_GROUPS[KEY].GID is defined -%} | ||
| + | RUN groupadd -g {{ CONF.SYS_GROUPS[KEY].GID }} {{ KEY }} | ||
| + | {%- else -%} | ||
| + | RUN groupadd {{ KEY }} | ||
| + | {%- endif +%} | ||
| + | {%+ endfor +%} | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | #### Méthode 2 | ||
| + | |||
| + | * Mettre des '' | ||
| + | * A chaque fin de ligne utiliser : | ||
| + | ~~~ | ||
| + | {{ ' | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | {% for KEY in CONF.SYS_GROUPS -%} | ||
| + | {%- if CONF.SYS_GROUPS[KEY].GID is defined -%} | ||
| + | RUN groupadd -g {{ CONF.SYS_GROUPS[KEY].GID }} {{ KEY + ' | ||
| + | {%- else -%} | ||
| + | RUN groupadd {{ KEY + ' | ||
| + | {%- endif -%} | ||
| + | {%- endfor %} | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Ansible Templating all j2 files | ||
| + | |||
| + | ~~~yaml | ||
| + | - name: Templating all j2 files with config.yaml | ||
| + | gather_facts: | ||
| + | hosts: localhost | ||
| + | |||
| + | tasks: | ||
| + | - name: Include vars of config.yaml | ||
| + | no_log: true | ||
| + | ansible.builtin.include_vars: | ||
| + | file: config.yaml | ||
| + | name: CONF | ||
| + | |||
| + | - name: Find all j2 templates files | ||
| + | register: reg_find_j2tpls | ||
| + | ansible.builtin.find: | ||
| + | paths: " | ||
| + | patterns: " | ||
| + | hidden: false | ||
| + | file_type: file | ||
| + | recurse: true | ||
| + | excludes: | ||
| + | - .git | ||
| + | |||
| + | - name: Templating j2 files | ||
| + | ansible.builtin.template: | ||
| + | src: "{{ item }}" | ||
| + | dest: "{{ dest }}" | ||
| + | mode: " | ||
| + | loop: "{{ reg_find_j2tpls.files | map(attribute=' | ||
| + | vars: | ||
| + | dest: "{{ item | regex_replace(' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ## Pb | ||
| + | |||
| + | ### Err template error while templating string: no filter named ' | ||
| + | |||
| + | ~~~ | ||
| + | fatal: [localhost]: | ||
| + | ~~~ | ||
| + | |||
| + | La version d' | ||
| + | |||
| + | A la place du filtre '' | ||
| + | |||
| + | ~~~yaml | ||
| + | - name: Set facts - get values - resolv.conf | ||
| + | ansible.builtin.set_fact: | ||
| + | # resolv_lines_domains: | ||
| + | resolv_lines_domains: | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
