{"id":14988,"date":"2022-09-16T11:57:37","date_gmt":"2022-09-16T09:57:37","guid":{"rendered":"https:\/\/touk.pl\/blog\/?p=14988"},"modified":"2023-03-23T11:49:59","modified_gmt":"2023-03-23T10:49:59","slug":"deploying-multiple-apps-with-different-versions-using-a-single-ansible-command","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2022\/09\/16\/deploying-multiple-apps-with-different-versions-using-a-single-ansible-command\/","title":{"rendered":"Deploying multiple apps with different versions using a single Ansible command"},"content":{"rendered":"<h2 id=\"problem-description\">Problem description<\/h2>\n<p>In this article, we will look at how to deal with the following scenario. Our system consists of several applications. Let\u2019s imagine we need to deploy some (or all) of those apps with a single command. We also need to specify a common version for all apps or different versions for each app. The desired command could look like this:<\/p>\n<pre><code>ansible-playbook -i prod app-1.yml app-2.yml app-3.yml -e app_1_version=1.2.3 -e app_version=3.2.1<\/code><\/pre>\n<p>Such a command should result in the installation of app1 in version<br \/>\n<code>1.2.3<\/code> and remaining apps in version <code>3.2.1<\/code>.<\/p>\n<p>The main difficulty here is that we use the same tasks (e.g.\u00a0common role) for deploying all apps, so the app version variable is dynamic. Additionally, our app names can contain dashes (which is an invalid character in variable names) so we need to replace it with an underscore.<\/p>\n<h2 id=\"how-to-get-a-dynamic-variable\">How to get a dynamic variable<\/h2>\n<p>First things first. We need to have access to all variables if the names of those variables are dynamic. Variables must be passed in a form that allows us to retrieve their value using a dynamic variable name. This is where a <code>vars<\/code> variable comes to the rescue.<\/p>\n<p><code>vars<\/code> is a special variable in the form of a dictionary, where we can get a specific value using the syntax of <code>vars[key]<\/code>. Now, if we have our app name in a variable <code>app_name<\/code> we can get that variable using the syntax <code>vars[app_name | replace('-', '_') + '_version']<\/code>. Alternative syntax for this is <code>lookup('vars', app_name | replace('-', '_') + '_version')<\/code>, but the first one is more pleasant to us. Anyway, not bad, is it?<\/p>\n<h2 id=\"default-app-version\">Default app version<\/h2>\n<p>Here comes our additional requirement \u2013 a default app version. That\u2019s<br \/>\na piece of cake \u2013 we just have to check if a variable is defined and<br \/>\ntake the default version, right? Both solutions above come with some<br \/>\n<code>default<\/code> support.<\/p>\n<pre><code>vars[spring_app_name | replace('-', '_') + '_version'] | default(app_version) }}\r\nlookup('vars', spring_app_name | replace('-', '_') + '_version', default = app_version)<\/code><\/pre>\n<p>All we need to do now is to register it in a fact (like<br \/>\n<code>target_app_version<\/code>) and we are good to go. This is not very<br \/>\nreadable though, with all those braces and filters. Can we do it<br \/>\nbetter?<\/p>\n<h2 id=\"custom-filter-plugin\">Custom filter plugin<\/h2>\n<p>We can create our custom filter plugin. Let\u2019s see the usage first and focus on the implementation afterwards. In order to get the app version using a custom filter, we can use<br \/>\nsomething like this:<\/p>\n<pre><code>app_name | ver(vars)<\/code><\/pre>\n<p>To create a filter, we must add it in the folder <code>filter_plugins<\/code> of our role. Filters are written in python, so let\u2019s add the following content to <code>ROLE_NAME\/filter_plugins\/ver.py<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">class FilterModule(object):\r\n    def filters(self):\r\n        return {\r\n            'ver': self.ver\r\n        }\r\n\r\n    @staticmethod\r\n    def ver(app_name, vars):\r\n        app_name_version = app_name.replace('-', '_') + '_version'\r\n        if app_name_version in vars:\r\n            return vars[app_name_version]\r\n        elif 'app_version' in vars:\r\n            return vars['app_version']\r\n        else:\r\n            raise Exception('No ' + app_name_version + ' or app_version defined')\r\n<\/pre>\n<p>This solution also has an additional advantage \u2013 it raises a meaningful and descriptive error, which is much better than in previous solutions. <\/p>\n<h2 id=\"summary\">Summary<\/h2>\n<p>What we achieved is the ability to retrieve the app version from the command line parameter even if multiple apps were deployed at once. Additionally, we are able to specify the default version if a specific version for the app is not defined. Finally, we simplified playbooks code by moving complicated formulas to a custom plugin, providing a descriptive error as well.<\/p>\n","protected":false},"excerpt":{"rendered":"Problem description In this article, we will look at how to deal with the following scenario. Our system&hellip;\n","protected":false},"author":69,"featured_media":15254,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":{"0":"post-14988","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-development-design"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/14988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=14988"}],"version-history":[{"count":3,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/14988\/revisions"}],"predecessor-version":[{"id":15586,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/14988\/revisions\/15586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media\/15254"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=14988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=14988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=14988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}