{"id":721,"date":"2021-02-11T20:22:08","date_gmt":"2021-02-12T01:22:08","guid":{"rendered":"https:\/\/crossan007.dev\/blog\/?p=721"},"modified":"2021-02-11T20:31:57","modified_gmt":"2021-02-12T01:31:57","slug":"a-better-ansible_managed-tag","status":"publish","type":"post","link":"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/","title":{"rendered":"A Better ansible_managed Tag"},"content":{"rendered":"\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=crossan007\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n\n<p>If you&#8217;re using Ansible to manage configuration files on your servers, you&#8217;re probably familiar with the <code> <\/code><a href=\"https:\/\/docs.ansible.com\/ansible\/2.4\/intro_configuration.html#ansible-managed\" target=\"_blank\" rel=\"noreferrer noopener\"><code>{{ ansible_managed }}<\/code> tag <\/a>.  <\/p>\n\n\n\n<p>This tag is well behaved from an idempotentcy  perspective, but in the nitty-gritty of managing servers where there&#8217;s a chance of interaction by humans, this built-in tag leaves a lot to be desired.<\/p>\n\n\n\n<p>Enter <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/dev_guide\/developing_plugins.html#filter-plugins\" target=\"_blank\" rel=\"noreferrer noopener\">Ansible filter plugins<\/a>, and a little bit of magic in <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/intro_inventory.html#organizing-host-and-group-variables\">group_vars<\/a>.<\/p>\n\n\n\n<p>This filter plugin will obtain (according to <a href=\"https:\/\/git-scm.com\/docs\/pretty-formats\" target=\"_blank\" rel=\"noreferrer noopener\">git pretty-formats<\/a>) the commit hash, author, and date of the last commit to the file provided by the filter plugin structure:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/python\n# this should be saved to .\/filter_plugins\/get_last_commit_to_file.py\n\nclass FilterModule(object):\n  def filters(self):\n    return {\n      'get_last_commit_to_file': self.get_last_commit_to_file\n    }\n\n  def get_last_commit_to_file(self, file_path):\n    import os\n    try:  # py3\n      from shlex import quote\n    except ImportError:  # py2\n      from pipes import quote\n    stream = os.popen('git log -n 1 --pretty=format:\\'%h by %an at %aD\\' -- ' + quote(file_path))\n    output = stream.read()\n    return output\n<\/pre><\/div>\n\n\n<p>So, now we can define two new variables in our <code>group_vars\/all.yml<\/code> file: <code>ansible_workspace_git_url<\/code> and <code>ansible_managed_custom<\/code>.    <\/p>\n\n\n\n<p>Notice the use of our custom filter plugin  &#8211; we can pipe the variable <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/template_module.html#synopsis\" target=\"_blank\" rel=\"noreferrer noopener\">template_path variable (which Ansible gives to us for free)<\/a> into our filter plugin to obtain a string about the git history of our file!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nansible_workspace_git_url: &quot;https:\/\/github.com\/myrepo&quot;\n\nansible_managed_custom: &quot;\nAnsible Managed for {{ inventory_hostname }} ({{ansible_system_vendor}}::{{ansible_product_name}}::{{ansible_product_serial}}) \\n\nAnsible Git Repository URL: {{ ansible_workspace_git_url }}\\n\nInventory File: {{ inventory_file|replace(inventory_dir,'') }}@{{ inventory_file | get_last_commit_to_file }}\\n\nTemplate file: {{ template_path|replace(inventory_dir,'') }}@{{ template_path | get_last_commit_to_file }}\\n\nRole Context: {{ role_name | default('None') }}\\n\nRendered Template Target: {{ template_destpath }}&quot;\n<\/pre><\/div>\n\n\n<p>We now have available for use inside any template a nicely formatted (but not necessarily idempotent) header which gives any readers DETAILED information including:<\/p>\n\n\n\n<ul><li>The hostname and BIOS serial number of the host to which this file was rendered<\/li><li>The git repository of the playbook responsible for this template<\/li><li>The filename of the template used to render this file <\/li><li>The hash of the last commit which affected the template used to render this file<\/li><li>The filename of the inventory supplying variables to render this template<\/li><li>The hash of the last commit which affected the inventory supplying variables to render this template<\/li><li>The target path on the target server to where this file was rendered<\/li><\/ul>\n\n\n\n<p>Now, anytime I&#8217;m templating out a file (well, at least any file that supports multiline comments), I can just include our new <code>ansible_managed_custom<\/code> variable:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&lt;!--\n  {{ ansible_managed_custom }}\n--&gt; \n<\/pre><\/div>\n\n\n<p>By switching to this header format, I was able to better assist my teammates with identifying why a template was rendered in a particular fashion, as well as provide them details about how to fix any problems they may find.    This template format also helps answer questions about whether the rendered file was _copied_ from one server to another (possibly in a migration) and whether the file is likely to still be under Ansible management. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We now have available for use inside any template a nicely formatted (but not necessarily idempotent) header which gives any readers DETAILED information<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[229],"tags":[287,157,25,288],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Better ansible_managed Tag - Charles&#039; Blog\" \/>\n<meta property=\"og:description\" content=\"We now have available for use inside any template a nicely formatted (but not necessarily idempotent) header which gives any readers DETAILED information\" \/>\n<meta property=\"og:url\" content=\"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/\" \/>\n<meta property=\"og:site_name\" content=\"Charles&#039; Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-12T01:22:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-12T01:31:57+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"crossan007\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/crossan007.dev\/blog\/#website\",\"url\":\"https:\/\/crossan007.dev\/blog\/\",\"name\":\"Charles&#039; Blog\",\"description\":\"SharePoint | PowerShell | Exchange | SCCM | Ubuntu | PHP | JavaScript | A\/V Live Production | More...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/crossan007.dev\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/#webpage\",\"url\":\"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/\",\"name\":\"A Better ansible_managed Tag - Charles&#039; Blog\",\"isPartOf\":{\"@id\":\"https:\/\/crossan007.dev\/blog\/#website\"},\"datePublished\":\"2021-02-12T01:22:08+00:00\",\"dateModified\":\"2021-02-12T01:31:57+00:00\",\"author\":{\"@id\":\"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979\"},\"breadcrumb\":{\"@id\":\"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/crossan007.dev\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Better ansible_managed Tag\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979\",\"name\":\"crossan007\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/crossan007.dev\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g\",\"caption\":\"crossan007\"},\"url\":\"https:\/\/crossan007.dev\/blog\/author\/crossan007\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/","og_locale":"en_US","og_type":"article","og_title":"A Better ansible_managed Tag - Charles&#039; Blog","og_description":"We now have available for use inside any template a nicely formatted (but not necessarily idempotent) header which gives any readers DETAILED information","og_url":"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/","og_site_name":"Charles&#039; Blog","article_published_time":"2021-02-12T01:22:08+00:00","article_modified_time":"2021-02-12T01:31:57+00:00","twitter_misc":{"Written by":"crossan007","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/crossan007.dev\/blog\/#website","url":"https:\/\/crossan007.dev\/blog\/","name":"Charles&#039; Blog","description":"SharePoint | PowerShell | Exchange | SCCM | Ubuntu | PHP | JavaScript | A\/V Live Production | More...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/crossan007.dev\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/#webpage","url":"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/","name":"A Better ansible_managed Tag - Charles&#039; Blog","isPartOf":{"@id":"https:\/\/crossan007.dev\/blog\/#website"},"datePublished":"2021-02-12T01:22:08+00:00","dateModified":"2021-02-12T01:31:57+00:00","author":{"@id":"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979"},"breadcrumb":{"@id":"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/crossan007.dev\/blog\/ci-cd-tools\/a-better-ansible_managed-tag\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/crossan007.dev\/blog\/"},{"@type":"ListItem","position":2,"name":"A Better ansible_managed Tag"}]},{"@type":"Person","@id":"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979","name":"crossan007","image":{"@type":"ImageObject","@id":"https:\/\/crossan007.dev\/blog\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g","caption":"crossan007"},"url":"https:\/\/crossan007.dev\/blog\/author\/crossan007\/"}]}},"_links":{"self":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts\/721"}],"collection":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":12,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}