{"id":127,"date":"2025-06-23T10:45:36","date_gmt":"2025-06-23T08:45:36","guid":{"rendered":"https:\/\/blog.cp2i.com\/?p=127"},"modified":"2025-06-23T11:10:27","modified_gmt":"2025-06-23T09:10:27","slug":"chargement-de-variables-en-fonction-de-los-distribution-dans-ansible","status":"publish","type":"post","link":"https:\/\/blog.cp2i.com\/index.php\/2025\/06\/23\/chargement-de-variables-en-fonction-de-los-distribution-dans-ansible\/","title":{"rendered":"Chargement de variables en fonction de l&rsquo;OS\/Distribution dans Ansible"},"content":{"rendered":"\n<p>Parfois, on aimerait charger un fichier de variables ou une t\u00e2che en fonction de l&rsquo;OS ou de la distribution sur laquelle on d\u00e9ploie, sans pour autant changer le nom des variables.<\/p>\n\n\n\n<p>Je vous pr\u00e9sente ici une m\u00e9thode \u00e9l\u00e9gante de choisir un fichier de configuration\/t\u00e2che dans Ansible:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">- name: <span class=\"hljs-keyword\">Include<\/span> distribution specific variables\n  include_vars: <span class=\"hljs-string\">\"{{ lookup('first_found', params) }}\"<\/span>\n  vars:\n    params:\n      files:\n        - <span class=\"hljs-string\">\"{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml\"<\/span>\n        - <span class=\"hljs-string\">\"{{ ansible_facts.os_family }}-{{ ansible_facts.distribution_major_version }}.yml\"<\/span>\n        - <span class=\"hljs-string\">\"{{ ansible_facts.distribution }}.yml\"<\/span>\n        - <span class=\"hljs-string\">\"{{ ansible_facts.os_family }}.yml\"<\/span>\n        - <span class=\"hljs-keyword\">default<\/span>.yml\n      paths:\n        - <span class=\"hljs-string\">\"{{ role_path }}\/vars\"<\/span>\n\n- name: <span class=\"hljs-keyword\">Include<\/span> distribution specific tasks\n  include_tasks: <span class=\"hljs-string\">\"{{ lookup('first_found', params) }}\"<\/span>\n  vars:\n    params:\n      files:\n        - <span class=\"hljs-string\">\"{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml\"<\/span>\n        - <span class=\"hljs-string\">\"{{ ansible_facts.os_family }}-{{ ansible_facts.distribution_major_version }}.yml\"<\/span>\n        - <span class=\"hljs-string\">\"{{ ansible_facts.distribution }}.yml\"<\/span>\n        - <span class=\"hljs-string\">\"{{ ansible_facts.os_family }}.yml\"<\/span>\n        - <span class=\"hljs-keyword\">default<\/span>.yml\n      paths:\n        - <span class=\"hljs-string\">\"{{ role_path }}\/tasks\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Langage du code\u00a0:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Le <em>first_found <\/em>chargera donc le premier fichier trouv\u00e9 correspondant aux crit\u00e8res mentionn\u00e9s, dans l&rsquo;ordre, de la liste <em>params<\/em>.<\/p>\n\n\n\n<p>El\u00e9gant non ?<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Parfois, on aimerait charger un fichier de variables ou une t\u00e2che en fonction de l&rsquo;OS ou de la distribution sur laquelle on d\u00e9ploie, sans pour autant changer le nom des variables. Je vous pr\u00e9sente ici une m\u00e9thode \u00e9l\u00e9gante de choisir un fichier de configuration\/t\u00e2che dans Ansible: Le first_found chargera donc le premier fichier trouv\u00e9 correspondant [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_regular_price":[],"currency_symbol":[],"footnotes":""},"categories":[18,20],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-ansible","category-automatisation"],"post_slider_layout_featured_media_urls":{"thumbnail":"","post_slider_layout_landscape_large":"","post_slider_layout_portrait_large":"","post_slider_layout_square_large":"","post_slider_layout_landscape":"","post_slider_layout_portrait":"","post_slider_layout_square":"","full":""},"_links":{"self":[{"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":2,"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cp2i.com\/index.php\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}