{"id":911,"date":"2021-11-19T09:44:00","date_gmt":"2021-11-19T09:44:00","guid":{"rendered":"https:\/\/techno.slomka.biz\/?p=911"},"modified":"2026-03-03T12:06:35","modified_gmt":"2026-03-03T12:06:35","slug":"use-ansible-module-copy-instead-of-template-for-small-files","status":"publish","type":"post","link":"https:\/\/techno.slomka.biz\/?p=911","title":{"rendered":"use ansible module copy instead of template for small files"},"content":{"rendered":"\n<p>In order to adapt files and their content on target servers I am used to using <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/copy_module.html\" data-type=\"URL\" data-id=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/copy_module.html\" target=\"_blank\" rel=\"noreferrer noopener\">ansible copy module<\/a>. However, if you only have a small file with just one or a few lines, you can do so <em>without <\/em>creating a jinja2 template.<\/p>\n\n\n\n<p>Here is the little cheat: Just use <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/copy_module.html\" data-type=\"URL\" data-id=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/copy_module.html\" target=\"_blank\">ansible&#8217;s copy module<\/a>. <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#adbac7;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#22272e\"><span style=\"background:#9eadbd;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#22272e\">YAML<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>- name: Adjust content of file\n  ansible.builtin.copy:\n    content: \"homedir: {{ lookup('ansible.builtin.env', 'HOME')\"\n    dest: \/app\/env.yml<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ADBAC7\">- <\/span><span style=\"color: #8DDB8C\">name<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">Adjust content of file<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #8DDB8C\">ansible.builtin.copy<\/span><span style=\"color: #ADBAC7\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #8DDB8C\">content<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">&quot;homedir: {{ lookup(&#39;ansible.builtin.env&#39;, &#39;HOME&#39;)&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #8DDB8C\">dest<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">\/app\/env.yml<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>That&#8217;s it.  This creates a file with variable content, without need to create a template.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to adapt files and their content on target servers I am used to using ansible copy module. However, if you only have a small file with just one or a few lines, you can do so without creating a jinja2 template. Here is the little cheat: Just use ansible&#8217;s copy module. That&#8217;s it. This creates a file with variable content, without need to create a template.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,3],"tags":[40,128],"class_list":["post-911","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","tag-ansible","tag-jinja2"],"_links":{"self":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=911"}],"version-history":[{"count":10,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions"}],"predecessor-version":[{"id":1077,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions\/1077"}],"wp:attachment":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}